diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-21 18:05:55 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-21 18:05:55 +0200 |
commit | e59d40c2b662a570f679c133a7fe26d1b47edd89 (patch) | |
tree | 23203ba5d09e85ba28375e1ccaf2ca43eef1877f /packages/server/src/api/v1/index.ts | |
parent | ae984a23f62d5d10128ff3287e11bd0edc13ceb3 (diff) |
Split the v1 api into two files
Diffstat (limited to 'packages/server/src/api/v1/index.ts')
-rw-r--r-- | packages/server/src/api/v1/index.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts new file mode 100644 index 0000000..31ab24f --- /dev/null +++ b/packages/server/src/api/v1/index.ts @@ -0,0 +1,53 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { verifyRepoName } from "../util"; +import { GitAPI } from "../git"; +import repo from "./repo"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) { + const git = new GitAPI(opts.config.settings.base_dir); + + fastify.setErrorHandler((err, req, reply) => { + console.log(err); + reply.code(500).send({ error: "Internal server error!" }); + }); + fastify.setNotFoundHandler((req, reply) => { + reply.code(404).send({ error: "Endpoint not found!" }); + }) + + fastify.route({ + method: "GET", + url: "/info", + handler: (req, reply) => { + reply.send({ data: { title: opts.config.settings.title, about: opts.config.settings.about } }); + } + }); + fastify.route({ + method: "GET", + url: "/repos", + handler: async(req, reply) => { + const repos = await git.getRepositories(); + + reply.send({ data: repos }); + } + }); + + fastify.route({ + method: "GET", + url: "/repos/:repo", + handler: async(req, reply) => { + const params: any = req.params; + const repo_verification = await verifyRepoName(opts.config.settings.base_dir, params.repo); + if(repo_verification.success === false && repo_verification.code) { + reply.code(repo_verification.code).send({ error: repo_verification.message }); + } + + const desc = await git.getRepositoryFile(params.repo, "description"); + + reply.send({ data: { name: params.repo, description: desc, has_readme: await git.doesReadmeExist(params.repo) } }); + } + }); + + fastify.register(repo, { prefix: "/repos/:repo", config: { git: git, settings: opts.config.settings } }); + + done(); +};
\ No newline at end of file |