all repos — gomod-index @ 80447de8300fed51924a09cac3abc45ca4731d17

simple go get handler

add documentation and change http bad request to not found
la-ninpre aaoth@aaoth.xyz
Sat, 02 Dec 2023 12:59:43 +0300
commit

80447de8300fed51924a09cac3abc45ca4731d17

parent

8cb0ced36a8a9bdcfbda9586f8b871613731de34

1 files changed, 16 insertions(+), 7 deletions(-)

jump to
M main.gomain.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 @@ <head>

<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 @@ Repo string

} 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 @@ flag.Parse()

mux := http.NewServeMux() mux.HandleFunc("/", indexHandler) + log.Println("listening on", *lFlag) log.Fatal(http.ListenAndServe(*lFlag, mux)) }