Skip to content

Commit

Permalink
Release v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Niweera committed May 4, 2022
1 parent f67b65a commit 675efd2
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 37 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "niwder-api",
"version": "0.1.1",
"version": "0.1.2",
"description": "API for Niwder; Download, upload, and go wild with Mega, Google Drive and direct download links.",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -71,6 +71,7 @@
"morgan": "^1.10.0",
"multer": "^1.4.4",
"parse-torrent": "^9.1.5",
"socket.io": "^4.5.0",
"swagger-ui-express": "^4.3.0",
"webtorrent": "^1.8.14",
"webtorrent-health": "^1.1.2"
Expand Down
23 changes: 9 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Keys from "./keys";
import chalk from "chalk";
import server from "./server";
import server, { io } from "./server";
import FirebaseService from "./services/FirebaseService";

const { PORT } = Keys;
Expand All @@ -21,24 +21,19 @@ server.on("close", () => {
console.log(chalk.yellow("Niwder-API is shutting down"));
});

process.on("SIGINT", () => {
server.close(async (error) => {
await FirebaseService.setServerDead(interval);
if (error) {
process.exit(1);
} else {
process.exit(0);
}
const shutDownAPI = () => {
io.close((error: Error) => {
if (error) console.log(`Error occurred in Socket.io ${error}`);
});
});

process.on("SIGTERM", () => {
server.close(async (error) => {
server.close(async (error: Error) => {
await FirebaseService.setServerDead(interval);
if (error) {
process.exit(1);
} else {
process.exit(0);
}
});
});
};

process.on("SIGINT", shutDownAPI);
process.on("SIGTERM", shutDownAPI);
1 change: 1 addition & 0 deletions src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export default {
OAUTH_REDIRECT_URL: "https://niwder.niweera.gq/transfers",
GDRIVE_FOLDER_NAME: "Niwder",
MEGA_FOLDER_NAME: "Niwder",
SOCKET_IO_CORS: ["http://localhost:9091", "https://niwder.niweera.gq"],
};
33 changes: 33 additions & 0 deletions src/middleware/socketIOMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Server as IOServer, Socket } from "socket.io";
import type {
ClientToServerEvents,
InterServerEvents,
ServerToClientEvents,
SocketData,
} from "../utilities/interfaces";
import type { Server } from "http";
import keys from "../keys";

export const SetupSocketIO = (server: Server): IOServer => {
const io: IOServer = new IOServer<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>(server, {
cors: {
origin: keys.SOCKET_IO_CORS,
},
});

io.on("connection", (socket: Socket) => {
console.log("socket connected:", socket.connected);
console.log("client connected:", socket.id);
socket.emit("api-alive");
socket.on("disconnect", () => {
console.log(`client disconnected:`, socket.id);
});
});

return io;
};
3 changes: 3 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import ErrorHandlingMiddleware from "../middleware/error-handling";
import FirebaseAuthMiddleware from "../middleware/firebaser";
import ServeOpenAPI from "../openapi";
import { SetupSocketIO } from "../middleware/socketIOMiddleware";
import type { Server as IOServer } from "socket.io";

export const app: Application = express();
app.disable("x-powered-by");
Expand All @@ -24,4 +26,5 @@ FirebaseAuthMiddleware(app);
app.use("", mainController);
ErrorHandlingMiddleware(app);

export const io: IOServer = SetupSocketIO(server);
export default server;
1 change: 1 addition & 0 deletions src/services/FirebaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class FirebaseService {
interval: ReturnType<typeof setInterval>
) => {
try {
console.log("Server status DEAD recorded.");
await db.ref("live").set(false);
if (interval) clearInterval(interval);
} catch (e) {
Expand Down
19 changes: 19 additions & 0 deletions src/utilities/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ export type TorrentsHealth = {
webtorrent: TorrentHealth;
bittorrent: TorrentHealth;
};

export interface ServerToClientEvents {
noArg: () => void;
basicEmit: (a: number, b: string, c: Buffer) => void;
withAck: (d: string, callback: (e: number) => void) => void;
}

export interface ClientToServerEvents {
hello: () => void;
}

export interface InterServerEvents {
ping: () => void;
}

export interface SocketData {
name: string;
age: number;
}
10 changes: 4 additions & 6 deletions src/worker/DBWorker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import DBService from "../Services/DBService";

FirebaseService.attachDBListeners(DBService.listenToRemovalsCB);

process.on("SIGINT", () => {
const shutDownDBWorker = () => {
FirebaseService.removeListeners(DBService.listenToRemovalsCB);
process.exit(0);
});
};

process.on("SIGTERM", () => {
FirebaseService.removeListeners(DBService.listenToRemovalsCB);
process.exit(0);
});
process.on("SIGINT", shutDownDBWorker);
process.on("SIGTERM", shutDownDBWorker);
14 changes: 4 additions & 10 deletions src/worker/TorrentsWorker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,14 @@ worker.on("progress", (job: Job, progress: number) => {
console.log(keys.TORRENTS_QUEUE, job.name, progress);
});

process.on("SIGINT", async () => {
const shutDownTorrentsWorker = async () => {
await worker.close(true);
client.destroy(async (error: Error) => {
if (error) console.log(error.message);
console.log("WebTorrent client destroyed");
process.exit(0);
});
});
};

process.on("SIGTERM", async () => {
await worker.close(true);
client.destroy(async (error: Error) => {
if (error) console.log(error.message);
console.log("WebTorrent client destroyed");
process.exit(0);
});
});
process.on("SIGINT", shutDownTorrentsWorker);
process.on("SIGTERM", shutDownTorrentsWorker);
10 changes: 4 additions & 6 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,10 @@ worker.on("progress", (job: Job, progress: number) => {
console.log(keys.MAIN_QUEUE, job.name, job.data.url, progress);
});

process.on("SIGINT", async () => {
const shutDownWorker = async () => {
await worker.close(true);
process.exit(0);
});
};

process.on("SIGTERM", async () => {
await worker.close(true);
process.exit(0);
});
process.on("SIGINT", shutDownWorker);
process.on("SIGTERM", shutDownWorker);

0 comments on commit 675efd2

Please sign in to comment.