Compare commits

..

7 Commits

4 changed files with 36 additions and 7 deletions

@ -1 +1 @@
Subproject commit 172da3590a2e7cec7b41dcf9a60306db277b7159 Subproject commit 1af78edd4c4e4e5b3834303e19f3507998019825

View File

@ -1,12 +1,11 @@
import pg from 'pg'; import pg from 'pg';
const { Pool } = pg;
import { getConnection } from './database/get_connection'; import { getConnection } from './database/get_connection';
export class Postgres { export class Postgres {
client: pg.PoolClient | null = null; client: pg.PoolClient | null = null;
public static async new(pool: Pool) { public static async new(pool: pg.Pool) {
const pg = new Postgres(); const pg = new Postgres();
pg.client = await getConnection().connect(); pg.client = await pool.connect();
return pg; return pg;
} }
@ -32,7 +31,7 @@ export class Postgres {
} }
export default async ( export default async (
pool: Pool pool: pg.Pool
) => { return await Postgres.new(pool); }; ) => { return await Postgres.new(pool); };
import { building } from '$app/environment'; import { building } from '$app/environment';

View File

@ -2,8 +2,15 @@
import type { Postgres } from '$lib/server/database'; import type { Postgres } from '$lib/server/database';
import fs from 'fs'; import fs from 'fs';
import { compile } from 'mdsvex'; import { compile } from 'mdsvex';
import { execSync } from 'child_process';
export default async function init(db: Postgres) { 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 = [ const schemas = [
{ {
name: 'article', name: 'article',
@ -107,7 +114,7 @@ export default async function init(db: Postgres) {
for (const { path, id } of articleFiles) { for (const { path, id } of articleFiles) {
const res = await db.query('select * from article where id = $1', [id]); const res = await db.query('select * from article where id = $1', [id]);
const compiled = await compile(fs.readFileSync(path, 'utf-8')); const compiled = await compile(fs.readFileSync(path, 'utf-8'));
const title = compiled.data.fm.title; const title = compiled.data.fm.title;
const category = path.split('/')[3]; const category = path.split('/')[3];
const tags: string[] = compiled.data.fm.tags; const tags: string[] = compiled.data.fm.tags;

View File

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