From d41b27f43335cfb1a8ff49aeb121f992332429cf Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 12 Aug 2021 15:37:30 +0200 Subject: Cleaned up the settings implementation & renamed base_dir project-wide to git_dir --- packages/server/src/app.ts | 17 ++++++++-- packages/server/src/git/http.ts | 2 +- packages/server/src/git/misc.ts | 6 ++-- packages/server/src/git/repository.ts | 22 ++++++------- packages/server/src/routes/api/v1/index.ts | 4 +-- packages/server/src/routes/api/v1/repo/index.ts | 2 +- packages/server/src/routes/repo.ts | 6 ++-- packages/server/src/server.ts | 42 +++++++------------------ packages/server/src/types/index.d.ts | 7 +++-- 9 files changed, 52 insertions(+), 56 deletions(-) (limited to 'packages/server/src') diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index f52333c..9ca3769 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -3,8 +3,11 @@ import { fastify as fastifyFactory, FastifyInstance } from "fastify"; import fastifyStatic from "fastify-static"; import { Settings } from "./types"; import repo from "./routes/repo"; +import { join } from "path"; +import { readdirSync } from "fs"; +import { exit } from "process"; -export default function buildApp(settings: Settings, dist_dir: string): FastifyInstance { +export default function buildApp(settings: Settings): FastifyInstance { const fastify = fastifyFactory(); fastify.setErrorHandler((err, req, reply) => { @@ -16,7 +19,17 @@ export default function buildApp(settings: Settings, dist_dir: string): FastifyI reply.code(404).send("Page not found!"); }); - if(settings.production) { + if(!settings.dev) { + const dist_dir = join(__dirname, "/../../client/dist"); + + try { + readdirSync(dist_dir); + } + catch { + console.error("Error: Client dist directory doesn't exist!"); + exit(1); + } + fastify.register(fastifyStatic, { root: dist_dir }); fastify.route({ diff --git a/packages/server/src/git/http.ts b/packages/server/src/git/http.ts index 183c6df..3e7e25d 100644 --- a/packages/server/src/git/http.ts +++ b/packages/server/src/git/http.ts @@ -50,7 +50,7 @@ export function connect(repository: Repository, req: Request, reply: FastifyRepl reply.raw.writeHead(200, { "Content-Type": content_type }); - const spawn_args = [ "--stateless-rpc", join(repository.base_dir, repository.name.full) ]; + const spawn_args = [ "--stateless-rpc", join(repository.git_dir, repository.name.full) ]; if(is_discovery) { spawn_args.push("--advertise-refs"); diff --git a/packages/server/src/git/misc.ts b/packages/server/src/git/misc.ts index da9b1ee..1fda304 100644 --- a/packages/server/src/git/misc.ts +++ b/packages/server/src/git/misc.ts @@ -17,13 +17,13 @@ export async function findAsync(arr: T[], callback: (t: T) => Promise { +export function getFile(git_dir: string, repository: string, file: string): Promise { return new Promise((resolve, reject) => { - readFile(`${base_dir}/${repository}/${file}`, (err, content) => { + readFile(`${git_dir}/${repository}/${file}`, (err, content) => { if(err) { reject(createError(MiscError, 500, "Failed to open repository file " + file)); return; diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts index 87c6d3a..63ede23 100644 --- a/packages/server/src/git/repository.ts +++ b/packages/server/src/git/repository.ts @@ -37,7 +37,7 @@ export class Repository { public ng_repository: NodeGitRepository; public name: RepositoryName; - public base_dir: string; + public git_dir: string; public branch_name: string; /** @@ -50,7 +50,7 @@ export class Repository { short: basename(repository.path()).slice(0, -4), full: basename(repository.path()) }; - this.base_dir = dirname(repository.path()); + this.git_dir = dirname(repository.path()); this.branch_name = branch; } @@ -59,14 +59,14 @@ export class Repository { * Returns the repository's description */ public description(): Promise { - return getFile(this.base_dir, this.name.full, "description"); + return getFile(this.git_dir, this.name.full, "description"); } /** * Returns the repository's owner */ public owner(): Promise { - return getFile(this.base_dir, this.name.full, "owner"); + return getFile(this.git_dir, this.name.full, "owner"); } /** @@ -154,13 +154,13 @@ export class Repository { /** * Opens a bare git repository * - * @param base_dir - The directory that contains the repository + * @param git_dir - The directory that contains the repository * @param repository - The directory of a bare repository * @param branch - A branch to use * @returns An instance of a git repository */ - public static async open(base_dir: string, repository: string, branch?: string): Promise { - let ng_repository = await NodeGitRepository.openBare(`${base_dir}/${getFullRepositoryName(repository)}`).catch((err: WeirdError) => { + public static async open(git_dir: string, repository: string, branch?: string): Promise { + let ng_repository = await NodeGitRepository.openBare(`${git_dir}/${getFullRepositoryName(repository)}`).catch((err: WeirdError) => { if(err.errno === -3) { throw(createError(RepositoryError, 404, "Repository not found")); } @@ -180,11 +180,11 @@ export class Repository { /** * Opens all of the git repositories inside a directory * - * @param base_dir - The directory that contains the repositories + * @param git_dir - The directory that contains the repositories * @returns An array of repository instances */ - public static async openAll(base_dir: string): Promise { - const dir_content = await getDirectory(base_dir); + public static async openAll(git_dir: string): Promise { + const dir_content = await getDirectory(git_dir); if(dir_content.length === 0) { return []; @@ -192,6 +192,6 @@ export class Repository { const repositories = dir_content.filter(dir_entry => dir_entry.endsWith(".git")); - return Promise.all(repositories.map(repository => this.open(base_dir, repository))); + return Promise.all(repositories.map(repository => this.open(git_dir, repository))); } } \ No newline at end of file diff --git a/packages/server/src/routes/api/v1/index.ts b/packages/server/src/routes/api/v1/index.ts index 9c35d53..1adb3cb 100644 --- a/packages/server/src/routes/api/v1/index.ts +++ b/packages/server/src/routes/api/v1/index.ts @@ -21,7 +21,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "GET", url: "/repos", handler: async(req, reply) => { - const repos = await Repository.openAll(opts.config.settings.base_dir); + const repos = await Repository.openAll(opts.config.settings.git_dir); if(!repos) { reply.send({ data: [] }); @@ -49,7 +49,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do return; } - const repository: Repository | BaseError = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch(err => err); + const repository: Repository | BaseError = await Repository.open(opts.config.settings.git_dir, req.params.repo).catch(err => err); if(repository instanceof BaseError) { reply.code(repository.code).send({ error: repository.message }); diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts index b1b1030..bcd2e5c 100644 --- a/packages/server/src/routes/api/v1/repo/index.ts +++ b/packages/server/src/routes/api/v1/repo/index.ts @@ -19,7 +19,7 @@ declare module "fastify" { function addHooks(fastify: FastifyInstance, opts: FastifyPluginOptions): void { fastify.addHook("preHandler", async(req: CoolFastifyRequest, reply) => { - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo, req.query.branch).catch((err: BaseError) => err); + const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo, req.query.branch).catch((err: BaseError) => err); if(repository instanceof BaseError) { reply.code(repository.code).send({ error: repository.message }); diff --git a/packages/server/src/routes/repo.ts b/packages/server/src/routes/repo.ts index 99c2295..43d89d2 100644 --- a/packages/server/src/routes/repo.ts +++ b/packages/server/src/routes/repo.ts @@ -35,7 +35,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do return; } - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo); + const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo); repository.HTTPconnect(req, reply); } }); @@ -44,7 +44,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "POST", url: "/git-upload-pack", handler: async(req, reply) => { - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo); + const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo); repository.HTTPconnect(req, reply); } }); @@ -62,7 +62,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "GET", url: "/refs/tags/:tag", handler: async(req, reply) => { - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch((err: BaseError) => err); + const repository = await Repository.open(opts.config.settings.git_dir, req.params.repo).catch((err: BaseError) => err); if(repository instanceof BaseError) { reply.code(repository.code).send(repository.message); diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 15f902c..11b3f7f 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -1,53 +1,35 @@ import { readFileSync, readdirSync } from "fs"; import { join } from "path"; -import { load } from "js-yaml"; import { exit } from "process"; import { Settings } from "./types"; import buildApp from "./app"; -const settings = load(readFileSync(join(__dirname, "/../../../settings.yml"), "utf8")) as Settings; +const settings = JSON.parse(readFileSync(join(__dirname, "/../../../settings.json"), "utf-8")) as Settings; + const settings_keys = Object.keys(settings); -const mandatory_settings = [ "host", "port", "dev_port", "title", "about", "base_dir", "production" ]; +const mandatory_settings = [ "host", "port", "title", "about", "git_dir" ]; -// Make sure that all the required settings are present +// 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.yml is missing ${(settings_not_included.length > 1) ? "keys" : "key"}:`); + 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 there's not an excessive amount of settings -const mandatory_not_included = settings_keys.filter(x => !mandatory_settings.includes(x)); -if(mandatory_not_included.length !== 0) { - console.log(`Error: settings.yml includes ${(mandatory_not_included.length > 1) ? "pointless keys" : "a pointless key"}:`); - console.log(mandatory_not_included.join(", ")); - exit(1); -} - -// Make sure that the base directory specified in the settings actually exists +// Make sure that the git directory specified in the settings actually exists try { - readdirSync(settings.base_dir); + readdirSync(settings.git_dir); } catch { - console.error(`Error: Tried opening the base directory. No such directory: ${settings.base_dir}`); + console.error(`Error: Git directory ${settings.git_dir} doesn't exist!`); exit(1); } -const dist_dir = join(__dirname, "/../../client/dist"); - -if(settings.production) { - try { - readdirSync(dist_dir); - } - catch { - console.error("Error: Tried opening the dist directory but it doesn't exist.\nDid you accidentally turn on the production setting?"); - exit(1); - } -} - -const app = buildApp(settings, dist_dir); +const app = buildApp(settings); app.listen(settings.port, settings.host, (err: Error, addr: string) => { if(err) { @@ -55,5 +37,5 @@ app.listen(settings.port, settings.host, (err: Error, addr: string) => { exit(1); } - console.log(`App is running on ${addr}`); + console.log(`Githermit is running on ${addr}`); }); \ No newline at end of file diff --git a/packages/server/src/types/index.d.ts b/packages/server/src/types/index.d.ts index beb5f49..8f592f9 100644 --- a/packages/server/src/types/index.d.ts +++ b/packages/server/src/types/index.d.ts @@ -1,9 +1,10 @@ export type Settings = { host: string, port: number, - dev_port: number, title: string, about: string, - base_dir: string, - production: boolean + git_dir: string, + dev: { + port: number + } } \ No newline at end of file -- cgit v1.2.3-18-g5258