package web import ( "fmt" "html/template" "net/http" "time" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "go.xsfx.dev/logginghandler" assets "go.xsfx.dev/schnutibox/assets/web" "go.xsfx.dev/schnutibox/internal/config" "go.xsfx.dev/schnutibox/pkg/sselog" ) func root(w http.ResponseWriter, r *http.Request) { logger := logginghandler.Logger(r) t, err := template.ParseFS(assets.Templates, "templates/index.html.tmpl") if err != nil { logger.Error().Err(err).Msg("could not parse template") http.Error(w, "could not parse template", http.StatusInternalServerError) return } t.Execute(w, struct{}{}) } func Run(command *cobra.Command, args []string) { // Create host string for serving web. l := fmt.Sprintf("%s:%d", config.Cfg.Box.Hostname, config.Cfg.Box.Port) // Define http handlers. http.Handle("/", logginghandler.Handler(http.HandlerFunc(root))) http.Handle("/log", logginghandler.Handler(http.HandlerFunc(sselog.LogHandler))) http.Handle( "/static/", logginghandler.Handler( http.StripPrefix("/static/", http.FileServer(http.FS(assets.Files))), ), ) http.Handle("/metrics", promhttp.Handler()) go func() { ticker := time.NewTicker(5 * time.Second) for { <-ticker.C log.Debug().Msg("ping") } }() // Serving this thing. log.Info().Msgf("serving on %s...", l) log.Fatal().Err(http.ListenAndServe(l, nil)).Msg("goodbye") }