all repos — goget @ 7732a61b7775ba7448e1c69e0d577577f1e228f2

returns a go-import meta tag

Add readme, better logging, etc.
Anirudh Oppiliappan x@icyphox.sh
Fri, 02 Dec 2022 17:33:11 +0530
commit

7732a61b7775ba7448e1c69e0d577577f1e228f2

parent

ccd27eaeb455cda3fee124357ab331eb2776e250

3 files changed, 41 insertions(+), 11 deletions(-)

jump to
M go.modgo.mod

@@ -1,3 +1,3 @@

-module git.icyphox.sh/goget +module icyphox.sh/goget go 1.19
M main.gomain.go

@@ -12,6 +12,7 @@

type config struct { prettyurl string giturl string + addr string } func logger(h http.Handler) http.Handler {

@@ -20,19 +21,24 @@ h.ServeHTTP(w, r)

uri := r.URL.String() method := r.Method - log.Printf("%s: %s", method, uri) + ua := r.UserAgent() + log.Printf("%s: %s -- %s", method, uri, ua) }) } func (c *config) goget(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/html") - pretty := path.Join(c.prettyurl, r.URL.Path) - content := path.Join(c.giturl, r.URL.Path) - fmt.Fprintf( - w, `<head><meta name="go-import" content="%s git https://%s"></head>`, - pretty, content, - ) + if r.URL.Query().Get("go-get") == "1" { + pretty := path.Join(c.prettyurl, r.URL.Path) + content := path.Join(c.giturl, r.URL.Path) + fmt.Fprintf( + w, `<head><meta name="go-import" content="%s git https://%s"></head>`, + pretty, content, + ) + } else { + http.Redirect(w, r, "https://"+c.giturl, http.StatusTemporaryRedirect) + } return }

@@ -40,15 +46,16 @@ func main() {

cfg := config{} flag.StringVar(&cfg.prettyurl, "pretty-url", "", "pretty url for your go module") flag.StringVar(&cfg.giturl, "git-url", "", "actual git url of your go module") + flag.StringVar(&cfg.addr, "addr", "0.0.0.0:6868", "listen address") flag.Parse() - if flag.NFlag() != 2 { + if cfg.giturl == "" || cfg.prettyurl == "" { fmt.Println("goget: required options --pretty-url and --git-url") os.Exit(1) } mux := http.NewServeMux() - log.Println("starting server on 0.0.0.0:6868") + log.Printf("starting server on %s", cfg.addr) mux.Handle("/", logger(http.HandlerFunc(cfg.goget))) - log.Fatal(http.ListenAndServe("0.0.0.0:6868", mux)) + log.Fatal(http.ListenAndServe(cfg.addr, mux)) }
A readme

@@ -0,0 +1,23 @@

+goget +----- + +go-import meta tag server. Serves the below meta tag if the URL query contains +?go-get=1; else, redirects to the specified git URL (assumes https://). + + <head><meta name="go-import" content="example.com git https://git.example.com"></head> + + +USAGE + + Usage of ./goget: + -addr string + listen address (default "0.0.0.0:6868") + -git-url string + actual git url of your go module + -pretty-url string + pretty url for your go module + + +NOTES + +Run behind a TLS terminating reverse proxy like relayd(8) or nginx.