diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index 1b129c8..d4bfd38 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -207,7 +207,7 @@ func main() { }) // Start status page - go app.statusPage() + go app.startRouter() // Actually connect to chat. err = app.TwitchClient.Connect() diff --git a/cmd/nourybot/router.go b/cmd/nourybot/router.go new file mode 100644 index 0000000..ea9cf3d --- /dev/null +++ b/cmd/nourybot/router.go @@ -0,0 +1,75 @@ +package main + +import ( + "fmt" + "net/http" + "strings" + + "github.com/julienschmidt/httprouter" + "github.com/lyx0/nourybot/internal/common" +) + +func (app *application) startRouter() { + router := httprouter.New() + router.GET("/status", app.statusPageRoute) + router.GET("/commands/:channel", app.channelCommandsRoute) + + app.Log.Fatal(http.ListenAndServe(":8080", router)) +} + +func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + channel := ps.ByName("channel") + + command, err := app.Models.Commands.GetAllChannel(channel) + if err != nil { + app.Log.Errorw("Error trying to retrieve all timers from database", err) + return + } + + // The slice of timers is only used to log them at + // the start so it looks a bit nicer. + var cs []string + + // Iterate over all timers and then add them onto the scheduler. + for i, v := range command { + // 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 c string + + if v.Category == "ascii" { + c = fmt.Sprintf( + "Name: \t%v\n"+ + "Help: \t%v\n"+ + "Level: \t%v\n"+ + "\n", + v.Name, v.Help, v.Level, + ) + } else { + c = fmt.Sprintf( + "Name: \t%v\n"+ + "Help: \t%v\n"+ + "Level: \t%v\n"+ + "Text: \t%v\n"+ + "\n", + v.Name, v.Help, v.Level, v.Text, + ) + } + + // Add new value to the slice + cs = append(cs, c) + + } + + text := strings.Join(cs, "") + fmt.Fprintf(w, fmt.Sprint(text), ps.ByName("name")) +} + +func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + commit := common.GetVersion() + started := common.GetUptime().Format("2006-1-2 15:4:5") + commitLink := fmt.Sprintf("https://github.com/lyx0/nourybot/commit/%v", common.GetVersionPure()) + + fmt.Fprintf(w, fmt.Sprintf("started: \t%v\nenvironment: \t%v\ncommit: \t%v\ngithub: \t%v", started, app.Environment, commit, commitLink)) +} diff --git a/cmd/nourybot/status_page.go b/cmd/nourybot/status_page.go deleted file mode 100644 index a0449ec..0000000 --- a/cmd/nourybot/status_page.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - "io" - "net/http" - - "github.com/lyx0/nourybot/internal/common" -) - -func (app *application) statusPage() { - commit := common.GetVersion() - started := common.GetUptime().Format("2006-1-2 15:4:5") - commitLink := fmt.Sprintf("https://github.com/lyx0/nourybot/commit/%v", common.GetVersionPure()) - - statusHandler := func(w http.ResponseWriter, req *http.Request) { - io.WriteString(w, fmt.Sprintf("started: \t%v\nenvironment: \t%v\ncommit: \t%v\ngithub: \t%v", started, app.Environment, commit, commitLink)) - } - - http.HandleFunc("/status", statusHandler) - app.Log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/go.mod b/go.mod index 1929d3c..144cc6e 100644 --- a/go.mod +++ b/go.mod @@ -26,4 +26,5 @@ require ( github.com/briandowns/openweathermap v0.19.0 github.com/dustin/go-humanize v1.0.1 github.com/joho/godotenv v1.5.1 + github.com/julienschmidt/httprouter v1.3.0 ) diff --git a/go.sum b/go.sum index a47a4b9..13c087d 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/jakecoffman/cron v0.0.0-20190106200828-7e2009c226a5 h1:kCvm3G3u+eTRbj github.com/jakecoffman/cron v0.0.0-20190106200828-7e2009c226a5/go.mod h1:6DM2KNNK69jRu0lAHmYK9LYxmqpNjYHOaNp/ZxttD4U= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/nicklaw5/helix/v2 v2.25.1 h1:hccFfWf1kdPKeC/Zp8jNbOvqV0f6ya12hdeNHuQa5wg=