From ba75b0ff45d47062ddb4308e7459a83008093c80 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 6 Apr 2021 12:38:15 -0500 Subject: [PATCH] Added some basic routes and implemented the IPC. All temporary --- src/server/index.ts | 52 +++++++++++++++++++++++++++++++++++++++++++-- tsconfig.json | 2 +- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/server/index.ts b/src/server/index.ts index 52fe5cf..aa971db 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,9 +1,25 @@ import fastify from "fastify"; import fastifyStatic from "fastify-static"; 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 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(); +server.register(fastifyMultipart, { + limits: { + fileSize: 16*1024*1024, + files: 50 + } +}); +server.register(fastifyFormBody); /** * 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 = (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 = (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(process.env["SERVER_PORT"])); server.listen(parseInt(process.env["SERVER_PORT"]), "0.0.0.0"); diff --git a/tsconfig.json b/tsconfig.json index 5cf66c1..1f6b863 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - // "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ // "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */