diff options
author | HampusM <hampus@hampusmat.com> | 2021-08-12 15:37:30 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-08-12 15:37:30 +0200 |
commit | d41b27f43335cfb1a8ff49aeb121f992332429cf (patch) | |
tree | 93b6e1ec9b292c3d80ee1a538d0e729612148052 /packages | |
parent | 30f0b2ee1f494be1786280040fb47ec18bde8a6d (diff) |
Cleaned up the settings implementation & renamed base_dir project-wide to git_dir
Diffstat (limited to 'packages')
-rw-r--r-- | packages/client/package.json | 3 | ||||
-rw-r--r-- | packages/client/vue.config.js | 5 | ||||
-rw-r--r-- | packages/server/package.json | 1 | ||||
-rw-r--r-- | packages/server/src/app.ts | 17 | ||||
-rw-r--r-- | packages/server/src/git/http.ts | 2 | ||||
-rw-r--r-- | packages/server/src/git/misc.ts | 6 | ||||
-rw-r--r-- | packages/server/src/git/repository.ts | 22 | ||||
-rw-r--r-- | packages/server/src/routes/api/v1/index.ts | 4 | ||||
-rw-r--r-- | packages/server/src/routes/api/v1/repo/index.ts | 2 | ||||
-rw-r--r-- | packages/server/src/routes/repo.ts | 6 | ||||
-rw-r--r-- | packages/server/src/server.ts | 42 | ||||
-rw-r--r-- | packages/server/src/types/index.d.ts | 7 |
12 files changed, 55 insertions, 62 deletions
diff --git a/packages/client/package.json b/packages/client/package.json index 54068d6..f1797e3 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -13,7 +13,6 @@ "core-js": "^3.6.5", "date-fns": "^2.22.1", "highlight.js": "^11.0.1", - "js-yaml": "^4.1.0", "marked": "^2.1.3", "vue": "^3.0.0", "vue-loading-overlay": "^4.0.3", @@ -32,6 +31,7 @@ "@vue/compiler-sfc": "^3.0.0", "@vue/eslint-config-standard": "^6.0.0", "@vue/eslint-config-typescript": "^7.0.0", + "api": "^1.0.0", "eslint": "^7.31.0", "eslint-config-base": "^1.0.0", "eslint-plugin-import": "^2.23.4", @@ -40,7 +40,6 @@ "eslint-plugin-vue": "^7.14.0", "sass": "^1.26.5", "sass-loader": "^12.1.0", - "api": "^1.0.0", "typescript": "^4.3.5", "vue-eslint-parser": "^7.9.0", "webpack": "^5.46.0" diff --git a/packages/client/vue.config.js b/packages/client/vue.config.js index cf7622e..f19863a 100644 --- a/packages/client/vue.config.js +++ b/packages/client/vue.config.js @@ -1,13 +1,12 @@ -const yaml = require("js-yaml"); const fs = require("fs"); const path = require("path"); -const settings = yaml.load(fs.readFileSync(path.join(__dirname, "/../../settings.yml"), "utf8")); +const settings = JSON.parse(fs.readFileSync(path.join(__dirname, "/../../settings.json"), "utf-8")); module.exports = { devServer: { host: settings.host, - port: settings.dev_port, + port: settings.dev.port, proxy: { "^/api": { target: `http://${settings.host}:${settings.port}`, diff --git a/packages/server/package.json b/packages/server/package.json index 8ffb19b..6d213dd 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -12,7 +12,6 @@ "date-fns": "^2.22.1", "fastify": "^3.17.0", "fastify-static": "^4.2.2", - "js-yaml": "^4.1.0", "nodegit": "^0.27.0", "openpgp": "^5.0.0-5", "tar-stream": "^2.2.0", 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<T>(arr: T[], callback: (t: T) => Promise<boolean /** * Returns the content of a file inside a repository * - * @param base_dir - The directory which the repository is in + * @param git_dir - The directory which the repository is in * @param repository - The directory of a bare repository * @param file - The path of a file */ -export function getFile(base_dir: string, repository: string, file: string): Promise<string> { +export function getFile(git_dir: string, repository: string, file: string): Promise<string> { 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<string> { - 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<string> { - 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<Repository> { - 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<Repository> { + 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<Repository[]> { - const dir_content = await getDirectory(base_dir); + public static async openAll(git_dir: string): Promise<Repository[]> { + 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 |