Framework Guide - Lokstra as Business Application Framework
Use Lokstra like NestJS or Spring Boot - Full enterprise framework with DI, services, and zero-code deployment topology changes
π‘ New to framework comparison? See detailed comparisons:
- Lokstra vs NestJS - TypeScript framework comparison
- Lokstra vs Spring Boot - Java framework comparison
Welcome to Track 2 of Lokstra! Here youβll learn to use Lokstra as a complete business application framework with:
- β Lazy Dependency Injection - Type-safe with Go generics
- β Service Architecture - Clean separation of concerns
- β Auto-Generated Routers - REST APIs from service definitions
- β Configuration-Driven Deployment - Monolith β Microservices without code changes
- β Environment Management - Dev, staging, production configs
Framework Comparison
| Feature | Lokstra Framework | NestJS | Spring Boot |
|---|---|---|---|
| Language | Go | TypeScript | Java |
| DI Pattern | Lazy + Generics | Decorators + Reflection | Annotations + Reflection |
| Performance | Compiled, fast startup | Runtime, slower startup | JVM, medium startup |
| Auto Router | β From service methods | β From decorators | β From annotations |
| Deployment Flexibility | β Zero-code topology change | β Requires code changes | β Requires code changes |
| Type Safety | β Compile-time | β TypeScript | β Java |
Detailed Comparisons:
- π Lokstra vs NestJS - TypeScript framework analysis
- π Lokstra vs Spring Boot - Java framework analysis
Quick Framework Example
1. Define Service (user_service.go):
type UserService struct {
db *Database
}
func (s *UserService) GetAll(p *GetAllParams) ([]User, error) {
return s.db.Query("SELECT * FROM users")
}
func (s *UserService) GetByID(p *GetByIDParams) (*User, error) {
return s.db.QueryOne("SELECT * FROM users WHERE id = ?", p.ID)
}
func (s *UserService) Create(p *CreateParams) (*User, error) {
user := &User{Name: p.Name, Email: p.Email}
return s.db.Insert(user)
}
func UserServiceFactory(deps map[string]any, config map[string]any) any {
return &UserService{
db: deps["database"].(*Database),
}
}
2. Register Service Factory (one-time setup):
lokstra_registry.RegisterServiceType(
"user-service-factory",
UserServiceFactory,
nil,
deploy.WithResource("user", "users"),
deploy.WithConvention("rest"),
)
3. Configure Deployment (config.yaml):
service-definitions:
user-service:
type: user-service-factory
depends-on: [database]
database:
type: database-factory
deployments:
production:
servers:
api:
addr: ":8080"
published-services: [user-service] # Auto-generates REST router!
4. Run Application:
func main() {
RegisterServiceAndMiddlewareTypes()
lokstra_registry.RunServerFromConfig()
}
Generated Routes:
GET /users β UserService.GetAll()
GET /users/{id} β UserService.GetByID()
POST /users β UserService.Create()
PUT /users/{id} β UserService.Update() (if defined)
DELETE /users/{id} β UserService.Delete() (if defined)
Learning Path
π― 1. Service Management
Learn the heart of Lokstra Framework - lazy dependency injection and service architecture.
What youβll learn:
- β
Type-safe lazy loading with
service.LazyLoad[T] - β Service registration and factory patterns
- β Dependency resolution and caching
- β Testing strategies with mocked dependencies
Key concepts:
// Service-level lazy loading - services created on first access
var userService = service.LazyLoad[*UserService]("user-service")
var database = service.LazyLoad[*Database]("database")
func handler() {
// Thread-safe, dependencies eagerly loaded when service created
users := userService.MustGet().GetAll()
}
π 2. Configuration Management
Master YAML-driven configuration and environment management.
What youβll learn:
- β Service definitions and dependencies
- β Multi-environment deployments
- β Environment variable overrides
- β Deployment topology strategies
Key concepts:
service-definitions:
user-service:
type: user-service-factory
depends-on: [database, cache]
deployments:
development:
servers:
dev: {addr: ":3000", published-services: [user-service]}
production:
servers:
api: {addr: ":8080", published-services: [user-service]}
worker: {addr: ":8081", published-services: [background-jobs]}
π¦ 3. Auto Router Generation
Generate REST APIs automatically from service method signatures.
What youβll learn:
- β Convention-based routing from method names
- β Parameter binding and validation
- β Route customization and overrides
- β Middleware integration
Key concepts:
// Method signature determines HTTP route
func (s *UserService) GetAll(p *GetAllParams) ([]User, error) // GET /users
func (s *UserService) GetByID(p *GetByIDParams) (*User, error) // GET /users/{id}
func (s *UserService) Create(p *CreateParams) (*User, error) // POST /users
// Register with auto-router
lokstra_registry.RegisterServiceType("user-service-factory", NewUserService, nil,
deploy.WithResource("user", "users"))
π§ 4. Middleware & Plugins
Add cross-cutting concerns like authentication, logging, and validation.
What youβll learn:
- β Framework middleware integration
- β Service-level middleware
- β Request/Response transformation
- β Error handling patterns
π 5. Application & Server Management
Deploy and manage your application across different environments.
What youβll learn:
- β Multi-server deployments
- β Monolith vs microservices patterns
- β Health checks and monitoring
- β Graceful shutdown handling
Framework Benefits
vs Manual Service Management
- β No initialization order issues - lazy loading handles dependencies
- β Type-safe dependency injection - compile-time guarantees
- β Declarative configuration - YAML instead of complex bootstrap code
- β Easy testing - mock services via registry
vs Other Go DI Frameworks
- β
Zero reflection overhead - uses generics, not
any - β Lazy by default - memory efficient, fast startup
- β Auto-generated routers - no controller boilerplate
- β Configuration flexibility - works with or without YAML
vs Traditional Frameworks (NestJS/Spring)
- β Better performance - compiled Go vs runtime interpretation
- β Simpler deployment - single binary vs complex packaging
- β Flexible topology - monolith β microservices without code changes
- β Type safety - compile-time errors vs runtime discovery
When to Use Framework Track
Choose Framework Track when youβre building:
- β Enterprise applications with multiple services
- β Applications that may scale from monolith to microservices
- β Team projects needing consistent patterns
- β API-heavy applications with many endpoints
- β Applications with complex dependencies between components
If you just need simple routing, consider Router Track instead.
Next Steps
- Start with Service Management - Core framework concepts
- Learn Configuration - YAML-driven setup
- Explore Auto Routers - Generate REST APIs
- Add Middleware - Cross-cutting concerns
- Deploy Applications - Production patterns
Ready to build enterprise applications with Lokstra? Letβs start with services! π
Quick Links
- π― Router Track - Use as Router only (like Echo/Gin)
- π‘ Introduction - New to Lokstra?
- π API Reference - Complete technical docs
- π§© Examples - Working code samples
- π Lokstra vs NestJS - TypeScript framework comparison
- β Lokstra vs Spring Boot - Java framework comparison