summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRagnis Armus <ragnis@armus.ee>2018-07-12 22:54:41 +0300
committerRagnis Armus <ragnis@armus.ee>2018-07-12 22:54:41 +0300
commit8389d48005858c4e6af2e90373e0c18d84330456 (patch)
treef3e57be70b0c24c091caf17ecde25fced974767c /main.go
first commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..f09ccce
--- /dev/null
+++ b/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+
+ "radr.ee/cgit-go-import/cgitrc"
+)
+
+type importRepo struct {
+ prefix string
+ cloneURL string
+}
+
+var (
+ handlers = make(map[string]http.Handler)
+)
+
+func serveStatus(w http.ResponseWriter, status int) {
+ w.WriteHeader(status)
+ w.Write([]byte(http.StatusText(status)))
+}
+
+func handleRequest(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "HEAD" {
+ return
+ }
+ if r.Method != "GET" || r.URL.RawQuery != "go-import=1" {
+ serveStatus(w, 404)
+ return
+ }
+ handler, ok := handlers[r.URL.Path]
+ if ok {
+ handler.ServeHTTP(w, r)
+ } else {
+ serveStatus(w, 404)
+ }
+}
+
+func createRepoHandler(cfg *cgitrc.Config, r *cgitrc.Repo) http.Handler {
+ body := []byte(fmt.Sprintf(
+ "<head><meta name=\"go-import\" content=\"radr.ee/%s git %s\"></head>",
+ r.URL,
+ cfg.ResolveRepoCloneURL(r),
+ ))
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html")
+ w.Write(body)
+ })
+}
+
+func main() {
+ cfg, err := cgitrc.Open("/etc/cgitrc")
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, r := range cfg.Repos {
+ handlers["/"+r.URL] = createRepoHandler(cfg, r)
+ }
+
+ http.ListenAndServe(":8080", http.HandlerFunc(handleRequest))
+}