From 4462b61a08abf8228862564679192ae99a7cce9b Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 11 Dec 2021 13:46:11 -0600 Subject: [PATCH] add info endpoint --- handler.go | 40 ++++++++++++++++++++++++---------------- info.go | 10 ++++++++++ 2 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 info.go diff --git a/handler.go b/handler.go index ee47913..d12401c 100644 --- a/handler.go +++ b/handler.go @@ -99,27 +99,30 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log_info.Printf("%s %s %s %s", r.Method, r.Host, r.URL.Host, r.URL.String()) // this is very stupid but I didn't want to add a routing library - // dependency for five endpoints + // dependency for five endpoints, since part of my goal is to not depend on + // anything with github.com in the import path. + // $base/$module/@v/list - list versions for a module + if matches := listP.FindStringSubmatch(r.URL.Path); matches != nil { + modpath := matches[1] + h.list(modpath, w, r) + return + } + + // $base/$module/@latest - get latest version number with timestamp if matches := latestP.FindStringSubmatch(r.URL.Path); matches != nil { modpath := matches[1] h.latest(modpath, w, r) return } - if matches := listP.FindStringSubmatch(r.URL.Path); matches != nil { + if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil { modpath := matches[1] - h.list(modpath, w, r) + modversion := matches[2] + h.info(modpath, modversion, w, r) return } - // if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil { - // modpath := matches[1] - // modversion := matches[2] - // h.info(modpath, modversion, w, r) - // return - // } - // if matches := modP.FindStringSubmatch(r.URL.Path); matches != nil { // modpath := matches[1] // modversion := matches[2] @@ -264,10 +267,6 @@ func (h handler) latest(modpath string, w http.ResponseWriter, r *http.Request) } log_info.Printf("all versions: %v", versions) - type Info struct { - Version string - Time time.Time - } last := versions[len(versions)-1] fi, err := h.stat(modpath, last) @@ -275,7 +274,7 @@ func (h handler) latest(modpath string, w http.ResponseWriter, r *http.Request) writeError(w, err) return } - json.NewEncoder(w).Encode(Info{ + json.NewEncoder(w).Encode(moduleInfo{ Version: last, Time: fi.ModTime(), }) @@ -290,12 +289,21 @@ func (h handler) list(modpath string, w http.ResponseWriter, r *http.Request) { } for _, version := range versions { - fmt.Fprint(w, version) + fmt.Fprintln(w, version) } } // info serves the $base/$module/@v/$version.info endpoint func (h handler) info(modpath, modversion string, w http.ResponseWriter, r *http.Request) { + fi, err := h.stat(modpath, modversion) + if err != nil { + writeError(w, err) + return + } + json.NewEncoder(w).Encode(moduleInfo{ + Version: modversion, + Time: fi.ModTime(), + }) } // modfile serves the $base/$module/@v/$version.mod endpoint diff --git a/info.go b/info.go new file mode 100644 index 0000000..52037f7 --- /dev/null +++ b/info.go @@ -0,0 +1,10 @@ +package main + +import ( + "time" +) + +type moduleInfo struct { + Version string + Time time.Time +}