all repos — goget @ 6e3f009983098a9f5d199dbcd16d047a9f60934a

returns a go-import meta tag

Add logging and pretty URL support
Anirudh Oppiliappan x@icyphox.sh
Wed, 30 Nov 2022 09:09:22 +0530
commit

6e3f009983098a9f5d199dbcd16d047a9f60934a

parent

048d28ac4d6a163e48102a83cae453cd260d2e56

1 files changed, 32 insertions(+), 4 deletions(-)

jump to
M main.gomain.go

@@ -1,24 +1,52 @@

package main import ( + "flag" "fmt" "log" "net/http" + "os" "path" ) -func goget( w http.ResponseWriter, r *http.Request) { +type config struct { + prettyurl string +} + +func logger(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + + uri := r.URL.String() + method := r.Method + log.Printf("%s: %s", method, uri) + }) +} + +func (c *config) goget(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/html") - p := path.Join(r.Host, r.URL.Path) + + pretty := path.Join(c.prettyurl, r.URL.Path) + content := path.Join(r.Host, r.URL.Path) fmt.Fprintf( w, `<head><meta name="go-import" content="%s git https://%s"></head>`, - p, p, + pretty, content, ) return } func main() { + cfg := config{} + flag.StringVar(&cfg.prettyurl, "pretty-url", "", "pretty url for your go module") + flag.Parse() + + if cfg.prettyurl == "" { + fmt.Println("goget: required flag --pretty-url") + os.Exit(1) + } + mux := http.NewServeMux() - mux.HandleFunc("/", goget) + log.Println("starting server on 0.0.0.0:6868") + mux.Handle("/", logger(http.HandlerFunc(cfg.goget))) log.Fatal(http.ListenAndServe("0.0.0.0:6868", mux)) }