From 80447de8300fed51924a09cac3abc45ca4731d17 Mon Sep 17 00:00:00 2001 From: la-ninpre Date: Sat, 2 Dec 2023 12:59:43 +0300 Subject: add documentation and change http bad request to not found --- main.go | 23 ++++++++++++++++------- 1 file 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 = ` aaoth.xyz go - +

redirecting to repository...

@@ -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)) } -- cgit v1.2.3