Lokstra Logo

Lokstra

Modern Go Web Framework with Declarative Service Management

Quick Start β€’ Features β€’ Examples β€’ Documentation β€’ GitHub


What is Lokstra?

Lokstra is a versatile Go web framework that works in two ways:

🎯 Track 1: As a Router (Like Echo, Gin, Chi)

Use Lokstra as a fast, flexible HTTP router with elegant middleware support. Perfect when you just need routing without the complexity of DI frameworks.

r := lokstra.NewRouter("api")
r.GET("/", func() string {
    return "Hello, Lokstra!"
})
app := lokstra.NewApp("hello", ":3000", r)
app.Run(30 * time.Second)

πŸ—οΈ Track 2: As a Business Application Framework (Like NestJS, Spring Boot)

Leverage the full framework with lazy dependency injection, auto-generated routers, and configuration-driven deployment. Build enterprise applications that scale from monolith to microservices.

// Define services in YAML
service-definitions:
  user-service:
    type: user-service-factory
    depends-on: [database]

// Service-level lazy loading - service created on first access
userService := service.LazyLoad[*UserService]("user-service")
users := userService.MustGet().GetAll()

Choose your path:


Quick Start

Install Lokstra CLI

go install github.com/primadi/lokstra/cmd/lokstra@latest

Create New Project

# Interactive template selection
lokstra new myapp

# Or choose specific template
lokstra new myapp -template 02_app_framework/01_medium_system

Install Framework Library

go get github.com/primadi/lokstra

Hello World (30 seconds)

package main

import "github.com/primadi/lokstra"

func main() {
    r := lokstra.NewRouter("api")
    
    r.GET("/", func() string {
        return "Hello, Lokstra!"
    })
    
    r.GET("/ping", func() string {
        return "pong"
    })
    
    app := lokstra.NewApp("hello", ":3000", r)
    if err := app.Run(30 * time.Second); err != nil {
      log.Fatal(err)
    }
}

With Services & Configuration (2 minutes)

1. Define configuration (config.yaml):

service-definitions:
  user-service:
    type: user-service-factory

deployments:
  production:
    servers:
      api:
        base-url: https://api.example.com
        addr: ":8080"
        published-services: [user-service]

2. Register factory (one-time setup):

lokstra_registry.RegisterServiceType("user-service-factory", 
    func() any { return service.NewUserService() }, nil)

3. Use anywhere (zero overhead after first access):

var userService = service.LazyLoad[*UserService]("user-service")

func handler() {
    users := userService.MustGet().GetAll()  // Loaded once, cached forever
}

πŸ‘‰ Full Tutorial


Key Features

🎯 Service-Level Lazy Loading

Type-safe service loading with zero overhead. Services created only when needed, dependencies eagerly resolved.

// Define once, use everywhere
var db = service.LazyLoad[*Database]("database")
var cache = service.LazyLoad[*Cache]("cache")

// Service loaded on first access, cached forever
users := db.MustGet().GetAll()

Benefits:

Learn More β†’


πŸ”„ Auto-Generated Routers

Generate REST APIs from service definitions automatically.

service-definitions:
  user-service:
    type: user-service-factory

deployments:
  production:
    servers:
      api:
        published-services: [user-service]  # Auto-creates REST router!

Generated routes:

GET    /users       β†’ List all users
GET    /users/{id}  β†’ Get user by ID
POST   /users       β†’ Create user
PUT    /users/{id}  β†’ Update user
DELETE /users/{id}  β†’ Delete user

With overrides:

router-definitions:
  user-service-router:
    path-prefix: /api/v1
    path-rewrites:
      - pattern: "^/api/v1/(.*)$"
        replacement: "/api/v2/$1"
    middlewares: [auth, logging]
    hidden: [InternalMethod]

Learn More β†’


πŸ—οΈ Flexible Deployment Topologies

One codebase, multiple deployment strategies. Switch from monolith to microservices without code changes.

Monolith:

deployments:
  production:
    servers:
      all-in-one:
        base-url: https://api.example.com
        published-services: [user-service, order-service, payment-service]

Microservices:

deployments:
  production:
    servers:
      user-api:
        base-url: https://user-api.example.com
        published-services: [user-service]
      
      order-api:
        base-url: https://order-api.example.com
        published-services: [order-service]

Same code, different topology!

Learn More β†’


πŸ›£οΈ Path Rewrites & Overrides

