mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add /timers route that shows all timers
This commit is contained in:
parent
6acc16b6cc
commit
283734935f
|
@ -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
|
||||
}
|
||||
|
|
20
web/templates/timers.page.gohtml
Normal file
20
web/templates/timers.page.gohtml
Normal file
|
@ -0,0 +1,20 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width,initial-scale=1" name="viewport">
|
||||
<title>nourybot - lidl twitch bot</title>
|
||||
<meta property="og:title" content="nourybot - timers">
|
||||
<meta property="og:description" content="nourybot">
|
||||
</head>
|
||||
<h1>Commands</h1>
|
||||
<p>General commands:</p>
|
||||
{{ with .Timers }}
|
||||
{{ range . }}
|
||||
<p>
|
||||
<b>Name:</b> {{ .Name }} </br>
|
||||
<b>Text:</b> {{ .Text }} </br>
|
||||
<b>Repeat:</b> {{ .Repeat }} </br>
|
||||
</p>
|
||||
{{ end }}
|
||||
{{ end }}
|
Loading…
Reference in a new issue