Re-implemented demo button
This commit is contained in:
parent
9e790696a5
commit
9d39ff5bc2
2 changed files with 18 additions and 2 deletions
|
@ -36,6 +36,10 @@ func main() {
|
||||||
// Register our handlers
|
// Register our handlers
|
||||||
server.Post("/api/register", gs.Register)
|
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
|
// Announce the port we are listening on and start the server
|
||||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,16 +10,19 @@ import (
|
||||||
// The actual interface that we will use
|
// The actual interface that we will use
|
||||||
type GlobalState interface {
|
type GlobalState interface {
|
||||||
Register(c *fiber.Ctx) error
|
Register(c *fiber.Ctx) error
|
||||||
|
GetButtonCount(c *fiber.Ctx) error
|
||||||
|
IncrementButtonCount(c *fiber.Ctx) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// "Constructor"
|
// "Constructor"
|
||||||
func NewGlobalState(db database.Database) GlobalState {
|
func NewGlobalState(db database.Database) GlobalState {
|
||||||
return &GState{Db: db}
|
return &GState{Db: db, ButtonCount: 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The global state, which implements all the handlers
|
// The global state, which implements all the handlers
|
||||||
type GState struct {
|
type GState struct {
|
||||||
Db database.Database
|
Db database.Database
|
||||||
|
ButtonCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GState) Register(c *fiber.Ctx) error {
|
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")
|
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})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue