package main
import (
"log"
"net/http"
"time"
"github.com/go-mizu/mizu"
)
// Simple logging middleware
func logger(next mizu.Handler) mizu.Handler {
return func(c *mizu.Ctx) error {
start := time.Now()
err := next(c)
c.Logger().Info("request completed",
"method", c.Request().Method,
"path", c.Request().URL.Path,
"status", c.StatusCode(),
"duration", time.Since(start),
)
return err
}
}
// Authentication middleware example
func auth(next mizu.Handler) mizu.Handler {
return func(c *mizu.Ctx) error {
token := c.Query("token")
if token != "secret" {
return c.Text(http.StatusUnauthorized, "Unauthorized")
}
return next(c)
}
}
// Protected route
func dashboard(c *mizu.Ctx) error {
return c.Text(http.StatusOK, "Welcome to your dashboard")
}
// Public route
func home(c *mizu.Ctx) error {
return c.Text(http.StatusOK, "Home page - no auth required")
}
func main() {
app := mizu.New()
// Global middleware
app.Use(logger)
// Public route
app.Get("/", home)
// Protected route with route-specific middleware
app.With(auth).Get("/dashboard", dashboard)
if err := app.Listen(":8080"); err != nil {
log.Fatal(err)
}
}