clonegur/backend/clonegur.ts

29 lines
910 B
TypeScript
Raw Permalink Normal View History

2023-06-29 14:31:25 +00:00
import { MongoClient } from "https://deno.land/x/mongo@v0.31.2/mod.ts";
2023-06-30 21:25:21 +00:00
import { APIRouter } from "./routers/APIRouter.ts";
2023-06-29 14:31:25 +00:00
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import AppDatabase from "./AppDatabase.ts";
2023-06-30 21:25:21 +00:00
import { StaticRouter } from "./routers/StaticRouter.ts";
2023-06-29 14:31:25 +00:00
2023-06-30 21:25:21 +00:00
export default async function clonegur(apiUrl: string) {
2023-06-29 14:31:25 +00:00
let salt;
try {
salt = new TextDecoder().decode(await Deno.readFile('keys/salt.txt'));
}
catch {
salt = await bcrypt.genSalt();
2023-06-30 00:37:07 +00:00
await Deno.mkdir('keys', { recursive: true });
2023-06-29 14:31:25 +00:00
await Deno.writeFile('keys/salt.txt', new TextEncoder().encode(salt));
}
const db = await new MongoClient().connect({
db: 'clonegur',
servers: [ { host: '127.0.0.1', port: 27017 } ]
});
2023-06-30 21:25:21 +00:00
return {
static: new StaticRouter(apiUrl),
api: new APIRouter(salt, new AppDatabase(db)),
}
2023-06-29 14:31:25 +00:00
}