cmd/golink: print link info when "+" suffix present

append "+" to the end of a golink to get information about the link
rather than being redirected.  For now, this just pretty prints the raw
JSON.  We could make it prettier later.

Change-Id: If25c9f61c326f6e12cdec89b78eb3ca1b741a0ef
This commit is contained in:
Will Norris
2022-06-10 13:53:36 -07:00
parent 41c8dbe5b1
commit 243f7ad377
+18
View File
@@ -166,6 +166,12 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.RequestURI, "/"), "/")
var serveInfo bool
if strings.HasSuffix(short, "+") {
serveInfo = true
short = strings.TrimSuffix(short, "+")
}
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
serveHome(w, short)
@@ -177,6 +183,17 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
return
}
if serveInfo {
j, err := json.MarshalIndent(link, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
return
}
stats.mu.Lock()
if stats.clicks == nil {
stats.clicks = make(map[string]int)
@@ -312,6 +329,7 @@ func serveExport(w http.ResponseWriter, r *http.Request) {
return
}
sort.Strings(names)
encoder := json.NewEncoder(w)
for _, name := range names {
link, err := db.Load(name)