|
@ -1,9 +1,25 @@ |
|
|
import fastify from "fastify"; |
|
|
import fastify from "fastify"; |
|
|
import fastifyStatic from "fastify-static"; |
|
|
import fastifyStatic from "fastify-static"; |
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
|
|
|
import fastifyMultipart from "fastify-multipart"; |
|
|
|
|
|
import fastifyFormBody from "fastify-formbody"; |
|
|
|
|
|
import assert from "assert"; |
|
|
import { join } from "path"; |
|
|
import { join } from "path"; |
|
|
|
|
|
import TorrentClientIpc from "./services/TorrentClientIpc"; |
|
|
|
|
|
|
|
|
|
|
|
let ipc = new TorrentClientIpc; |
|
|
|
|
|
ipc.boot(); |
|
|
|
|
|
|
|
|
|
|
|
// @TODO Proper typing https://www.fastify.io/docs/v1.14.x/TypeScript/
|
|
|
|
|
|
|
|
|
let server = fastify(); |
|
|
let server = fastify(); |
|
|
|
|
|
server.register(fastifyMultipart, { |
|
|
|
|
|
limits: { |
|
|
|
|
|
fileSize: 16*1024*1024, |
|
|
|
|
|
files: 50 |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
server.register(fastifyFormBody); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* If the app is in production mode, serve static assets. |
|
|
* If the app is in production mode, serve static assets. |
|
@ -24,8 +40,40 @@ if (process.env["NODE_ENV"] == "production") { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
server.get("/test", (request, reply) => { |
|
|
|
|
|
reply.send({result: "Success"}) |
|
|
|
|
|
|
|
|
// API Routes --------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
server.get("/api/torrent/list", async (request, reply) => { |
|
|
|
|
|
return await ipc.list(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
server.get("/api/torrent/:infoHash", async (request, reply) => { |
|
|
|
|
|
let infoHash: string = (<any>request.params)["infoHash"]; |
|
|
|
|
|
assert(infoHash !== undefined, "Invalid request"); |
|
|
|
|
|
return { result: await ipc.details(infoHash) }; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
server.post("/api/torrent/add", async (request, reply) => { |
|
|
|
|
|
let result: string[] = []; |
|
|
|
|
|
if (request.isMultipart()) { |
|
|
|
|
|
let files = request.files(); |
|
|
|
|
|
for await (let file of files) { |
|
|
|
|
|
let torrentFile = await file.toBuffer(); |
|
|
|
|
|
let infoHash = await ipc.add(torrentFile); |
|
|
|
|
|
result.push(infoHash); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
assert(request.body !== undefined, "Invalid request: empty body"); |
|
|
|
|
|
let links = (<any>request.body)["link"]; |
|
|
|
|
|
assert(links !== undefined, "Invalid request: empty links"); |
|
|
|
|
|
for (let link of (Array.isArray(links) ? links : [links])) { |
|
|
|
|
|
let infoHash = await ipc.add(link); |
|
|
|
|
|
result.push(infoHash); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return { result: result }; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Listening on port:", parseInt(<string>process.env["SERVER_PORT"])); |
|
|
server.listen(parseInt(<string>process.env["SERVER_PORT"]), "0.0.0.0"); |
|
|
server.listen(parseInt(<string>process.env["SERVER_PORT"]), "0.0.0.0"); |