Matthew Raymer
1 year ago
4 changed files with 95 additions and 34 deletions
@ -1,12 +1,72 @@ |
|||||
import express from 'express'; |
import { Subscription, SubscriptionService } from './subscriptionService.js'; |
||||
import bodyParser from 'body-parser'; |
import express, { Express, Request, Response } from 'express'; |
||||
import router from './routes.js'; |
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()); |
constructor(port: number) { |
||||
app.use(router); |
this.app = express(); |
||||
|
this.port = port; |
||||
|
this.subscriptionService = new SubscriptionService(); |
||||
|
|
||||
app.listen(3000, () => { |
this.setupRoutes(); |
||||
console.log('Server is running on port 3000'); |
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(); |
||||
|
@ -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; |
|
@ -1,3 +0,0 @@ |
|||||
export class WebPushService { |
|
||||
|
|
||||
} |
|
@ -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…
Reference in new issue