From b103cef1693e50ae1fbb8768e830baa731941110 Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Mon, 1 May 2023 13:26:49 -0400 Subject: [PATCH] Add git server --- .gitignore | 1 + web/.env.example | 3 +- web/package-lock.json | 22 +++++++++++++++ web/package.json | 2 ++ web/src/hooks.server.ts | 3 ++ web/src/lib/server/gitserver.ts | 49 +++++++++++++++++++++++++++++++++ web/svelte.config.js | 7 +---- 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 web/src/lib/server/gitserver.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffdcec4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +repo \ No newline at end of file diff --git a/web/.env.example b/web/.env.example index 2ecfaf3..a3e8454 100644 --- a/web/.env.example +++ b/web/.env.example @@ -1 +1,2 @@ -DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public" \ No newline at end of file +DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public" +GIT_REPO_DIR="./repo" \ No newline at end of file diff --git a/web/package-lock.json b/web/package-lock.json index b3fd4eb..97b075e 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -14,6 +14,7 @@ "diff": "^5.1.0", "diff2html": "^3.4.35", "highlight.js": "^11.8.0", + "node-git-server": "^1.0.0", "prisma": "^4.13.0", "uuid": "^9.0.0", "zod": "^3.21.4" @@ -23,6 +24,7 @@ "@sveltejs/kit": "^1.15.9", "@types/bootstrap": "^5.2.6", "@types/diff": "^5.0.3", + "@types/node": "^18.16.3", "@types/uuid": "^9.0.1", "@typescript-eslint/eslint-plugin": "^5.59.1", "@typescript-eslint/parser": "^5.59.1", @@ -811,6 +813,12 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, + "node_modules/@types/node": { + "version": "18.16.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", + "devOptional": true + }, "node_modules/@types/pug": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.6.tgz", @@ -2338,6 +2346,15 @@ "integrity": "sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==", "dev": true }, + "node_modules/node-git-server": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-git-server/-/node-git-server-1.0.0.tgz", + "integrity": "sha512-LdAEExF4N6/RIuAdJe16l1U5OZrw5M1U7B4C4O2PiRVImyaPNTFeTHlj46vGgpjthvrK7z43JKcIPc4r6+j0fA==", + "hasInstallScript": true, + "dependencies": { + "through": "^2.3.8" + } + }, "node_modules/nopt": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz", @@ -3000,6 +3017,11 @@ "integrity": "sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==", "dev": true }, + "node_modules/through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==" + }, "node_modules/tiny-glob": { "version": "0.2.9", "resolved": "https://registry.npmjs.org/tiny-glob/-/tiny-glob-0.2.9.tgz", diff --git a/web/package.json b/web/package.json index 0b964ee..36a6a11 100644 --- a/web/package.json +++ b/web/package.json @@ -16,6 +16,7 @@ "@sveltejs/kit": "^1.15.9", "@types/bootstrap": "^5.2.6", "@types/diff": "^5.0.3", + "@types/node": "^18.16.3", "@types/uuid": "^9.0.1", "@typescript-eslint/eslint-plugin": "^5.59.1", "@typescript-eslint/parser": "^5.59.1", @@ -38,6 +39,7 @@ "diff": "^5.1.0", "diff2html": "^3.4.35", "highlight.js": "^11.8.0", + "node-git-server": "^1.0.0", "prisma": "^4.13.0", "uuid": "^9.0.0", "zod": "^3.21.4" diff --git a/web/src/hooks.server.ts b/web/src/hooks.server.ts index fa10523..832fb1a 100644 --- a/web/src/hooks.server.ts +++ b/web/src/hooks.server.ts @@ -1,6 +1,9 @@ import { redirect, type Handle } from '@sveltejs/kit'; import { db } from '$lib/server/prisma'; import type { Session } from '@prisma/client'; +import { startGitServer } from '$lib/server/gitserver'; + +startGitServer(); const sessionExpireMilliseconds = 1000 * 60 * 60 * 24; // 24 hours diff --git a/web/src/lib/server/gitserver.ts b/web/src/lib/server/gitserver.ts new file mode 100644 index 0000000..0bb7761 --- /dev/null +++ b/web/src/lib/server/gitserver.ts @@ -0,0 +1,49 @@ +import { error } from 'console'; +import { Git } from 'node-git-server'; +import { join } from 'path'; + +let gitRunning = false; + +export function startGitServer() { + if (!gitRunning) { + const port = + !process.env.GIT_PORT || isNaN(parseInt(process.env.GIT_PORT)) + ? 7006 + : parseInt(process.env.GIT_PORT); + + if (!process.env.GIT_REPO_DIR) { + throw error('GIT_REPO_DIR not specified in .env'); + } + const repoDir = process.env.GIT_REPO_DIR; + + const repos = new Git(join(repoDir), { + autoCreate: false, + authenticate: ({ type, user, repo }, next) => { + if (type == 'push') { + console.log(repo); + user((username, password) => { + console.log(username, password); + next(); + }); + } else { + next(); + } + } + }); + + repos.on('push', (push) => { + console.log(`push ${push.repo}/${push.commit} ( ${push.branch} )`); + push.accept(); + }); + + repos.on('fetch', (fetch) => { + console.log(`fetch ${fetch.commit}`); + fetch.accept(); + }); + + repos.listen(port, { type: 'http' }, () => { + console.log(`node-git-server running at http://localhost:${port}`); + gitRunning = true; + }); + } +} diff --git a/web/svelte.config.js b/web/svelte.config.js index 2214c60..a2c7b5c 100644 --- a/web/svelte.config.js +++ b/web/svelte.config.js @@ -3,15 +3,10 @@ import { vitePreprocess } from '@sveltejs/kit/vite'; /** @type {import('@sveltejs/kit').Config} */ const config = { - // Consult https://kit.svelte.dev/docs/integrations#preprocessors - // for more information about preprocessors preprocess: vitePreprocess(), kit: { - // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list. - // If your environment is not supported or you settled on a specific environment, switch out the adapter. - // See https://kit.svelte.dev/docs/adapters for more information about adapters. - adapter: adapter() + adapter: adapter(), } };