diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8b33d00 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.env +.svelte-kit +build/ \ No newline at end of file diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts new file mode 100644 index 0000000..b8edc70 --- /dev/null +++ b/src/lib/db/index.ts @@ -0,0 +1,54 @@ +import { MongoClient } from 'mongodb'; +import { env } from '$env/dynamic/private'; +import type { Db, Collection } from 'mongodb'; +import type PasteSchema from "./paste-schema"; +import { get } from 'http'; + +// let client: MongoClient; +// let db: Db; +// let pastes: Collection; + +// const connect = async (url: string) => { +// client = new MongoClient(url); +// await client.connect(); +// const db = client.db("paste69"); +// pastes = db.collection("pastes"); +// } + +// export { +// db, +// pastes, +// connect, +// } + +interface Collections { + pastes: Collection; +} + +export class Mongo { + private static instance: Promise | null = null; + + private constructor() {} + + public static getClient(): Promise { + if (!Mongo.instance) { + Mongo.instance = MongoClient.connect(env.DB_URL); + } + + return Mongo.instance; + } + + public static async getDb(): Promise { + const client = await Mongo.getClient(); + return client.db("paste69"); + } + + public static async getCollection(name: string): Promise> { + const db = await Mongo.getDb(); + return db.collection(name); + } + + public static async getNamedCollection(name: name): Promise { + return Mongo.getCollection(name) as Promise; + } +} \ No newline at end of file diff --git a/src/lib/db/paste-schema.ts b/src/lib/db/paste-schema.ts new file mode 100644 index 0000000..7fb1c3a --- /dev/null +++ b/src/lib/db/paste-schema.ts @@ -0,0 +1,8 @@ +export default interface PasteSchema { + id: string; + contents: string; + highlight: string; + encrypted: boolean; + burnAfterReading: boolean; + createdAt: Date; + } \ No newline at end of file diff --git a/src/routes/stats/+page.server.ts b/src/routes/stats/+page.server.ts new file mode 100644 index 0000000..5a72225 --- /dev/null +++ b/src/routes/stats/+page.server.ts @@ -0,0 +1,28 @@ +import { Mongo } from "$lib/db/index"; +import type { PageServerLoad } from "./$types"; +import { env } from '$env/dynamic/private'; +import { error } from '@sveltejs/kit'; + +export const load: PageServerLoad = async () => { + // Check if the stats page is enabled + if (!env.STATS_ENABLED) { + throw error(404, 'Paste not found'); + } + + const pastes = await Mongo.getNamedCollection('pastes'); + + // Total pastes + const totalPastes = await pastes.countDocuments(); + + // Total encrypted pastes + const totalEncryptedPastes = await pastes.countDocuments({ encrypted: true }); + + // Total pastes with burnAfterReading enabled (and not yet read) + const totalBurnAfterReadingPastes = await pastes.countDocuments({ burnAfterReading: true }); + + return { + totalPastes, + totalEncryptedPastes, + totalBurnAfterReadingPastes, + }; +} \ No newline at end of file diff --git a/src/routes/stats/+page.svelte b/src/routes/stats/+page.svelte new file mode 100644 index 0000000..129313b --- /dev/null +++ b/src/routes/stats/+page.svelte @@ -0,0 +1,39 @@ + + + + Paste69 - Server stats + + +
+ +
+ +
+

Server Stats

+ +

+ Total pastes: {totalPastes} +

+ +

+ Total encrypted pastes: {totalEncryptedPastes} +

+ +

+ Total burn after reading pastes (unread): {totalBurnAfterReadingPastes} +

+
+ +
+ +
\ No newline at end of file