aboutsummaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-25 12:32:59 +0200
committerHampusM <hampus@hampusmat.com>2021-07-25 12:32:59 +0200
commitc63e558f402cfad914031a58fdcf3d8e0f3d125d (patch)
treebf080e4c23310f5a5a1d14f15bc3e575c0671625 /packages
parenta5afb39803e70a6117965760f50615aaba82f84a (diff)
Moved backend routes to a dedicated directory
Diffstat (limited to 'packages')
-rw-r--r--packages/server/src/app.ts92
-rw-r--r--packages/server/src/routes/api/util.ts (renamed from packages/server/src/api/util.ts)4
-rw-r--r--packages/server/src/routes/api/v1/index.ts (renamed from packages/server/src/api/v1/index.ts)6
-rw-r--r--packages/server/src/routes/api/v1/repo/branches.ts (renamed from packages/server/src/api/v1/repo/branches.ts)4
-rw-r--r--packages/server/src/routes/api/v1/repo/index.ts (renamed from packages/server/src/api/v1/repo/index.ts)14
-rw-r--r--packages/server/src/routes/api/v1/repo/log.ts (renamed from packages/server/src/api/v1/repo/log.ts)6
-rw-r--r--packages/server/src/routes/repo.ts84
7 files changed, 104 insertions, 106 deletions
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<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<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<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/api/util.ts b/packages/server/src/routes/api/util.ts
index d467fed..2a06393 100644
--- a/packages/server/src/api/util.ts
+++ b/packages/server/src/routes/api/util.ts
@@ -1,5 +1,5 @@
-import { Commit } from "../git/commit";
-import { Repository } from "../git/repository";
+import { Commit } from "../../git/commit";
+import { Repository } from "../../git/repository";
type VerificationResultType = "SUCCESS" | "NOT_FOUND" | "INVALID";
diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/routes/api/v1/index.ts
index e2e2104..d956063 100644
--- a/packages/server/src/api/v1/index.ts
+++ b/packages/server/src/routes/api/v1/index.ts
@@ -1,10 +1,10 @@
import { FastifyInstance, FastifyPluginOptions } from "fastify";
-import { Repository } from "../../git/repository";
-import { Route } from "../../types/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";
+import { BaseError } from "../../../git/error";
function setHandlers(fastify: FastifyInstance): void {
fastify.setErrorHandler((err, req, reply) => {
diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/routes/api/v1/repo/branches.ts
index 4aa6665..10ac736 100644
--- a/packages/server/src/api/v1/repo/branches.ts
+++ b/packages/server/src/routes/api/v1/repo/branches.ts
@@ -1,6 +1,6 @@
import { FastifyInstance, FastifyPluginOptions } from "fastify";
-import { Branch } from "../../../git/branch";
-import { Route } from "../../../types/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 {
diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts
index 1fe08e9..0042b60 100644
--- a/packages/server/src/api/v1/repo/index.ts
+++ b/packages/server/src/routes/api/v1/repo/index.ts
@@ -1,16 +1,16 @@
-import { CoolFastifyRequest, Route } from "../../../types/fastify";
+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 { 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";
+import { BaseError } from "../../../../git/error";
+import { Tree } from "../../../../git/tree";
declare module "fastify" {
interface FastifyRequest {
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/routes/api/v1/repo/log.ts
index 24937ad..5ba4044 100644
--- a/packages/server/src/api/v1/repo/log.ts
+++ b/packages/server/src/routes/api/v1/repo/log.ts
@@ -1,7 +1,7 @@
import { FastifyInstance, FastifyPluginOptions } from "fastify";
-import { Commit } from "../../../git/commit";
-import { Patch } from "../../../git/patch";
-import { Route } from "../../../types/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";
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<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<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<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