From 283734935f3431216bf80bb95ffe38f5fb436f99 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 10 Feb 2024 21:36:46 +0100 Subject: [PATCH] add /timers route that shows all timers --- cmd/nourybot/router.go | 45 ++++++++++++++++++++++++++++++++ web/templates/timers.page.gohtml | 20 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 web/templates/timers.page.gohtml diff --git a/cmd/nourybot/router.go b/cmd/nourybot/router.go index 26923a1..df98af3 100644 --- a/cmd/nourybot/router.go +++ b/cmd/nourybot/router.go @@ -19,15 +19,60 @@ func (app *application) startRouter() { router.GET("/status", app.statusPageRoute) router.GET("/commands/:channel", app.channelCommandsRoute) router.GET("/commands", app.commandsRoute) + router.GET("/timer", app.timersRoute) router.GET("/timer/:channel", app.channelTimersRoute) // Serve files uploaded by the meme command, but don't list the directory contents. fs := justFilesFilesystem{http.Dir("/public/uploads/")} router.Handler("GET", "/uploads/*filepath", http.StripPrefix("/uploads", http.FileServer(fs))) + app.Log.Info("Serving on :8080") app.Log.Fatal(http.ListenAndServe(":8080", router)) } +type timersRouteData struct { + Timers []data.Timer +} + +func (app *application) timersRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + t, err := template.ParseFiles("./web/templates/timers.page.gohtml") + if err != nil { + app.Log.Error(err) + return + } + var ts []data.Timer + + timerData, err := app.Models.Timers.GetAll() + if err != nil { + app.Log.Errorw("Error trying to retrieve all timer for a channel from database", err) + return + } + // The slice of timers is only used to log them at + // the start so it looks a bit nicer. + + // Iterate over all timers and then add them onto the scheduler. + for i, v := range timerData { + // idk why this works but it does so no touchy touchy. + // https://github.com/robfig/cron/issues/420#issuecomment-940949195 + i, v := i, v + _ = i + var t data.Timer + t.Name = v.Name + t.Text = v.Text + t.Repeat = v.Repeat + + // Add new value to the slice + ts = append(ts, t) + } + + data := &timersRouteData{ts} + err = t.Execute(w, data) + if err != nil { + app.Log.Error(err) + return + } +} + type commandsRouteData struct { Commands map[string]command } diff --git a/web/templates/timers.page.gohtml b/web/templates/timers.page.gohtml new file mode 100644 index 0000000..b547ac9 --- /dev/null +++ b/web/templates/timers.page.gohtml @@ -0,0 +1,20 @@ + + +
+ + +General commands:
+{{ with .Timers }} +{{ range . }} ++ Name: {{ .Name }} + Text: {{ .Text }} + Repeat: {{ .Repeat }} +
+{{ end }} +{{ end }}