home to your local SPACEGIRL 馃挮
arimelody.space
1package view
2
3import (
4 "arimelody-web/controller"
5 "arimelody-web/model"
6 "arimelody-web/templates"
7 "fmt"
8 "net/http"
9 "os"
10)
11
12func IndexHandler(app *model.AppState) http.Handler {
13 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14 if r.Method == http.MethodHead {
15 w.WriteHeader(http.StatusOK)
16 return
17 }
18
19 if r.URL.Path == "/" || r.URL.Path == "/index.html" {
20 type IndexData struct {
21 TwitchStatus *model.TwitchStreamInfo
22 }
23 var err error
24 var twitchStatus *model.TwitchStreamInfo = nil
25 if app.Twitch != nil && len(app.Config.Twitch.Broadcaster) > 0 {
26 twitchStatus, err = controller.GetTwitchStatus(app, app.Config.Twitch.Broadcaster)
27 if err != nil {
28 fmt.Fprintf(os.Stderr, "WARN: Failed to get Twitch status for %s: %v\n", app.Config.Twitch.Broadcaster, err)
29 }
30 }
31 err = templates.IndexTemplate.Execute(w, IndexData{
32 TwitchStatus: twitchStatus,
33 })
34 if err != nil {
35 fmt.Fprintf(os.Stderr, "WARN: Failed to render index page: %v\n", err)
36 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
37 }
38 return
39 }
40
41 http.FileServer(http.Dir("./public")).ServeHTTP(w, r)
42 //ServeEmbedFS(app.PublicFS, "public").ServeHTTP(w, r)
43 })
44}