all repos — grimoire @ cf3715068c81e9a3a8b6ebd4a6a3dc298f819011

dead simple icecast frontend

grimoire.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)
}

type Sentry struct {}

func (self *Sentry) GetStatus(url string) int {
  resp, err := http.Get(url)
  if err != nil {
    return 500
  } else {
    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)
}