Re-implemented demo button

This commit is contained in:
Imbus 2024-03-06 09:41:36 +01:00
parent 9e790696a5
commit 9d39ff5bc2
2 changed files with 18 additions and 2 deletions

View file

@ -36,6 +36,10 @@ func main() {
// Register our handlers
server.Post("/api/register", gs.Register)
// Register handlers for example button count
server.Get("/api/button", gs.GetButtonCount)
server.Post("/api/button", gs.IncrementButtonCount)
// Announce the port we are listening on and start the server
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
if err != nil {

View file

@ -10,16 +10,19 @@ import (
// The actual interface that we will use
type GlobalState interface {
Register(c *fiber.Ctx) error
GetButtonCount(c *fiber.Ctx) error
IncrementButtonCount(c *fiber.Ctx) error
}
// "Constructor"
func NewGlobalState(db database.Database) GlobalState {
return &GState{Db: db}
return &GState{Db: db, ButtonCount: 0}
}
// The global state, which implements all the handlers
type GState struct {
Db database.Database
ButtonCount int
}
func (gs *GState) Register(c *fiber.Ctx) error {
@ -34,3 +37,12 @@ func (gs *GState) Register(c *fiber.Ctx) error {
return c.Status(200).SendString("User added")
}
func (gs *GState) GetButtonCount(c *fiber.Ctx) error {
return c.Status(200).JSON(fiber.Map{"pressCount": gs.ButtonCount})
}
func (gs *GState) IncrementButtonCount(c *fiber.Ctx) error {
gs.ButtonCount++
return c.Status(200).JSON(fiber.Map{"pressCount": gs.ButtonCount})
}