2024-10-30 05:38:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hacklab.nilfm.cc/quartzgun/renderer"
|
|
|
|
"hacklab.nilfm.cc/quartzgun/router"
|
|
|
|
"hacklab.nilfm.cc/quartzgun/util"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"html/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func withTitleAndStreamsAndSentry(next http.Handler, cfg Config, sentry *Sentry) http.Handler {
|
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
util.AddContextValue(req, "title", cfg.Name)
|
|
|
|
util.AddContextValue(req, "streams", cfg.Streams)
|
|
|
|
util.AddContextValue(req, "sentry", sentry)
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
|
|
|
|
2024-10-30 17:33:58 +00:00
|
|
|
type Sentry struct {
|
|
|
|
}
|
2024-10-30 05:38:05 +00:00
|
|
|
|
|
|
|
func (self *Sentry) GetStatus(url string) int {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return 500
|
|
|
|
} else {
|
2024-10-30 17:33:58 +00:00
|
|
|
resp.Body.Close()
|
2024-10-30 05:38:05 +00:00
|
|
|
return resp.StatusCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cfg := ReadConfig()
|
|
|
|
|
|
|
|
templateRoot := filepath.Join(cfg.DataDir, "templates")
|
|
|
|
|
|
|
|
rtr := &router.Router{
|
|
|
|
StaticPaths: map[string]string{
|
|
|
|
"/static/": filepath.Join(cfg.DataDir, "static"),
|
|
|
|
},
|
|
|
|
Fallback: *template.Must(template.ParseFiles(
|
|
|
|
filepath.Join(templateRoot, "err.html"),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rtr.Get("/", withTitleAndStreamsAndSentry(
|
|
|
|
renderer.Template(
|
|
|
|
filepath.Join(templateRoot, "radio.html"),
|
|
|
|
),
|
|
|
|
*cfg,
|
|
|
|
&Sentry{},
|
|
|
|
))
|
|
|
|
|
|
|
|
http.ListenAndServe(cfg.ListenAddress, rtr)
|
|
|
|
}
|