commit 1aa0c2b
uint
·
2026-02-01 01:16:58 +0000 UTC
parent ca263e9
gorados: parse /meta/{id} and display data
1 files changed,
+66,
-12
+66,
-12
1@@ -6,12 +6,13 @@ import (
2 "fmt"
3 "net/http"
4 "os"
5+ "strings"
6 "time"
7 )
8
9 // library item
10 type item struct {
11- ID string `json:"id"`
12+ ID string `json:"id"`
13 Path string `json:"path"`
14 }
15
16@@ -21,6 +22,18 @@ type library_resp struct {
17 Items []item `json:"items"`
18 }
19
20+// /meta/{id} response
21+type meta_resp struct {
22+ Proto int `json:"proto"`
23+ ID string `json:"id"`
24+ Path string `json:"path"`
25+ Name string `json:"name"`
26+ Size uint64 `json:"size"`
27+ Mtime int64 `json:"mtime"`
28+ Type string `json:"type"`
29+ Kind string `json:"kind"`
30+}
31+
32 // app state
33 type handler struct {
34 parados string
35@@ -29,30 +42,34 @@ type handler struct {
36 httpc* http.Client
37 }
38
39-func (h* handler) library(w http.ResponseWriter, r *http.Request) {
40- // build upstream req
41- req, _ := http.NewRequest("GET", h.parados+"/library", nil)
42+func (h* handler) get_json(url string, out any) (int, error) {
43+ req, _ := http.NewRequest("GET", url, nil)
44 if h.user != "" {
45 req.SetBasicAuth(h.user, h.pass)
46 }
47
48- // call upstream
49 resp, err := h.httpc.Do(req)
50 if err != nil {
51- http.Error(w, "Upstream Error", 502)
52- return
53+ return 0, err
54 }
55 defer resp.Body.Close()
56
57 if resp.StatusCode != 200 {
58- http.Error(w, resp.Status, resp.StatusCode)
59- return
60+ return resp.StatusCode, nil
61 }
62
63- // decode json
64+ return 200, json.NewDecoder(resp.Body).Decode(out)
65+}
66+
67+func (h* handler) library(w http.ResponseWriter, r *http.Request) {
68 var lr library_resp
69- if err := json.NewDecoder(resp.Body).Decode(&lr); err != nil {
70- http.Error(w, "Bad JSON", 502)
71+ code, err := h.get_json(h.parados+"/library", &lr)
72+ if err != nil {
73+ http.Error(w, "Upstream Error", 502)
74+ return
75+ }
76+ if code != 200 {
77+ http.Error(w, http.StatusText(code), code)
78 return
79 }
80 if lr.Proto != 1 {
81@@ -70,6 +87,42 @@ func (h* handler) library(w http.ResponseWriter, r *http.Request) {
82 fmt.Fprintf(w, "</ul>")
83 }
84
85+func (h* handler) item(w http.ResponseWriter, r *http.Request) {
86+ id := strings.TrimPrefix(r.URL.Path, "/item/")
87+ if id == "" {
88+ http.NotFound(w, r)
89+ return
90+ }
91+
92+ var mr meta_resp
93+ code, err := h.get_json(h.parados+"/meta/"+id, &mr)
94+ if err != nil {
95+ http.Error(w, "Upstream Error", 502)
96+ return
97+ }
98+ if code != 200 {
99+ http.Error(w, http.StatusText(code), code)
100+ return
101+ }
102+ if mr.Proto != 1 {
103+ http.Error(w, "Unknown Proto Version", 502)
104+ return
105+ }
106+
107+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
108+ fmt.Fprintf(w, "<!doctype html><meta charset=utf-8><title>%s</title>", mr.Name)
109+ fmt.Fprintf(w, "<h1>%s</h1>", mr.Name)
110+ fmt.Fprintf(w, "<p><code>%s</code></p>", mr.Path)
111+ fmt.Fprintf(w, "<ul>")
112+ fmt.Fprintf(w, "<li>id: <code>%s</code></li>", mr.ID)
113+ fmt.Fprintf(w, "<li>type: <code>%s</code></li>", mr.Type)
114+ fmt.Fprintf(w, "<li>kind: <code>%s</code></li>", mr.Kind)
115+ fmt.Fprintf(w, "<li>size: %d</li>", mr.Size)
116+ fmt.Fprintf(w, "<li>mtime: %d</li>", mr.Mtime)
117+ fmt.Fprintf(w, "</ul>")
118+ fmt.Fprintf(w, `<p><a href="/library">back</a> | <a href="/stream/%s">stream</a></p>`, mr.ID) // TODO
119+}
120+
121 func main() {
122 listen := flag.String("listen", "127.0.0.1:8808", "Listen Addr")
123 par := flag.String("parados", "http://127.0.0.1:8088", "Parados Base URL")
124@@ -91,6 +144,7 @@ func main() {
125 http.Redirect(w, r, "/library", http.StatusSeeOther)
126 })
127 mux.HandleFunc("/library", h.library)
128+ mux.HandleFunc("/item/", h.item)
129
130 // start server
131 fmt.Fprintf(os.Stderr, "gorados: listening on %s\n", *listen)