package view import ( "arimelody-web/controller" "arimelody-web/model" "arimelody-web/templates" "fmt" "net/http" "os" ) func IndexHandler(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodHead { w.WriteHeader(http.StatusOK) return } type IndexData struct { TwitchStatus *model.TwitchStreamInfo } var err error var twitchStatus *model.TwitchStreamInfo = nil if app.Twitch != nil && len(app.Config.Twitch.Broadcaster) > 0 { twitchStatus, err = controller.GetTwitchStatus(app, app.Config.Twitch.Broadcaster) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to get Twitch status for %s: %v\n", app.Config.Twitch.Broadcaster, err) } } if r.URL.Path == "/" || r.URL.Path == "/index.html" { err := templates.IndexTemplate.Execute(w, IndexData{ TwitchStatus: twitchStatus, }) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to render index page: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } return } StaticHandler("public").ServeHTTP(w, r) }) }