Browse Source

Make sure everything is done with es6 classes and introduce a Worker thread

pull/1/head
Matthew Raymer 1 year ago
parent
commit
a6de7aaf18
  1. 78
      src/main.ts
  2. 22
      src/routes.ts
  3. 3
      src/webpush.ts
  4. 26
      src/worker.ts

78
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();

22
src/routes.ts

@ -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;

3
src/webpush.ts

@ -1,3 +0,0 @@
export class WebPushService {
}

26
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
Loading…
Cancel
Save