Track 1: Router-Only Examples
Use Lokstra as a flexible HTTP router (like Echo, Gin, Chi)
Time: 2-3 hours β’ Level: Beginner
π What Youβll Learn
This track focuses on using Lokstra as a pure HTTP router without dependency injection or framework features:
- β Basic routing and handler registration
- β 29 different handler signature variations
- β Middleware patterns (global, per-route, groups)
- β Quick API prototyping
No DI, no config files, no services - just routing!
π― Learning Path
01 - Hello World β±οΈ 15 min
Your first Lokstra API in 10 lines of code.
r := lokstra.NewRouter("api")
r.GET("/", func() string {
return "Hello, Lokstra!"
})
app := lokstra.NewApp("hello", ":3000", r)
app.Run(30 * time.Second)
What youβll learn:
- Router creation
- Simple handlers
- Starting the app
02 - Handler Forms β±οΈ 30 min
Explore Lokstraβs flexible handler signatures.
// Simple return
r.GET("/ping", func() string { return "pong" })
// With error
r.GET("/users", func() ([]User, error) {
return db.GetUsers()
})
// Request binding
r.POST("/users", func(req *CreateUserRequest) (*User, error) {
return db.Create(req)
})
// Full control
r.GET("/custom", func(ctx *request.Context) (*response.Response, error) {
resp := response.NewResponse()
resp.Json(data)
return resp, nil
})
What youβll learn:
- 29 handler signature variations
- When to use each form
- Request parameter binding
- Response patterns
03 - Middleware β±οΈ 45 min
Master middleware for cross-cutting concerns.
// Global middleware
r.Use(RecoveryMiddleware, LoggerMiddleware)
// Per-route middleware
r.GET("/public", publicHandler)
r.GET("/protected", protectedHandler, AuthMiddleware)
// Group middleware
admin := r.AddGroup("/admin")
admin.Use(AuthMiddleware, AdminMiddleware)
admin.GET("/users", listUsers)
What youβll learn:
- Global middleware (all routes)
- Per-route middleware (specific endpoints)
- Group middleware (API versioning)
- Custom middleware creation
- Built-in middleware (CORS, Recovery, Logger)
π Running Examples
# Navigate to any example
cd 01-hello-world # or 02, 03
# Run it
go run main.go
# Test it
curl http://localhost:3000/
π Skills Progression
Example 01: Router Basics
β Create routers, register simple handlers
Example 02: Handler Flexibility
β 29 handler forms, request/response patterns
Example 03: Middleware
β Global, per-route, groups, custom middleware
π― After This Track
Want to Continue with Router?
- Router Guide - Deep dive into routing features
- API Reference - Router - Complete router API
Ready for More?
- Framework Track - Learn DI, services, auto-router
- Framework Guide - Enterprise patterns
π‘ When to Use Router-Only
Good for:
- β Quick prototypes and MVPs
- β Simple REST APIs
- β Learning HTTP routing
- β Microservices without DI needs
- β Teams familiar with Echo/Gin/Chi
Upgrade to Framework when:
- Need dependency injection
- Want auto-generated routers
- Building microservices
- Need configuration-driven deployment
Ready to start? β 01 - Hello World