Browse Source

Added some basic routes and implemented the IPC. All temporary

master
David Ludwig 4 years ago
parent
commit
ba75b0ff45
2 changed files with 51 additions and 3 deletions
  1. +50
    -2
      src/server/index.ts
  2. +1
    -1
      tsconfig.json

+ 50
- 2
src/server/index.ts View File

@ -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 = (<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");

+ 1
- 1
tsconfig.json View File

@ -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. */


Loading…
Cancel
Save