magnetico/cmd/magneticow/handlers.go

121 lines
2.9 KiB
Go
Raw Normal View History

2018-07-12 09:58:39 +02:00
package main
import (
"net/http"
"strings"
"time"
"github.com/pkg/errors"
2018-07-12 09:58:39 +02:00
"github.com/boramalper/magnetico/pkg/persistence"
)
// DONE
func rootHandler(w http.ResponseWriter, r *http.Request) {
nTorrents, err := database.GetNumberOfTorrents()
if err != nil {
2018-12-24 19:30:07 +01:00
handlerError(errors.Wrap(err, "GetNumberOfTorrents"), w)
return
2018-07-12 09:58:39 +02:00
}
2018-12-24 19:30:07 +01:00
_ = templates["homepage"].Execute(w, struct {
2018-07-12 09:58:39 +02:00
NTorrents uint
}{
NTorrents: nTorrents,
})
}
func torrentsHandler(w http.ResponseWriter, r *http.Request) {
data := mustAsset("templates/torrents.html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400")
2018-12-24 19:30:07 +01:00
_, _ = w.Write(data)
2018-07-12 09:58:39 +02:00
}
func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
data := mustAsset("templates/torrent.html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400")
_, _ = w.Write(data)
return
2018-07-12 09:58:39 +02:00
}
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
data := mustAsset("templates/statistics.html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400")
2018-12-24 19:30:07 +01:00
_, _ = w.Write(data)
2018-07-12 09:58:39 +02:00
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
var query, title string
switch len(r.URL.Query()["query"]) {
case 0:
query = ""
case 1:
query = r.URL.Query()["query"][0]
default:
respondError(w, 400, "query supplied multiple times!")
return
}
if query == "" {
title = "Most recent torrents - magneticow"
} else {
title = "`" + query + "` - magneticow"
}
torrents, err := database.QueryTorrents(
query,
time.Now().Unix(),
persistence.ByDiscoveredOn,
false,
20,
2018-07-12 09:58:39 +02:00
nil,
nil,
)
if err != nil {
2018-12-24 19:30:07 +01:00
handlerError(errors.Wrap(err, "query torrent"), w)
2018-07-12 09:58:39 +02:00
return
}
// It is much more convenient to write the XML deceleration manually*, and then process the XML
2018-12-24 19:30:07 +01:00
// template using template/html and send, than to use encoding/xml.
2018-07-12 09:58:39 +02:00
//
// *: https://github.com/golang/go/issues/3133
//
// TODO: maybe do it properly, even if it's inconvenient?
2018-12-24 19:30:07 +01:00
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>`))
_ = templates["feed"].Execute(w, struct {
2018-07-12 09:58:39 +02:00
Title string
Torrents []persistence.TorrentMetadata
}{
Title: title,
2018-07-12 09:58:39 +02:00
Torrents: torrents,
})
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
data, err := Asset(r.URL.Path[1:])
if err != nil {
http.NotFound(w, r)
return
}
var contentType string
if strings.HasSuffix(r.URL.Path, ".css") {
contentType = "text/css; charset=utf-8"
} else if strings.HasSuffix(r.URL.Path, ".js") {
contentType = "text/javascript; charset=utf-8"
2018-07-12 09:58:39 +02:00
} else { // fallback option
contentType = http.DetectContentType(data)
}
w.Header().Set("Content-Type", contentType)
// Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400")
2018-12-24 19:30:07 +01:00
_, _ = w.Write(data)
2018-07-12 09:58:39 +02:00
}