aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorla-ninpre <aaoth@aaoth.xyz>2023-12-02 12:59:43 +0300
committerla-ninpre <aaoth@aaoth.xyz>2023-12-02 12:59:43 +0300
commit80447de8300fed51924a09cac3abc45ca4731d17 (patch)
treef5bba628e1082df54f5fc4c3d3618779c7db448b /main.go
parent8cb0ced36a8a9bdcfbda9586f8b871613731de34 (diff)
downloadgomod-index-main.tar.gz
gomod-index-main.zip
add documentation and change http bad request to not foundHEADmain
Diffstat (limited to 'main.go')
-rw-r--r--main.go23
1 files changed, 16 insertions, 7 deletions
diff --git a/main.go b/main.go
index 780f9e4..7340d72 100644
--- a/main.go
+++ b/main.go
@@ -1,3 +1,11 @@
+// gomod-index is a simple http redirector for go modules.
+// it listens on a specified address and port and handles go get
+// requests to a git repo. it is designed to be run behind a tls-terminating
+// reverse-proxy.
+//
+// usage:
+//
+// gomod-index [-l listen_addr] [-m module_root] [-g git_repo_root]
package main
import (
@@ -21,7 +29,7 @@ const indexTmpl = `<!DOCTYPE html>
<title>aaoth.xyz go</title>
<meta charset="utf-8">
<meta name="go-import" content="{{.Mod}} git {{.Repo}}">
-<meta http-equiv="refresh" content="0; url={{.Repo}}">
+<meta http-equiv="refresh" content="5; url={{.Repo}}">
</head>
<body>
<p>redirecting to <a href="{{.Repo}}">repository</a>...</p>
@@ -36,32 +44,32 @@ type Data struct {
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
+ // XXX: do nested modules work?
err := req.ParseForm()
if err != nil {
log.Println("parse form:", err)
- http.Error(w, "bad request", http.StatusBadRequest)
+ http.Error(w, "not found", http.StatusNotFound)
return
}
if goGet, ok := req.Form["go-get"]; !ok || len(goGet) < 1 || goGet[0] != "1" {
- http.Error(w, "bad request", http.StatusBadRequest)
+ http.Error(w, "not found", http.StatusNotFound)
return
}
path := strings.Trim(req.URL.EscapedPath(), "/")
parts := strings.Split(path, "/")
if len(parts) < 2 {
- log.Println("requested", req.RequestURI, "got", parts)
- http.Error(w, "bad request", http.StatusBadRequest)
+ http.Error(w, "not found", http.StatusNotFound)
return
}
mod, err := url.JoinPath(*mFlag, parts[0], parts[1])
if err != nil {
- log.Println("url join:", err)
+ log.Println("ERROR: url join:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
repo, err := url.JoinPath(*gFlag, parts[0], parts[1])
if err != nil {
- log.Println("url join:", err)
+ log.Println("ERROR: url join:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
@@ -80,5 +88,6 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
+ log.Println("listening on", *lFlag)
log.Fatal(http.ListenAndServe(*lFlag, mux))
}