aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/api/v1.ts
blob: e6391a03e7af5d2576f71d4ba6302b7f83aea17f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { FastifyInstance, FastifyPluginOptions } from "fastify";
import { verifyCommitID, verifyRepoName } from "./util";
import { GitAPI } from "./git";
/* eslint-disable max-lines-per-function */

export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) {
	const git = new GitAPI(opts.config.settings.base_dir);

	fastify.route({
		method: "GET",
		url: "/info",
		handler: (req, reply) => {
			reply.send({ data: opts.config.settings });
		}
	});
	fastify.route({
		method: "GET",
		url: "/repos",
		handler: async(req, reply) => {
			const repos = await git.getRepositories();

			if(!repos) {
				reply.code(500).send({ error: "Internal server error!" });
				return;
			}

			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) {
				reply.code(repo_verification.code).send(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((fastify_repo, opts_repo, done_repo) => {
		fastify_repo.addHook("onRequest", 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) {
				reply.code(repo_verification.code).send({ error: repo_verification.message });
			}
		});

		fastify_repo.route({
			method: "GET",
			url: "/log",
			handler: async(req, reply) => {
				const log = await git.getLog((<any>req.params).repo);

				if(log.length === 0) {
					reply.code(500).send({ error: "Internal server error!" });
					return;
				}

				reply.send({ data: log });
			}
		});

		fastify_repo.route({
			method: "GET",
			url: "/log/:commit",
			handler: async(req, reply) => {
				const params: any = req.params;
				const commit_verification = await verifyCommitID(git, params.repo, params.commit);
				if(commit_verification.success === false) {
					reply.code(commit_verification.code).send(commit_verification.message);
				}

				const commit = await git.getCommit(params.repo, params.commit);

				reply.send({ data: commit });
			}
		});

		fastify_repo.route({
			method: "GET",
			url: "/tree",
			handler: async(req, reply) => {
				const params: any = req.params;
				const query: any = req.query;

				const tree_path = (query.length !== 0 && query.path) ? query.path : null;

				const tree = await git.getTree(params.repo, tree_path);

				if(tree.error) {
					if(tree.error === 404) {
						reply.code(404).send({ error: "Path not found" });
					}
					else {
						reply.code(500).send({ error: "Internal server error" });
					}
				}
				reply.send({ data: tree });
			}
		});

		done_repo();
	}, { prefix: "/repos/:repo" });

	done();
};