Skip to main content

Quickstart

Minimal Server

The simplest Zen server - one route, one response:

package main

import "github.com/Pavan-Silva/go-zen"

func main() {
r := zen.New(":8080")
r.GET("/", func(c *zen.Ctx) {
c.String(200, "Hello, World!")
})
r.Run()
}

zen.New(":8080") creates a router. r.Run() starts the server with graceful shutdown.

With Middleware

Add recovery and logging middleware in production:

r := zen.New(":8080")
r.Use(middleware.Recover)
r.Use(middleware.Logger)
r.GET("/api/health", func(c *zen.Ctx) {
c.JSON(200, map[string]string{"status": "ok"})
})
r.Run()

Route Parameters

Access path parameters with c.Param("name") - zero-allocation extraction from the radix tree router:

r.GET("/users/{id}", func(c *zen.Ctx) {
c.JSON(200, map[string]string{"id": c.Param("id")})
})

Request Binding

Bind JSON bodies, validate, and respond:

type CreateUser struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}

r.POST("/users", func(c *zen.Ctx) {
var req CreateUser
if err := c.BindJSON(&req); err != nil {
c.Error(400, err.Error())
return
}
if err := zen.Validate(&req); err != nil {
c.Error(400, err.Error())
return
}
c.JSON(201, req)
})

HTTPS

Start with TLS using a certificate and key file:

r.RunTLS("cert.pem", "key.pem")

Full Example

See the intro page for a complete runnable example.