mirror of
https://github.com/wahyd4/golink.git
synced 2026-08-09 05:05:56 +10:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user