diff --git a/src/db.ts b/src/db.ts new file mode 100644 index 0000000..159f2a8 --- /dev/null +++ b/src/db.ts @@ -0,0 +1,58 @@ +import { Database } from 'sqlite3'; + +export class DBService { + private db: Database; + + constructor() { + this.db = new Database('subscriptions.db', (err) => { + if (err) { + console.error("Failed to connect to the database:", err); + return; + } + console.log("Connected to SQLite database"); + this.createTable(); + }); + } + + private createTable() { + const createSubscriptionsTableSQL = ` + CREATE TABLE IF NOT EXISTS subscriptions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + endpoint TEXT NOT NULL, + keys_p256dh TEXT NOT NULL, + keys_auth TEXT NOT NULL + ); + `; + + this.db.run(createSubscriptionsTableSQL, (err) => { + if (err) throw err; + console.log("Subscriptions table ready"); + }); + } + + saveSubscription(endpoint: string, keys_p256dh: string, keys_auth: string) { + return new Promise((resolve, reject) => { + const insertSQL = `INSERT INTO subscriptions (endpoint, keys_p256dh, keys_auth) VALUES (?, ?, ?)`; + this.db.run(insertSQL, [endpoint, keys_p256dh, keys_auth], function(err) { + if (err) { + reject(new Error(`Failed to insert subscription: ${err.message}`)); + return; + } + resolve(); + }); + }); + } + + getSubscriptions(): Promise> { + return new Promise((resolve, reject) => { + const selectSQL = `SELECT * FROM subscriptions`; + this.db.all(selectSQL, [], (err, rows) => { + if (err) { + reject(new Error(`Failed to retrieve subscriptions: ${err.message}`)); + return; + } + resolve(rows); + }); + }); + } +} diff --git a/src/routes.ts b/src/routes.ts index 63d36f5..93d56e4 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,18 +1,22 @@ import express from 'express'; -import { saveSubscription, getSubscriptions } from './subscriptions.js'; +import { SubscriptionService } from './subscriptionService.js'; import { WebPushService } from './webpush.js'; const router = express.Router(); -router.post('/subscribe', (req, res) => { +router.post('/subscribe', async (req, res) => { const subscription = req.body; - saveSubscription(subscription); + const subscriptionService = new SubscriptionService(); + await subscriptionService.addSubscription(subscription); res.status(201).send(); }); -router.post('/sendNotification', (_, res) => { +router.post('/sendNotification', async (_, res) => { const webPush = new WebPushService(); - const subscriptions = getSubscriptions(); + + const subscriptionService = new SubscriptionService(); + const subscriptions = await subscriptionService.fetchSubscriptions(); + const data = { title: "Notification Title", message: "This is a message from your web push server" diff --git a/src/subscriptionService.ts b/src/subscriptionService.ts new file mode 100644 index 0000000..04c1bb8 --- /dev/null +++ b/src/subscriptionService.ts @@ -0,0 +1,29 @@ +import { DBService } from './db.js'; + +export interface Subscription { + endpoint: string; + keys: { + p256dh: string; + auth: string; + }; +} + +export class SubscriptionService { + private dbService: DBService; + + constructor() { + this.dbService = new DBService(); + } + + async addSubscription(subscription: Subscription): Promise { + await this.dbService.saveSubscription( + subscription.endpoint, + subscription.keys.p256dh, + subscription.keys.auth + ); + } + + async fetchSubscriptions(): Promise { + return this.dbService.getSubscriptions(); + } +} diff --git a/src/subscriptions.ts b/src/subscriptions.ts deleted file mode 100644 index 1c33464..0000000 --- a/src/subscriptions.ts +++ /dev/null @@ -1,18 +0,0 @@ -export type Subscription = { - endpoint: string; - keys: { - p256dh: string; - auth: string; - }; - }; - - let subscriptions: Subscription[] = []; - - export function saveSubscription(sub: Subscription) { - subscriptions.push(sub); - } - - export function getSubscriptions(): Subscription[] { - return subscriptions; - } - \ No newline at end of file diff --git a/src/webpush.ts b/src/webpush.ts index 2fe7417..f4e0f95 100644 --- a/src/webpush.ts +++ b/src/webpush.ts @@ -1,5 +1,5 @@ import * as http_ece from 'http_ece'; -import { Subscription } from './subscriptions.js'; +import { Subscription } from './subscriptionService.js'; import * as jwt from 'jsonwebtoken'; import * as https from 'https';