From a6de7aaf18c80d9b28b46531a63f2ea3bd41c407 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 15 Aug 2023 18:39:35 +0800 Subject: [PATCH] Make sure everything is done with es6 classes and introduce a Worker thread --- src/main.ts | 78 ++++++++++++++++++++++++++++++++++++++++++++------ src/routes.ts | 22 -------------- src/webpush.ts | 3 -- src/worker.ts | 26 +++++++++++++++++ 4 files changed, 95 insertions(+), 34 deletions(-) delete mode 100644 src/routes.ts delete mode 100644 src/webpush.ts create mode 100644 src/worker.ts diff --git a/src/main.ts b/src/main.ts index ed03d64..c08d346 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,72 @@ -import express from 'express'; -import bodyParser from 'body-parser'; -import router from './routes.js'; +import { Subscription, SubscriptionService } from './subscriptionService.js'; +import express, { Express, Request, Response } from 'express'; +import path from 'path'; +import { Worker } from 'worker_threads'; -const app = express(); +class Server { + private app: Express; + private port: number; + private worker?: Worker; + private subscriptionService: SubscriptionService; -app.use(bodyParser.json()); -app.use(router); + constructor(port: number) { + this.app = express(); + this.port = port; + this.subscriptionService = new SubscriptionService(); -app.listen(3000, () => { - console.log('Server is running on port 3000'); -}); + this.setupRoutes(); + this.startWorker(); + this.setupWorkerListeners(); + } + + private setupRoutes(): void { + this.app.post('/subscribe', async (req: Request, res: Response) => { + const subscription = req.body as Subscription; + const subscriptionService = new SubscriptionService(); + await subscriptionService.addSubscription(subscription); + res.status(201).send(); + }); + } + + private startWorker(): void { + const workerPath = path.join(__dirname, 'worker.js'); + + this.worker = new Worker(workerPath, { workerData: 'world' }); + + this.worker.on('message', (message) => { + console.log(`Received message from worker: ${message}`); + }); + + this.worker.on('error', (error) => { + console.error(`Worker error: ${error.message}`); + }); + + this.worker.on('exit', (code) => { + if (code !== 0) { + console.error(`Worker stopped with exit code ${code}`); + } + }); + } + + private setupWorkerListeners(): void { + if (this.worker) { + this.worker.on('message', (message) => { + if (message.type === 'REQUEST_DATA') { + this.subscriptionService.fetchSubscriptions().then(data => { + this.worker?.postMessage({ type: 'DATA_RESPONSE', data }); + }); + } + }); + } + } + + public start(): void { + this.app.listen(this.port, () => { + console.log(`Server is running on http://localhost:${this.port}`); + }); + } +} + +// Initialize and start the server +const server = new Server(3000); +server.start(); diff --git a/src/routes.ts b/src/routes.ts deleted file mode 100644 index a650d3e..0000000 --- a/src/routes.ts +++ /dev/null @@ -1,22 +0,0 @@ -import express from 'express'; -import { SubscriptionService, Subscription } from './subscriptionService.js'; -import { NotificationService, Message } from './notificationService.js'; - -const router = express.Router(); - -router.post('/subscribe', async (req, res) => { - const subscription = req.body as Subscription; - const subscriptionService = new SubscriptionService(); - await subscriptionService.addSubscription(subscription); - res.status(201).send(); -}); - -router.post('/broadcast', async (req, res) => { - const message = req.body as Message; - const notificationService = new NotificationService(); - - notificationService.broadcast(message); - res.status(201).send(); -}); - -export default router; diff --git a/src/webpush.ts b/src/webpush.ts deleted file mode 100644 index 147c866..0000000 --- a/src/webpush.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class WebPushService { - -} diff --git a/src/worker.ts b/src/worker.ts new file mode 100644 index 0000000..5091ebb --- /dev/null +++ b/src/worker.ts @@ -0,0 +1,26 @@ +import { parentPort } from 'worker_threads'; +import { Message, NotificationService } from './notificationService.js' + +class WorkerThread { + private interval: number; + private notificationService: NotificationService; + private message: Message; + + constructor(interval: number) { + this.interval = interval; + this.notificationService = new NotificationService(); + this.message.title = "Check the app."; + + this.startPeriodicTask(); + } + + private startPeriodicTask(): void { + setInterval(() => { + if (parentPort) { + this.notificationService.broadcast(this.message); + } + }, this.interval); + } +} + +new WorkerThread(3600 * 24); // pole once per day