Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Last active September 6, 2022 20:06
Show Gist options
  • Save scotthaleen/25aa3c44c12706714afb68507632ef9d to your computer and use it in GitHub Desktop.
Save scotthaleen/25aa3c44c12706714afb68507632ef9d to your computer and use it in GitHub Desktop.
Simple no auth git-http-backend handler in go - linux
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/cgi"
"net/http/httputil"
)
//modified from https://github.com/pasela/git-cgi-server
var (
git_http_backend_bin = "/usr/lib/git-core/git-http-backend"
git_repos = "/etc/git/repos"
)
func gitBackend(w http.ResponseWriter, r *http.Request) {
env := []string{
fmt.Sprintf("GIT_PROJECT_ROOT=%s", git_repos),
"GIT_HTTP_EXPORT_ALL=",
"REMOTE_USER=githttp",
}
// debug git request
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(requestDump))
var stdErr bytes.Buffer
handler := &cgi.Handler{
Path: git_http_backend_bin,
Root: "/repos",
Env: env,
Stderr: &stdErr,
}
handler.ServeHTTP(w, r)
if stdErr.Len() > 0 {
log.Println("[backend]", stdErr.String())
}
}
func Run() {
listen := ":3000"
mux := http.NewServeMux()
mux.HandleFunc("/repos/", gitBackend)
log.Printf("Listening %s\n", listen)
httpServer := &http.Server{
Addr: listen,
Handler: mux,
}
log.Fatal(httpServer.ListenAndServe())
}
func main() {
Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment