Update default example and test

state-management
Juan Calderon-Perez 2025-03-30 21:31:41 -04:00
parent 9ea8b223a7
commit a9c29b121c
2 changed files with 11 additions and 10 deletions

View File

@ -478,6 +478,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
) )
@ -489,7 +490,7 @@ func main() {
// Middleware: Increase counter for every request. // Middleware: Increase counter for every request.
app.Use(func(c fiber.Ctx) error { app.Use(func(c fiber.Ctx) error {
count := c.App().GetStateWithDefault(app.State(), "requestCount", 0) count, _ := c.App().State().GetInt("requestCount")
app.State().Set("requestCount", count+1) app.State().Set("requestCount", count+1)
return c.Next() return c.Next()
}) })
@ -499,7 +500,7 @@ func main() {
}) })
app.Get("/stats", func(c fiber.Ctx) error { app.Get("/stats", func(c fiber.Ctx) error {
count := c.App().GetStateWithDefault(c.App().State(), "requestCount", 0) count, _ := c.App().State().Get("requestCount")
return c.SendString(fmt.Sprintf("Total requests: %d", count)) return c.SendString(fmt.Sprintf("Total requests: %d", count))
}) })
@ -542,8 +543,8 @@ func main() {
app.Get("/config", func(c fiber.Ctx) error { app.Get("/config", func(c fiber.Ctx) error {
config := map[string]any{ config := map[string]any{
"environment": environment, "environment": environment,
"apiUrl": c.App().GetStateWithDefault(app.State(), "apiUrl", ""), "apiUrl": fiber.GetStateWithDefault(app.State(), "apiUrl", ""),
"debug": c.App().GetStateWithDefault(app.State(), "debug", false), "debug": fiber.GetStateWithDefault(app.State(), "debug", false),
} }
return c.JSON(config) return c.JSON(config)
}) })
@ -600,7 +601,7 @@ func main() {
} }
// Save the user to the database. // Save the user to the database.
rdb, ok := c.App().GetState[*redis.Client](app.State(), "redis") rdb, ok := fiber.GetState[*redis.Client](app.State(), "redis")
if !ok { if !ok {
return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found") return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found")
} }
@ -618,7 +619,7 @@ func main() {
app.Get("/user/:id", func(c fiber.Ctx) error { app.Get("/user/:id", func(c fiber.Ctx) error {
id := c.Params("id") id := c.Params("id")
rdb, ok := c.App().GetState[*redis.Client](app.State(), "redis") rdb, ok := fiber.GetState[*redis.Client](app.State(), "redis")
if !ok { if !ok {
return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found") return c.Status(fiber.StatusInternalServerError).SendString("Redis client not found")
} }

View File

@ -13,13 +13,13 @@ func TestState_SetAndGet_WithApp(t *testing.T) {
app := New() app := New()
// test setting and getting a value // test setting and getting a value
app.state.Set("foo", "bar") app.State().Set("foo", "bar")
val, ok := app.state.Get("foo") val, ok := app.State().Get("foo")
require.True(t, ok) require.True(t, ok)
require.Equal(t, "bar", val) require.Equal(t, "bar", val)
// test key not found // test key not found
_, ok = app.state.Get("unknown") _, ok = app.State().Get("unknown")
require.False(t, ok) require.False(t, ok)
} }