aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-06-21 18:05:55 +0200
committerHampusM <hampus@hampusmat.com>2021-06-21 18:05:55 +0200
commite59d40c2b662a570f679c133a7fe26d1b47edd89 (patch)
tree23203ba5d09e85ba28375e1ccaf2ca43eef1877f
parentae984a23f62d5d10128ff3287e11bd0edc13ceb3 (diff)
Split the v1 api into two files
-rw-r--r--packages/server/src/api/v1.ts154
-rw-r--r--packages/server/src/api/v1/index.ts53
-rw-r--r--packages/server/src/api/v1/repo.ts108
3 files changed, 161 insertions, 154 deletions
diff --git a/packages/server/src/api/v1.ts b/packages/server/src/api/v1.ts
deleted file mode 100644
index 5b0e7b9..0000000
--- a/packages/server/src/api/v1.ts
+++ /dev/null
@@ -1,154 +0,0 @@
-import { FastifyInstance, FastifyPluginOptions } from "fastify";
-import { verifySHA, 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.setErrorHandler((err, req, reply) => {
- 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((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 && repo_verification.code) {
- 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 verifySHA(git, params.repo, params.commit);
- if(commit_verification.success === false && commit_verification.code) {
- reply.code(commit_verification.code).send({ error: 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) {
- reply.code(404).send({ error: "Path not found" });
- }
-
- reply.send({ data: tree });
- }
- });
-
- fastify_repo.route({
- method: "GET",
- url: "/branches",
- handler: async(req, reply) => {
- const params: any = req.params;
- const branches = await git.getBranches(params.repo);
-
- reply.send({ data: branches });
- }
- });
-
- fastify_repo.route({
- method: "GET",
- url: "/branches/:branch",
- handler: async(req, reply) => {
- const params: any = req.params;
- const branch_verification = await verifySHA(git, params.repo, params.branch);
- if(branch_verification.success === false && branch_verification.code) {
- reply.code(branch_verification.code).send({ error: branch_verification.message });
- }
-
- const branch = await git.getBranch(params.repo, params.branch);
-
- if(!branch) {
- reply.code(404).send({ error: "Branch not found!" });
- return;
- }
-
- reply.send({ data: branch });
- }
- });
-
- fastify_repo.route({
- method: "GET",
- url: "/tags",
- handler: async(req, reply) => {
- const params: any = req.params;
- const refs = await git.getTags(params.repo);
- reply.send({ data: refs });
- }
- });
-
- done_repo();
- }, { prefix: "/repos/:repo" });
-
- done();
-}; \ No newline at end of file
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
diff --git a/packages/server/src/api/v1/repo.ts b/packages/server/src/api/v1/repo.ts
new file mode 100644
index 0000000..6ef2923
--- /dev/null
+++ b/packages/server/src/api/v1/repo.ts
@@ -0,0 +1,108 @@
+import { FastifyInstance, FastifyPluginOptions } from "fastify";
+import { verifySHA, verifyRepoName } from "../util";
+
+export default function (fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) {
+ const git = opts.config.git;
+
+ fastify.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 && repo_verification.code) {
+ reply.code(repo_verification.code).send({ error: repo_verification.message });
+ }
+ });
+
+ fastify.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.route({
+ method: "GET",
+ url: "/log/:commit",
+ handler: async(req, reply) => {
+ const params: any = req.params;
+ const commit_verification = await verifySHA(git, params.repo, params.commit);
+ if(commit_verification.success === false && commit_verification.code) {
+ reply.code(commit_verification.code).send({ error: commit_verification.message });
+ }
+
+ const commit = await git.getCommit(params.repo, params.commit);
+
+ reply.send({ data: commit });
+ }
+ });
+
+ fastify.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) {
+ reply.code(404).send({ error: "Path not found" });
+ }
+
+ reply.send({ data: tree });
+ }
+ });
+
+ fastify.route({
+ method: "GET",
+ url: "/branches",
+ handler: async(req, reply) => {
+ const params: any = req.params;
+ const branches = await git.getBranches(params.repo);
+
+ reply.send({ data: branches });
+ }
+ });
+
+ fastify.route({
+ method: "GET",
+ url: "/branches/:branch",
+ handler: async(req, reply) => {
+ const params: any = req.params;
+ const branch_verification = await verifySHA(git, params.repo, params.branch);
+ if(branch_verification.success === false && branch_verification.code) {
+ reply.code(branch_verification.code).send({ error: branch_verification.message });
+ }
+
+ const branch = await git.getBranch(params.repo, params.branch);
+
+ if(!branch) {
+ reply.code(404).send({ error: "Branch not found!" });
+ return;
+ }
+
+ reply.send({ data: branch });
+ }
+ });
+
+ fastify.route({
+ method: "GET",
+ url: "/tags",
+ handler: async(req, reply) => {
+ const params: any = req.params;
+ const refs = await git.getTags(params.repo);
+ reply.send({ data: refs });
+ }
+ });
+
+ done();
+} \ No newline at end of file