aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/api/v1/index.ts
blob: 31ab24f1a18644ee5915de3965aefe7d85420fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
};