mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
switch to httprouter. add /commands/:channel route to display commands for a channel
This commit is contained in:
parent
8e13bb4102
commit
70e8081045
|
@ -207,7 +207,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start status page
|
// Start status page
|
||||||
go app.statusPage()
|
go app.startRouter()
|
||||||
|
|
||||||
// Actually connect to chat.
|
// Actually connect to chat.
|
||||||
err = app.TwitchClient.Connect()
|
err = app.TwitchClient.Connect()
|
||||||
|
|
75
cmd/nourybot/router.go
Normal file
75
cmd/nourybot/router.go
Normal file
|
@ -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))
|
||||||
|
}
|
|
@ -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))
|
|
||||||
}
|
|
1
go.mod
1
go.mod
|
@ -26,4 +26,5 @@ require (
|
||||||
github.com/briandowns/openweathermap v0.19.0
|
github.com/briandowns/openweathermap v0.19.0
|
||||||
github.com/dustin/go-humanize v1.0.1
|
github.com/dustin/go-humanize v1.0.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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/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 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
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 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
github.com/nicklaw5/helix/v2 v2.25.1 h1:hccFfWf1kdPKeC/Zp8jNbOvqV0f6ya12hdeNHuQa5wg=
|
github.com/nicklaw5/helix/v2 v2.25.1 h1:hccFfWf1kdPKeC/Zp8jNbOvqV0f6ya12hdeNHuQa5wg=
|
||||||
|
|
Loading…
Reference in a new issue