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?

Ready for More?


πŸ’‘ 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