Add support for videoId query flags

This commit is contained in:
WardPearce 2024-04-23 18:51:03 +12:00
parent c7a6438110
commit 617f8ab7ef

15
main.go
View file

@ -51,15 +51,26 @@ func main() {
}, },
) )
app.Get("/votes/:videoId", handler) // Route for /votes?videoId=:videoId
app.Get("/votes", handleQuery)
// Route for /votes/:videoId
app.Get("/votes/:videoId", handleParam)
log.Fatal(app.Listen(":3000")) log.Fatal(app.Listen(":3000"))
} }
func handler(c *fiber.Ctx) error { func handleQuery(c *fiber.Ctx) error {
videoId := c.Query("videoId")
return getVotes(c, videoId)
}
func handleParam(c *fiber.Ctx) error {
videoId := c.Params("videoId") videoId := c.Params("videoId")
return getVotes(c, videoId)
}
func getVotes(c *fiber.Ctx, videoId string) error {
match, _ := regexp.Match("^([a-zA-Z0-9_-]{11})", []byte(videoId)) match, _ := regexp.Match("^([a-zA-Z0-9_-]{11})", []byte(videoId))
if !match { if !match {