diff options
author | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
commit | d1a1b7dc947063aef5f8375a6a1e03246b272c84 (patch) | |
tree | f5cb9bd6d4b5463d9d022026ac6fea87cb6ebe02 /packages/server/src/server.ts | |
parent | 6ed078de30a7bf35deace728857d1d293d59eb15 (diff) |
Implemented caching for certain API endpoints, Added documentation & made backend-fixes
Diffstat (limited to 'packages/server/src/server.ts')
-rw-r--r-- | packages/server/src/server.ts | 70 |
1 files changed, 42 insertions, 28 deletions
diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 11b3f7f..3056bf5 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -1,41 +1,55 @@ -import { readFileSync, readdirSync } from "fs"; +import { readFile, readdir } from "fs/promises"; import { join } from "path"; import { exit } from "process"; import { Settings } from "./types"; import buildApp from "./app"; +import { ServerCache } from "./cache"; -const settings = JSON.parse(readFileSync(join(__dirname, "/../../../settings.json"), "utf-8")) as Settings; +async function main() { + const settings = JSON.parse(await readFile(join(__dirname, "/../../../settings.json"), { encoding: "utf-8" })) as Settings; -const settings_keys = Object.keys(settings); + const settings_keys = Object.keys(settings); -const mandatory_settings = [ "host", "port", "title", "about", "git_dir" ]; + const mandatory_settings = [ "host", "port", "title", "about", "git_dir" ]; -// Get missing mandatory settings -const settings_not_included = mandatory_settings.filter(x => !settings_keys.includes(x)); + // Get missing mandatory settings + const settings_not_included = mandatory_settings.filter(x => !settings_keys.includes(x)); -// Error out and exit if there's any missing settings -if(settings_not_included.length !== 0) { - console.log(`Error: settings file is missing ${(settings_not_included.length > 1) ? "keys" : "key"}:`); - console.log(settings_not_included.join(", ")); - exit(1); -} - -// Make sure that the git directory specified in the settings actually exists -try { - readdirSync(settings.git_dir); -} -catch { - console.error(`Error: Git directory ${settings.git_dir} doesn't exist!`); - exit(1); -} - -const app = buildApp(settings); + // Error out and exit if there's any missing settings + if(settings_not_included.length !== 0) { + console.log(`Error: settings file is missing ${(settings_not_included.length > 1) ? "keys" : "key"}:`); + console.log(settings_not_included.join(", ")); + exit(1); + } -app.listen(settings.port, settings.host, (err: Error, addr: string) => { - if(err) { - console.error(err); + // Make sure that the git directory specified in the settings actually exists + await readdir(settings.git_dir).catch(() => { + console.error(`Error: Git directory ${settings.git_dir} doesn't exist!`); exit(1); + }); + + const cache = (settings.cache && settings.cache.enabled === true) || settings.cache === undefined || settings.cache.enabled === undefined + ? new ServerCache(settings.cache) + : null; + + if(cache) { + await cache.init(settings.git_dir); + if(!cache.ready) { + console.error("Error: cache failed to initialize!"); + return 1; + } } - console.log(`Githermit is running on ${addr}`); -});
\ No newline at end of file + const app = await buildApp(settings, cache); + + app.listen(settings.port, settings.host, (err: Error, addr: string) => { + if(err) { + console.error(err); + exit(1); + } + + console.log(`Githermit is running on ${addr}`); + }); +} + +main();
\ No newline at end of file |