add route for glance widget
This commit is contained in:
66
main.go
66
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -176,6 +177,71 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /glance", func(w http.ResponseWriter, r *http.Request) {
|
||||
type glanceTorrent struct {
|
||||
Title string
|
||||
}
|
||||
|
||||
type glanceItem struct {
|
||||
Query string
|
||||
Link string
|
||||
Torrents []glanceTorrent
|
||||
}
|
||||
|
||||
glanceItems := []glanceItem{}
|
||||
|
||||
items, err := m.GetItems()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
baseUrl := r.URL.Query().Get("base_url")
|
||||
|
||||
for _, item := range items {
|
||||
gItem := glanceItem{
|
||||
Query: item.Query,
|
||||
Link: fmt.Sprintf("%s?item=%d", baseUrl, item.ID),
|
||||
Torrents: []glanceTorrent{},
|
||||
}
|
||||
torrents, err := m.GetItemTorrents(item.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, torrent := range torrents {
|
||||
gItem.Torrents = append(gItem.Torrents, glanceTorrent{
|
||||
Title: torrent.Title,
|
||||
})
|
||||
}
|
||||
|
||||
glanceItems = append(glanceItems, gItem)
|
||||
}
|
||||
|
||||
tmpl := `
|
||||
<ul class="list list-gap-2">
|
||||
{{range .Items}}
|
||||
<li class="size-h5">[{{len .Torrents}}] <a href="{{.Link}}" target="_blank">{{.Query}}</a></li>
|
||||
{{end}}
|
||||
</ul>
|
||||
`
|
||||
|
||||
t, err := template.New("glance").Parse(tmpl)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Widget-Title", "tvqueue")
|
||||
w.Header().Set("Widget-Content-Type", "html")
|
||||
|
||||
t.ExecuteTemplate(w, "glance", struct {
|
||||
Items []glanceItem
|
||||
}{
|
||||
Items: glanceItems,
|
||||
})
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /api/items", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Query string
|
||||
|
||||
Reference in New Issue
Block a user