Powerful URL transformation without touching code.

router-definitions:
  api-router:
    path-rewrites:
      - pattern: "^/api/v1/(.*)$"
        replacement: "/api/v2/$1"
    middlewares: [auth, rate-limiter]

Use cases:

Learn More β†’


πŸ“ Type-Safe Request Binding

Automatic validation with struct tags. Lokstra automatically injects and validates parameters.

type CreateUserParams struct {
    Name  string `json:"name" validate:"required"`
    Email string `json:"email" validate:"required,email"`
    Age   int    `json:"age" validate:"min=18,max=100"`
}

// Parameters automatically injected and validated!
func createUser(ctx *request.Context, params *CreateUserParams) error {
    // params are already validated and ready to use
    user := db.CreateUser(params.Name, params.Email, params.Age)
    return ctx.Api.Ok(user)
}

No manual binding code needed! Lokstra handles:

Learn More β†’


πŸš€ Lokstra CLI

Create new projects from templates in seconds!

# Install CLI
go install github.com/primadi/lokstra/cmd/lokstra@latest

# Create project (interactive)
lokstra new myapp

# Create with specific template
lokstra new blog-api -template 02_app_framework/01_medium_system

# Generate code for Enterprise templates
lokstra autogen ./myproject

Available Templates

Router Patterns (Learning & Simple APIs):

Framework Patterns (Production Apps):

What CLI Does Automatically

βœ… Downloads template from GitHub
βœ… Fixes all import paths
βœ… Runs go mod init
βœ… Runs go mod tidy
βœ… Ready to run immediately!

πŸ“– Full CLI Documentation β†’


Examples

πŸ“š Introduction & Examples

Quick Examples - Get started in minutes:

Comparing Approaches:


πŸŽ“ Essential Guides

Step-by-step learning path:

  1. Quick Start - Get up and running in 5 minutes
  2. Service Management - Master lazy dependency injection
  3. Routing & Handlers - Request handling patterns
  4. Configuration - YAML setup and environment management

Each guide includes:


πŸ“– API Reference

Complete technical documentation:

Advanced Topics:


Why Lokstra?

🎯 Two Ways to Use Lokstra

Use as Router Only (Track 1)

Compare with Echo, Gin, Chi, Fiber:

// Just routing - no DI, no config files
r := lokstra.NewRouter("api")
r.GET("/users", getUsersHandler)
r.Use(cors.Middleware("*")

app := lokstra.NewApp("api", ":8080", r)
if err := app.Run(30 * time.Second); err != nil {
  log.Fatal(err)
}

βœ… Flexible handler signatures (29+ forms!)
βœ… Clean middleware support
βœ… Group routing for API versioning
βœ… No framework lock-in

β†’ Router Guide


Use as Full Framework (Track 2)

Compare with NestJS, Spring Boot, Uber Fx:

# config.yaml
service-definitions:
  user-service:
    type: user-service-factory
    depends-on: [database]

deployments:
  production:
    servers:
      api:
        published-services: [user-service]  # Auto-router!
// Type-safe lazy DI
var userService = service.LazyLoad[*UserService]("user-service")

func handler() {
    users := userService.MustGet().GetAll()
}

βœ… Lazy dependency injection (type-safe)
βœ… Auto-generated REST routers from services
βœ… Configuration-driven deployment
βœ… Monolith β†’ Microservices without code changes

β†’ Framework Guide


vs Traditional DI Frameworks

vs Manual Dependency Management

vs Other Go Frameworks


Community & Support


πŸ€– AI Agent Guide

For AI assistants

Copilot, Claude, GPT β†’

⚑ Quick Reference

Fast lookup cheatsheet

Cheatsheet β†’

🎯 Router Guide

Use Lokstra as a Router

Like Echo, Gin, Chi β†’

πŸ—οΈ Framework Guide

Full framework with DI

Like NestJS, Spring β†’
vs NestJS β€’ vs Spring Boot

πŸš€ Quick Start

New to Lokstra? Start here!

Get Started β†’

οΏ½ Examples

Learn by doing

Browse Examples β†’

οΏ½ API Reference

Complete documentation

Technical Docs β†’

πŸ—οΈ Architecture

Design principles

Framework Design β†’

⚑ CLI Tool

Project scaffolding

Lokstra CLI β†’

Made with ❀️ by Primadi

⭐ Star on GitHub β€’ Apache 2.0 License