From c63e558f402cfad914031a58fdcf3d8e0f3d125d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 25 Jul 2021 12:32:59 +0200 Subject: Moved backend routes to a dedicated directory --- packages/server/src/api/util.ts | 42 -------- packages/server/src/api/v1/index.ts | 91 ---------------- packages/server/src/api/v1/repo/branches.ts | 48 --------- packages/server/src/api/v1/repo/index.ts | 114 --------------------- packages/server/src/api/v1/repo/log.ts | 81 --------------- packages/server/src/app.ts | 92 +---------------- packages/server/src/routes/api/util.ts | 42 ++++++++ packages/server/src/routes/api/v1/index.ts | 91 ++++++++++++++++ packages/server/src/routes/api/v1/repo/branches.ts | 48 +++++++++ packages/server/src/routes/api/v1/repo/index.ts | 114 +++++++++++++++++++++ packages/server/src/routes/api/v1/repo/log.ts | 81 +++++++++++++++ packages/server/src/routes/repo.ts | 84 +++++++++++++++ 12 files changed, 463 insertions(+), 465 deletions(-) delete mode 100644 packages/server/src/api/util.ts delete mode 100644 packages/server/src/api/v1/index.ts delete mode 100644 packages/server/src/api/v1/repo/branches.ts delete mode 100644 packages/server/src/api/v1/repo/index.ts delete mode 100644 packages/server/src/api/v1/repo/log.ts create mode 100644 packages/server/src/routes/api/util.ts create mode 100644 packages/server/src/routes/api/v1/index.ts create mode 100644 packages/server/src/routes/api/v1/repo/branches.ts create mode 100644 packages/server/src/routes/api/v1/repo/index.ts create mode 100644 packages/server/src/routes/api/v1/repo/log.ts create mode 100644 packages/server/src/routes/repo.ts (limited to 'packages') diff --git a/packages/server/src/api/util.ts b/packages/server/src/api/util.ts deleted file mode 100644 index d467fed..0000000 --- a/packages/server/src/api/util.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Commit } from "../git/commit"; -import { Repository } from "../git/repository"; - -type VerificationResultType = "SUCCESS" | "NOT_FOUND" | "INVALID"; - -export class VerificationResult { - constructor(result: VerificationResultType, subject?: string) { - this.success = result === "SUCCESS"; - - if(result !== "SUCCESS") { - const verification_error_types = { - NOT_FOUND: { code: 404, message: `${String(subject?.substr(0, 1).toUpperCase()) + subject?.substr(1)} not found!` }, - INVALID: { code: 403, message: `Invalid ${subject}` } - }; - - this.message = verification_error_types[result].message; - this.code = verification_error_types[result].code; - } - } - - success: boolean; - code: number | null = null; - message: string | null = null; -} - -export function verifyRepoName(repo_name: string): boolean { - return /^[a-zA-Z0-9.\-_]+$/u.test(repo_name); -} - -export async function verifySHA(repository: Repository, sha: string): Promise { - if(!(/^[a-fA-F0-9]+$/u).test(sha)) { - return new VerificationResult("INVALID", "sha"); - } - - const object_exists = await Commit.lookupExists(repository, sha); - - if(!object_exists) { - return new VerificationResult("NOT_FOUND", "object"); - } - - return new VerificationResult("SUCCESS"); -} \ No newline at end of file diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts deleted file mode 100644 index e2e2104..0000000 --- a/packages/server/src/api/v1/index.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { Repository } from "../../git/repository"; -import { Route } from "../../types/fastify"; -import repo from "./repo"; -import { verifyRepoName } from "../util"; -import { Info as APIInfo, RepositorySummary as APIRepositorySummary, Repository as APIRepository } from "shared_types"; -import { BaseError } from "../../git/error"; - -function setHandlers(fastify: FastifyInstance): void { - 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!" }); - }); -} - -function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - fastify.route({ - method: "GET", - url: "/repos", - handler: async(req, reply) => { - const repos = await Repository.openAll(opts.config.settings.base_dir); - - if(!repos) { - reply.send({ data: [] }); - return; - } - - reply.send({ - data: await Promise.all(repos.map(async repository => { - return { - name: repository.name.short, - description: repository.description, - last_updated: (await repository.masterCommit()).date - }; - })) - }); - } - }); - - fastify.route({ - method: "GET", - url: "/repos/:repo", - handler: async(req, reply) => { - if(!verifyRepoName(req.params.repo)) { - reply.code(400).send({ error: "Bad request" }); - return; - } - - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch(err => err); - - if(repository instanceof BaseError) { - reply.code(repository.code).send({ error: repository.message }); - return; - } - - const data: APIRepository = { - name: repository.name.short, - description: repository.description, - has_readme: await (await repository.tree()).findExists("README.md") - }; - - reply.send({ data: data }); - } - }); - done(); -} - -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - setHandlers(fastify); - - fastify.route({ - method: "GET", - url: "/info", - handler: (req, reply) => { - const data: APIInfo = { - title: opts.config.settings.title, - about: opts.config.settings.about - }; - - reply.send({ data: data }); - } - }); - - fastify.register(reposEndpoints, { config: { settings: opts.config.settings } }); - fastify.register(repo, { prefix: "/repos/:repo", config: { settings: opts.config.settings } }); - - done(); -} \ No newline at end of file diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/api/v1/repo/branches.ts deleted file mode 100644 index 4aa6665..0000000 --- a/packages/server/src/api/v1/repo/branches.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { Branch } from "../../../git/branch"; -import { Route } from "../../../types/fastify"; -import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "shared_types"; - -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - fastify.route({ - method: "GET", - url: "/branches", - handler: async(req, reply) => { - const branches = await req.repository.branches(); - - reply.send({ - data: branches.map(branch => { - return { - id: branch.id, - name: branch.name - }; - }) - }); - } - }); - - fastify.route({ - method: "GET", - url: "/branches/:branch", - handler: async(req, reply) => { - const branch = await Branch.lookup(req.repository, req.params.branch); - - if(!branch) { - reply.code(404).send({ error: "Branch not found!" }); - return; - } - - const data: APIBranch = { - id: branch.id, - name: branch.name, - latest_commit: await branch.latestCommit() - }; - - reply.send({ - data: data - }); - } - }); - - done(); -} \ No newline at end of file diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts deleted file mode 100644 index 1fe08e9..0000000 --- a/packages/server/src/api/v1/repo/index.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { CoolFastifyRequest, Route } from "../../../types/fastify"; -import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { Blob } from "../../../git/blob"; -import { Repository } from "../../../git/repository"; -import { Tag } from "../../../git/tag"; -import { TreeEntry } from "../../../git/tree_entry"; -import { basename } from "path"; -import branches from "./branches"; -import log from "./log"; -import { verifyRepoName } from "../../util"; -import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "shared_types"; -import { BaseError } from "../../../git/error"; -import { Tree } from "../../../git/tree"; - -declare module "fastify" { - interface FastifyRequest { - repository: Repository, - } -} - -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); - - if(repository instanceof BaseError) { - reply.code(repository.code).send({ error: repository.message }); - return; - } - - req.repository = repository; - }); - - fastify.addHook("onRequest", async(req: CoolFastifyRequest, reply) => { - if(!verifyRepoName(req.params.repo)) { - reply.code(400).send({ error: "Bad request" }); - } - }); -} -async function treeEntryMap(entry: TreeEntry) { - const latest_commit = await entry.latestCommit(); - return { - name: basename(entry.path), - type: entry.type, - latest_commit: { - id: latest_commit.id, - message: latest_commit.message, - date: latest_commit.date - } - }; -} - -async function tagMap(tag: Tag) { - const author = await tag.author(); - return { - name: tag.name, - author: { name: author.name, email: author.email }, - date: await tag.date() - }; -} - -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - addHooks(fastify, opts); - - fastify.register(log); - fastify.register(branches); - - fastify.route({ - method: "GET", - url: "/tree", - handler: async(req, reply) => { - const tree: Tree | BaseError = await req.repository.tree().catch(err => err); - - if(tree instanceof BaseError) { - reply.code(tree.code).send({ error: tree.message }); - return; - } - - const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null; - - let data: APITree; - - if(tree_path) { - const tree_path_entry: Tree | Blob | BaseError = await tree.find(tree_path).catch(err => err); - - if(tree_path_entry instanceof BaseError) { - reply.code(tree_path_entry.code).send({ error: tree_path_entry.message }); - return; - } - - data = tree_path_entry instanceof Blob - ? { type: "blob", content: await tree_path_entry.content() } - : { type: "tree", content: await Promise.all(tree_path_entry.entries().map(treeEntryMap)) }; - } - else { - data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) }; - } - - reply.send({ data: data }); - } - }); - - fastify.route({ - method: "GET", - url: "/tags", - handler: async(req, reply) => { - const tags = await req.repository.tags(); - reply.send({ - data: await Promise.all(tags.map(tagMap)) - }); - } - }); - - done(); -} \ No newline at end of file diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts deleted file mode 100644 index 24937ad..0000000 --- a/packages/server/src/api/v1/repo/log.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { Commit } from "../../../git/commit"; -import { Patch } from "../../../git/patch"; -import { Route } from "../../../types/fastify"; -import { verifySHA } from "../../util"; -import { LogCommit as APILogCommit, Patch as APIPatch, Commit as APICommit } from "shared_types"; - -async function commitMap(commit: Commit) { - const stats = await commit.stats(); - return { - id: commit.id, - author: { - name: commit.author.name, - email: commit.author.email - }, - message: commit.message, - date: commit.date, - insertions: stats.insertions, - deletions: stats.deletions, - files_changed: stats.files_changed - }; -} - -async function patchMap(patch: Patch, index: number) { - return { - additions: patch.additions, - deletions: patch.deletions, - from: patch.from, - to: patch.to, - too_large: await patch.isTooLarge(index), - hunks: await patch.getHunks(index) - }; -} - -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - fastify.route({ - method: "GET", - url: "/log", - handler: async(req, reply) => { - const commits = await req.repository.commits(); - - reply.send({ - data: await Promise.all(commits.map(commitMap)) - }); - } - }); - - fastify.route({ - method: "GET", - url: "/log/:commit", - handler: async(req, reply) => { - const commit_verification = await verifySHA(req.repository, req.params.commit); - if(commit_verification.success === false && commit_verification.code) { - reply.code(commit_verification.code).send({ error: commit_verification.message }); - } - - const commit = await Commit.lookup(req.repository, req.params.commit); - - const stats = await commit.stats(); - - const data: APICommit = { - message: commit.message, - author: { - name: commit.author.name, - email: commit.author.email - }, - date: commit.date, - insertions: stats.insertions, - deletions: stats.deletions, - files_changed: stats.files_changed, - diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap)) - }; - - reply.send({ - data: data - }); - } - }); - - done(); -} \ No newline at end of file diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index deddf00..f52333c 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -1,12 +1,8 @@ -import { Repository } from "./git/repository"; -import { Route } from "./types/fastify"; -import { Tag } from "./git/tag"; -import api from "./api/v1"; +import api from "./routes/api/v1"; import { fastify as fastifyFactory, FastifyInstance } from "fastify"; import fastifyStatic from "fastify-static"; -import { verifyRepoName } from "./api/util"; -import { BaseError } from "./git/error"; import { Settings } from "./types"; +import repo from "./routes/repo"; export default function buildApp(settings: Settings, dist_dir: string): FastifyInstance { const fastify = fastifyFactory(); @@ -36,89 +32,7 @@ export default function buildApp(settings: Settings, dist_dir: string): FastifyI fastify.addContentTypeParser("application/x-git-receive-pack-request", (req, payload, done) => done(null, payload)); fastify.register(api, { prefix: "/api/v1", config: { settings: settings } }); - - fastify.route({ - method: "GET", - url: "/:repo([a-zA-Z0-9\\.\\-_]+)/info/refs", - handler: async(req, reply) => { - reply.header("Content-Type", "application/x-git-upload-pack-advertisement"); - - if(!verifyRepoName(req.params.repo)) { - reply.code(400).send({ error: "Bad request" }); - return; - } - - if(!req.query.service) { - reply.header("Content-Type", "text/plain"); - reply.code(403).send("Missing service query parameter\n"); - return; - } - - if(req.query.service !== "git-upload-pack") { - reply.header("Content-Type", "text/plain"); - reply.code(403).send("Access denied!\n"); - return; - } - - if(Object.keys(req.query).length !== 1) { - reply.code(403).send("Too many query parameters!\n"); - return; - } - - const repository = await Repository.open(settings.base_dir, req.params.repo); - repository.HTTPconnect(req, reply); - } - }); - - fastify.route({ - method: "POST", - url: "/:repo([a-zA-Z0-9\\.\\-_]+)/git-upload-pack", - handler: async(req, reply) => { - if(!verifyRepoName(req.params.repo)) { - reply.code(400).send({ error: "Bad request" }); - return; - } - - const repository = await Repository.open(settings.base_dir, req.params.repo); - repository.HTTPconnect(req, reply); - } - }); - - fastify.route({ - method: "POST", - url: "/:repo([a-zA-Z0-9\\.\\-_]+)/git-receive-pack", - handler: (req, reply) => { - reply.header("Content-Type", "application/x-git-receive-pack-result"); - reply.code(403).send("Access denied!"); - } - }); - - fastify.route({ - method: "GET", - url: "/:repo([a-zA-Z0-9\\.\\-_]+)/refs/tags/:tag", - handler: async(req, reply) => { - if(!verifyRepoName(req.params.repo)) { - reply.code(400).send({ error: "Bad request" }); - return; - } - - const repository: Repository | BaseError = await Repository.open(settings.base_dir, req.params.repo).catch(err => err); - - if(repository instanceof BaseError) { - reply.code(repository.code).send(repository.message); - return; - } - - const tag = await Tag.lookup(repository, req.params.tag).catch(err => err); - - if(tag instanceof BaseError) { - reply.code(tag.code).send(tag.message); - return; - } - - tag.downloadTarball(reply); - } - }); + fastify.register(repo, { prefix: "/:repo([a-zA-Z0-9\\.\\-_]+)", config: { settings: settings } }); return fastify; } \ No newline at end of file diff --git a/packages/server/src/routes/api/util.ts b/packages/server/src/routes/api/util.ts new file mode 100644 index 0000000..2a06393 --- /dev/null +++ b/packages/server/src/routes/api/util.ts @@ -0,0 +1,42 @@ +import { Commit } from "../../git/commit"; +import { Repository } from "../../git/repository"; + +type VerificationResultType = "SUCCESS" | "NOT_FOUND" | "INVALID"; + +export class VerificationResult { + constructor(result: VerificationResultType, subject?: string) { + this.success = result === "SUCCESS"; + + if(result !== "SUCCESS") { + const verification_error_types = { + NOT_FOUND: { code: 404, message: `${String(subject?.substr(0, 1).toUpperCase()) + subject?.substr(1)} not found!` }, + INVALID: { code: 403, message: `Invalid ${subject}` } + }; + + this.message = verification_error_types[result].message; + this.code = verification_error_types[result].code; + } + } + + success: boolean; + code: number | null = null; + message: string | null = null; +} + +export function verifyRepoName(repo_name: string): boolean { + return /^[a-zA-Z0-9.\-_]+$/u.test(repo_name); +} + +export async function verifySHA(repository: Repository, sha: string): Promise { + if(!(/^[a-fA-F0-9]+$/u).test(sha)) { + return new VerificationResult("INVALID", "sha"); + } + + const object_exists = await Commit.lookupExists(repository, sha); + + if(!object_exists) { + return new VerificationResult("NOT_FOUND", "object"); + } + + return new VerificationResult("SUCCESS"); +} \ 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 new file mode 100644 index 0000000..d956063 --- /dev/null +++ b/packages/server/src/routes/api/v1/index.ts @@ -0,0 +1,91 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { Repository } from "../../../git/repository"; +import { Route } from "../../../types/fastify"; +import repo from "./repo"; +import { verifyRepoName } from "../util"; +import { Info as APIInfo, RepositorySummary as APIRepositorySummary, Repository as APIRepository } from "shared_types"; +import { BaseError } from "../../../git/error"; + +function setHandlers(fastify: FastifyInstance): void { + 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!" }); + }); +} + +function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + fastify.route({ + method: "GET", + url: "/repos", + handler: async(req, reply) => { + const repos = await Repository.openAll(opts.config.settings.base_dir); + + if(!repos) { + reply.send({ data: [] }); + return; + } + + reply.send({ + data: await Promise.all(repos.map(async repository => { + return { + name: repository.name.short, + description: repository.description, + last_updated: (await repository.masterCommit()).date + }; + })) + }); + } + }); + + fastify.route({ + method: "GET", + url: "/repos/:repo", + handler: async(req, reply) => { + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); + return; + } + + const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch(err => err); + + if(repository instanceof BaseError) { + reply.code(repository.code).send({ error: repository.message }); + return; + } + + const data: APIRepository = { + name: repository.name.short, + description: repository.description, + has_readme: await (await repository.tree()).findExists("README.md") + }; + + reply.send({ data: data }); + } + }); + done(); +} + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + setHandlers(fastify); + + fastify.route({ + method: "GET", + url: "/info", + handler: (req, reply) => { + const data: APIInfo = { + title: opts.config.settings.title, + about: opts.config.settings.about + }; + + reply.send({ data: data }); + } + }); + + fastify.register(reposEndpoints, { config: { settings: opts.config.settings } }); + fastify.register(repo, { prefix: "/repos/:repo", config: { settings: opts.config.settings } }); + + done(); +} \ No newline at end of file diff --git a/packages/server/src/routes/api/v1/repo/branches.ts b/packages/server/src/routes/api/v1/repo/branches.ts new file mode 100644 index 0000000..10ac736 --- /dev/null +++ b/packages/server/src/routes/api/v1/repo/branches.ts @@ -0,0 +1,48 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { Branch } from "../../../../git/branch"; +import { Route } from "../../../../types/fastify"; +import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "shared_types"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + fastify.route({ + method: "GET", + url: "/branches", + handler: async(req, reply) => { + const branches = await req.repository.branches(); + + reply.send({ + data: branches.map(branch => { + return { + id: branch.id, + name: branch.name + }; + }) + }); + } + }); + + fastify.route({ + method: "GET", + url: "/branches/:branch", + handler: async(req, reply) => { + const branch = await Branch.lookup(req.repository, req.params.branch); + + if(!branch) { + reply.code(404).send({ error: "Branch not found!" }); + return; + } + + const data: APIBranch = { + id: branch.id, + name: branch.name, + latest_commit: await branch.latestCommit() + }; + + reply.send({ + data: data + }); + } + }); + + done(); +} \ No newline at end of file diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts new file mode 100644 index 0000000..0042b60 --- /dev/null +++ b/packages/server/src/routes/api/v1/repo/index.ts @@ -0,0 +1,114 @@ +import { CoolFastifyRequest, Route } from "../../../../types/fastify"; +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { Blob } from "../../../../git/blob"; +import { Repository } from "../../../../git/repository"; +import { Tag } from "../../../../git/tag"; +import { TreeEntry } from "../../../../git/tree_entry"; +import { basename } from "path"; +import branches from "./branches"; +import log from "./log"; +import { verifyRepoName } from "../../util"; +import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "shared_types"; +import { BaseError } from "../../../../git/error"; +import { Tree } from "../../../../git/tree"; + +declare module "fastify" { + interface FastifyRequest { + repository: Repository, + } +} + +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); + + if(repository instanceof BaseError) { + reply.code(repository.code).send({ error: repository.message }); + return; + } + + req.repository = repository; + }); + + fastify.addHook("onRequest", async(req: CoolFastifyRequest, reply) => { + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); + } + }); +} +async function treeEntryMap(entry: TreeEntry) { + const latest_commit = await entry.latestCommit(); + return { + name: basename(entry.path), + type: entry.type, + latest_commit: { + id: latest_commit.id, + message: latest_commit.message, + date: latest_commit.date + } + }; +} + +async function tagMap(tag: Tag) { + const author = await tag.author(); + return { + name: tag.name, + author: { name: author.name, email: author.email }, + date: await tag.date() + }; +} + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + addHooks(fastify, opts); + + fastify.register(log); + fastify.register(branches); + + fastify.route({ + method: "GET", + url: "/tree", + handler: async(req, reply) => { + const tree: Tree | BaseError = await req.repository.tree().catch(err => err); + + if(tree instanceof BaseError) { + reply.code(tree.code).send({ error: tree.message }); + return; + } + + const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null; + + let data: APITree; + + if(tree_path) { + const tree_path_entry: Tree | Blob | BaseError = await tree.find(tree_path).catch(err => err); + + if(tree_path_entry instanceof BaseError) { + reply.code(tree_path_entry.code).send({ error: tree_path_entry.message }); + return; + } + + data = tree_path_entry instanceof Blob + ? { type: "blob", content: await tree_path_entry.content() } + : { type: "tree", content: await Promise.all(tree_path_entry.entries().map(treeEntryMap)) }; + } + else { + data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) }; + } + + reply.send({ data: data }); + } + }); + + fastify.route({ + method: "GET", + url: "/tags", + handler: async(req, reply) => { + const tags = await req.repository.tags(); + reply.send({ + data: await Promise.all(tags.map(tagMap)) + }); + } + }); + + done(); +} \ No newline at end of file diff --git a/packages/server/src/routes/api/v1/repo/log.ts b/packages/server/src/routes/api/v1/repo/log.ts new file mode 100644 index 0000000..5ba4044 --- /dev/null +++ b/packages/server/src/routes/api/v1/repo/log.ts @@ -0,0 +1,81 @@ +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { Commit } from "../../../../git/commit"; +import { Patch } from "../../../../git/patch"; +import { Route } from "../../../../types/fastify"; +import { verifySHA } from "../../util"; +import { LogCommit as APILogCommit, Patch as APIPatch, Commit as APICommit } from "shared_types"; + +async function commitMap(commit: Commit) { + const stats = await commit.stats(); + return { + id: commit.id, + author: { + name: commit.author.name, + email: commit.author.email + }, + message: commit.message, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed + }; +} + +async function patchMap(patch: Patch, index: number) { + return { + additions: patch.additions, + deletions: patch.deletions, + from: patch.from, + to: patch.to, + too_large: await patch.isTooLarge(index), + hunks: await patch.getHunks(index) + }; +} + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + fastify.route({ + method: "GET", + url: "/log", + handler: async(req, reply) => { + const commits = await req.repository.commits(); + + reply.send({ + data: await Promise.all(commits.map(commitMap)) + }); + } + }); + + fastify.route({ + method: "GET", + url: "/log/:commit", + handler: async(req, reply) => { + const commit_verification = await verifySHA(req.repository, req.params.commit); + if(commit_verification.success === false && commit_verification.code) { + reply.code(commit_verification.code).send({ error: commit_verification.message }); + } + + const commit = await Commit.lookup(req.repository, req.params.commit); + + const stats = await commit.stats(); + + const data: APICommit = { + message: commit.message, + author: { + name: commit.author.name, + email: commit.author.email + }, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed, + diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap)) + }; + + reply.send({ + data: data + }); + } + }); + + done(); +} \ No newline at end of file diff --git a/packages/server/src/routes/repo.ts b/packages/server/src/routes/repo.ts new file mode 100644 index 0000000..ce81dcd --- /dev/null +++ b/packages/server/src/routes/repo.ts @@ -0,0 +1,84 @@ +import { Repository } from "../git/repository"; +import { CoolFastifyRequest, Route } from "../types/fastify"; +import { Tag } from "../git/tag"; +import { FastifyInstance, FastifyPluginOptions } from "fastify"; +import { verifyRepoName } from "../routes/api/util"; +import { BaseError } from "../git/error"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + fastify.addHook("onRequest", async(req: CoolFastifyRequest, reply) => { + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send("Bad request"); + } + }); + + fastify.route({ + method: "GET", + url: "/info/refs", + handler: async(req, reply) => { + reply.header("Content-Type", "application/x-git-upload-pack-advertisement"); + + if(!req.query.service) { + reply.header("Content-Type", "text/plain"); + reply.code(403).send("Missing service query parameter\n"); + return; + } + + if(req.query.service !== "git-upload-pack") { + reply.header("Content-Type", "text/plain"); + reply.code(403).send("Access denied!\n"); + return; + } + + if(Object.keys(req.query).length !== 1) { + reply.code(403).send("Too many query parameters!\n"); + return; + } + + const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo); + repository.HTTPconnect(req, reply); + } + }); + + fastify.route({ + method: "POST", + url: "/git-upload-pack", + handler: async(req, reply) => { + const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo); + repository.HTTPconnect(req, reply); + } + }); + + fastify.route({ + method: "POST", + url: "/git-receive-pack", + handler: (req, reply) => { + reply.header("Content-Type", "application/x-git-receive-pack-result"); + reply.code(403).send("Access denied!"); + } + }); + + fastify.route({ + method: "GET", + url: "/refs/tags/:tag", + handler: async(req, reply) => { + const repository: Repository | BaseError = await Repository.open(opts.settings.base_dir, req.params.repo).catch(err => err); + + if(repository instanceof BaseError) { + reply.code(repository.code).send(repository.message); + return; + } + + const tag = await Tag.lookup(repository, req.params.tag).catch(err => err); + + if(tag instanceof BaseError) { + reply.code(tag.code).send(tag.message); + return; + } + + tag.downloadTarball(reply); + } + }); + + done(); +} \ No newline at end of file -- cgit v1.2.3-18-g5258