forked from petersalomonsen/wasm-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithttpserver.js
51 lines (42 loc) · 1.61 KB
/
githttpserver.js
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
import http from 'http';
import fs from 'fs';
import cgi from 'cgi';
import { tmpdir } from 'os';
import { execSync } from 'child_process';
/**
* This example will create a git http server to repositories on your local disk.
* Set the GIT_PROJECT_ROOT environment variable to point to location of your git repositories.
*/
export function startServer() {
const testrepodir = `${tmpdir()}/testrepo.git`;
fs.rmSync(testrepodir, {recursive: true, force: true});
execSync(`git init --initial-branch=master --bare ${tmpdir()}/testrepo.git`);
fs.rmSync(`${tmpdir()}/testremote.git`, {recursive: true, force: true});
execSync(`git init --initial-branch=master --bare ${tmpdir()}/testremote.git`);
const script = 'git';
const gitcgi = cgi(script, {args: ['http-backend'],
stderr: process.stderr,
env: {
'GIT_PROJECT_ROOT': tmpdir(),
'GIT_HTTP_EXPORT_ALL': '1',
'REMOTE_USER': '[email protected]' // Push requires authenticated users by default
}
});
return http.createServer( (request, response) => {
let path = request.url.substring(1);
console.log('git http server request', request.url);
if (path.indexOf('ping') > -1) {
response.statusCode = 200;
response.end('pong');
} else if( path.indexOf('git-upload') > -1 ||
path.indexOf('git-receive') > -1) {
gitcgi(request, response);
} else {
response.statusCode = 404;
response.end('not found');
}
}).listen(8080);
}
export default {
startServer: startServer
}