Routing
GoliveKit's router supports LiveView components, HTTP handlers, and middleware.
Basic Routes
main.go
r := router.New()
// HTTP handler
r.Handle("/api/health", healthHandler)
// HTTP handler function
r.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
// Method-specific routes (Go 1.22+)
r.Get("/users", listUsers)
r.Post("/users", createUser)
r.Put("/users/{id}", updateUser)
r.Delete("/users/{id}", deleteUser)
http.ListenAndServe(":3000", r)LiveView Routes
Use r.Live() to register LiveView components:
main.go
// LiveView handles both HTTP (initial render) and WebSocket (updates)
r.Live("/", NewHomePage)
r.Live("/dashboard", NewDashboard)
r.Live("/users/{id}", NewUserProfile)
// Don't forget the client JS
r.Handle("/_live/", http.StripPrefix("/_live/", client.Handler()))Route Groups
routes.go
// Group routes with a common prefix
r.Group("/api", func(g *router.RouteGroup) {
g.Get("/users", listUsers)
g.Post("/users", createUser)
g.Get("/users/{id}", getUser)
})
// Nested groups
r.Group("/admin", func(g *router.RouteGroup) {
g.Use(requireAdmin) // Group middleware
g.Live("/dashboard", NewAdminDashboard)
g.Live("/users", NewUserManagement)
})Middleware
Built-in middleware for common tasks:
middleware.go
r := router.New()
// Request ID - adds X-Request-ID header
r.Use(router.RequestID())
// Logger - structured request logging
r.Use(router.Logger())
// Recovery - panic recovery with stack trace
r.Use(router.Recovery())
// CORS - Cross-Origin Resource Sharing
r.Use(router.CORS(router.CORSConfig{
AllowOrigins: []string{"https://example.com"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowHeaders: []string{"Authorization", "Content-Type"},
}))
// Rate limiting
r.Use(router.RateLimit(100)) // 100 requests per minute
// Secure headers (CSP, X-Frame-Options, etc.)
r.Use(router.SecureHeaders())Route Parameters
params.go
// Route: /users/{id}/posts/{postID}
func (c *PostView) Mount(ctx context.Context, params core.Params, session core.Session) error {
// URL parameters
userID := params["id"] // "123"
postID := params["postID"] // "456"
// Query parameters (from URL: ?sort=date&page=2)
sort := params["sort"] // "date"
page := params["page"] // "2"
return nil
}Static Files
static.go
// Serve static files from a directory
r.Static("/static/", "web/static")
// Or use http.FileServer
r.Handle("/assets/", http.StripPrefix("/assets/",
http.FileServer(http.Dir("public/assets"))))