package mainimport "github.com/go-mizu/mizu"func main() { app := mizu.New() // Homepage app.Get("/", func(c *mizu.Ctx) error { return c.JSON(200, map[string]string{ "message": "Hello, Mizu!", }) }) // Get user by ID app.Get("/users/{id}", func(c *mizu.Ctx) error { id := c.Param("id") return c.JSON(200, map[string]string{ "id": id, "name": "User " + id, }) }) // Create a user app.Post("/users", func(c *mizu.Ctx) error { var input struct { Name string `json:"name"` } if err := c.BindJSON(&input, 1<<20); err != nil { return c.JSON(400, map[string]string{"error": err.Error()}) } return c.JSON(201, map[string]string{ "id": "123", "name": input.Name, }) }) app.Listen(":3000")}
Test the new routes:
# Get a usercurl http://localhost:3000/users/42# Create a usercurl -X POST http://localhost:3000/users \ -H "Content-Type: application/json" \ -d '{"name": "Alice"}'