diff --git a/articles b/articles index 172da35..1af78ed 160000 --- a/articles +++ b/articles @@ -1 +1 @@ -Subproject commit 172da3590a2e7cec7b41dcf9a60306db277b7159 +Subproject commit 1af78edd4c4e4e5b3834303e19f3507998019825 diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index 88aa140..189b72f 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -1,12 +1,11 @@ import pg from 'pg'; -const { Pool } = pg; import { getConnection } from './database/get_connection'; export class Postgres { client: pg.PoolClient | null = null; - public static async new(pool: Pool) { + public static async new(pool: pg.Pool) { const pg = new Postgres(); - pg.client = await getConnection().connect(); + pg.client = await pool.connect(); return pg; } @@ -32,7 +31,7 @@ export class Postgres { } export default async ( - pool: Pool + pool: pg.Pool ) => { return await Postgres.new(pool); }; import { building } from '$app/environment'; diff --git a/src/lib/server/database/init_db.ts b/src/lib/server/database/init_db.ts index 1964187..cd090f8 100644 --- a/src/lib/server/database/init_db.ts +++ b/src/lib/server/database/init_db.ts @@ -2,8 +2,15 @@ import type { Postgres } from '$lib/server/database'; import fs from 'fs'; import { compile } from 'mdsvex'; +import { execSync } from 'child_process'; + export default async function init(db: Postgres) { - // Create tables(when not exists) + if (fs.existsSync('./articles/')) { + console.log('Pulling articles from git..'); + const stdout = execSync('git pull', { cwd: './articles/' }); + console.log(stdout.toString()); + } + const schemas = [ { name: 'article', @@ -107,7 +114,7 @@ export default async function init(db: Postgres) { for (const { path, id } of articleFiles) { const res = await db.query('select * from article where id = $1', [id]); const compiled = await compile(fs.readFileSync(path, 'utf-8')); - + const title = compiled.data.fm.title; const category = path.split('/')[3]; const tags: string[] = compiled.data.fm.tags; diff --git a/src/routes/api/fetch_articles/+server.ts b/src/routes/api/fetch_articles/+server.ts new file mode 100644 index 0000000..2520687 --- /dev/null +++ b/src/routes/api/fetch_articles/+server.ts @@ -0,0 +1,23 @@ +import { json } from '@sveltejs/kit'; +import { error } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; + +import { + TOKEN +} from '$env/static/private' + +import PG from '$lib/server/database'; +import { getConnection } from '$lib/server/database/get_connection'; +// import { building } from '$app/environment'; +import init from '$lib/server/database/init_db'; + +export const POST: RequestHandler = async ({ url }) => { + const token = url.searchParams.get('token'); + console.log(token); + if (token !== TOKEN) { + return error(401, 'Unauthorized'); + } + + await init(await PG(getConnection())); + return new Response(String(token)); +};