Skip to content

Instantly share code, notes, and snippets.

@stereosteve
Last active April 15, 2021 15:32
Show Gist options
  • Save stereosteve/540e098f3009bde0ae31d3f48a306139 to your computer and use it in GitHub Desktop.
Save stereosteve/540e098f3009bde0ae31d3f48a306139 to your computer and use it in GitHub Desktop.
import {
serve,
ServerRequest,
} from "https://deno.land/[email protected]/http/server.ts";
const GIT_PROJECT_ROOT = "repoz";
const encoder = new TextEncoder();
async function gitCgi(request: ServerRequest) {
let [path, query] = request.url.split("?");
query = query || "";
const contentType = request.headers.get("Content-Type") || "";
const git = Deno.run({
cmd: ["git", "http-backend", "--stateless-rpc", "--enable=receive-pack"],
env: {
GIT_HTTP_EXPORT_ALL: "true",
GIT_PROJECT_ROOT,
PATH_INFO: path,
REQUEST_METHOD: request.method,
QUERY_STRING: query,
REMOTE_USER: "steve",
CONTENT_TYPE: contentType,
},
stdin: "piped",
stdout: "piped",
});
let wrote = 0;
wrote = await Deno.copy(request.body, git.stdin);
console.log("wrote to stdin", wrote);
// https://github.com/denoland/deno/issues/7727#issuecomment-699698890
// took me a while to sort that out
git.stdin.close();
wrote = await request.w.write(encoder.encode("HTTP/1.1 200 OK\r\n"));
console.log("wrote header", wrote);
wrote = await Deno.copy(git.stdout, request.w);
console.log("copied from stdout", wrote);
// send output to response
await request.w.flush();
console.log("flushed");
// status
const status = await git.status();
console.log("status", status);
git.close();
request.conn.close();
}
const port = 4500;
const server = serve({ hostname: "0.0.0.0", port: 4500 });
console.log(
`HTTP webserver running. Access it at: http://localhost:${port}/`
);
for await (const request of server) {
await gitCgi(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment