From 243f7ad377f0715b13bcca6f31c7e6862d30bc6e Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 10 Jun 2022 13:53:36 -0700 Subject: [PATCH] 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 --- golink.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/golink.go b/golink.go index 8e533b9..8784945 100644 --- a/golink.go +++ b/golink.go @@ -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)