Skip to content

primadi/lokstra

Repository files navigation

Lokstra

Lokstra Logo

Modern Go Web Framework with Declarative Service Management

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


What is Lokstra?

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

🎯 As a Router (Like Echo, Gin, Chi)

Use Lokstra as a fast, flexible HTTP router with elegant middleware support.

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

πŸ—οΈ As a Framework (Like NestJS, Spring Boot)

Leverage lazy dependency injection, auto-generated routers, and configuration-driven deployment.

// Type-safe lazy DI
var userService = service.LazyLoad[*UserService]("user-service")

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

Quick Start

πŸš€ Using Lokstra CLI (Recommended)

1. Install CLI:

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

2. Create Project:

# Interactive template selection
lokstra new myapp

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

3. Run:

cd myapp
go run .

Done! Your app is running with routes already set up.

πŸ“¦ Using as Library

go get github.com/primadi/lokstra
package main

import "github.com/primadi/lokstra"

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

Features

⚑ Lokstra CLI

  • πŸ“¦ 6+ Production Templates - Router patterns & enterprise frameworks
  • πŸ”§ Auto-fix Imports - No manual configuration needed
  • πŸš€ Code Generation - Built-in autogen command for annotation-based templates
  • 🌐 Always Updated - Templates downloaded from GitHub
lokstra new myapp                           # Interactive
lokstra new myapp -template 01_router/...   # Specific template
lokstra autogen ./myproject                 # Generate code

🎯 Lazy Dependency Injection

Type-safe service loading with zero overhead:

var db = service.LazyLoad[*Database]("database")
users := db.MustGet().GetAll()  // Loaded once, cached forever

πŸ”„ Auto-Generated Routers

Generate REST APIs from service definitions:

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

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

πŸ—οΈ Flexible Deployment

One codebase, multiple topologies:

# Monolith
servers:
  all-in-one:
    published-services: [user-service, order-service]

# Microservices
servers:
  user-api:
    published-services: [user-service]
  order-api:
    published-services: [order-service]

πŸ“ Type-Safe Request Binding

Automatic validation with struct tags:

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

func createUser(ctx *request.Context, params *CreateUserParams) error {
    // params already validated!
    user := db.CreateUser(params.Name, params.Email)
    return ctx.Api.Ok(user)
}

CLI Templates

Router Patterns

  • 01_router/01_router_only - Pure routing basics
  • 01_router/02_single_app - Production single app
  • 01_router/03_multi_app - Multiple apps server

Framework Patterns

  • 02_app_framework/01_medium_system - Domain-driven (2-10 entities)
  • 02_app_framework/02_enterprise_modular - DDD with bounded contexts
  • 02_app_framework/03_enterprise_router_service - Annotation-based enterprise

πŸ“– View All Templates β†’


Documentation


Why Lokstra?

vs Traditional Routers (Echo, Gin, Chi)

βœ… 29+ handler signatures - Ultimate flexibility
βœ… Clean middleware - Easy to compose
βœ… Type-safe DI - Optional, when you need it
βœ… Auto-generated routes - From services

vs DI Frameworks (Fx, Wire, Dig)

βœ… Type-safe - No any casting
βœ… Zero reflection - In hot path
βœ… Lazy loading - Memory efficient
βœ… Optional config - Start with code, scale with YAML

vs Full Frameworks (NestJS, Spring Boot)

βœ… Simpler - No decorators, no code generation required
βœ… Flexible - Use as router OR framework
βœ… Go-native - Idiomatic Go patterns
βœ… Deployment agnostic - Monolith β†’ microservices without code changes


Project Structure

lokstra/
β”œβ”€β”€ cmd/lokstra/              # CLI tool for scaffolding
β”œβ”€β”€ core/                     # Core framework
β”‚   β”œβ”€β”€ deploy/              # Deployment & config
β”‚   β”œβ”€β”€ registry/            # Service registry
β”‚   β”œβ”€β”€ request/             # Request handling
β”‚   └── router/              # HTTP routing
β”œβ”€β”€ project_templates/        # Project templates
β”‚   β”œβ”€β”€ templates.json       # Template registry
β”‚   β”œβ”€β”€ 01_router/          # Router patterns
β”‚   └── 02_app_framework/   # Framework patterns
β”œβ”€β”€ services/                # Built-in services
β”œβ”€β”€ middleware/              # Standard middleware
└── docs/                    # Documentation site

Community & Support


License

Apache 2.0 License - see LICENSE file for details.


Made with ❀️ by Primadi

⭐ Star on GitHub

About

A versatile Go web framework that works in two ways: as a Router or as a complete Business Application Framework

Resources

License

Stars

Watchers

Forks