1. Project Setup & Custom Configuration
In this 9-part tutorial, we will build TaskForge (github.com/acme/taskforge) — a complete, production-structured backend using every feature of GoForge:
- Limen Authentication (cookie sessions, bcrypt password hashing,
RequireAuthDI middleware) - User-Owned Task CRUD (GORM models, custom
validator/v10rules, canonicalAppErrorresponses) - Database Tooling (
di.CollectModels, Atlas migrations, SQLC type-safe queries, transactional seeders) - In-Process Event Bus (async
Dispatchwithcontext.WithoutCancel,DispatchSync, audit log listeners) - Interactive OpenAPI 3.1 Docs (Huma v2 docs for both
authandtaskservices) - gRPC & Typed Clients (protobuf CRUD + streaming,
grpcurl,boot/clientHTTP & gRPC clients) - TUI Dashboard & Custom Commands (
gen:command, live server TUI, Air hot reload) - Automated Tests & Production Docker (unit, HTTP, gRPC, integration tests,
-tags prodcontainer)
1.1 Scaffold the Project
Create the taskforge project with module path github.com/acme/taskforge:
goforge new taskforge github.com/acme/taskforge
cd taskforge(If you run goforge new with no arguments, the interactive wizard lets you choose which database drivers to keep. With two arguments, GoForge keeps sqlite and initializes a Git repository automatically.)
Inspect the generated layout:
taskforge/
├── app/main.go # Calls console.Execute()
├── boot/ # Kernel, HTTP/gRPC servers, middleware, clients
├── core/ # Config, console/TUI, database, di, errors, events, validator
├── internal/
│ ├── database/
│ │ ├── migrations/ # Atlas SQL migrations
│ │ ├── queries/ # SQLC SQL queries
│ │ └── seeders/ # DatabaseSeeder & custom seeders
│ ├── proto/ # .proto definitions
│ ├── services/
│ │ ├── kernel.go # Modules() & Model() composition root
│ │ └── todo/ # Built-in reference service
│ └── tests/ # unit/, http/, grpc/, integration/
├── .env.example
├── air.toml
└── Dockerfile1.2 Configure .env and Generate APP_KEY
Copy .env.example to .env and generate a 32-byte base64 APP_KEY:
cp .env.example .env
goforge gen:keyOpen .env and configure it for TaskForge:
APP_NAME=taskforge
APP_VERSION=1.0.0
APP_MODULE=github.com/acme/taskforge
APP_DEBUG=true
APP_KEY=<generated-by-goforge-gen:key>
APP_HOST=127.0.0.1
APP_PORT=3000
GRPC_ENABLE=true
GRPC_REFLECTION=true
GRPC_HOST=127.0.0.1
GRPC_PORT=50051
RATE_LIMIT_PER_MINUTE=60
CORS_ALLOWED_ORIGINS=http://localhost:5173
DB_CONNECTION=sqlite
DB_NAME=taskforge.db
DB_MIGRATOR=atlas
DB_MAX_OPEN_CONNS=25
DB_MAX_IDLE_CONNS=5
DB_CONN_MAX_LIFETIME=5m
LOG_TYPE=terminal
LOG_LEVEL=info
LOG_FORMAT=log
LOG_COLOR=autoWhy APP_KEY and CORS_ALLOWED_ORIGINS matter
boot/server/middleware/auth.goenforcesX-App-Keyon all routes (except/health,/ready, and/api/docs*whenAPP_DEBUG=true).core/configfails fast inValidateForServe()ifAPP_KEYis empty.CORS_ALLOWED_ORIGINS=http://localhost:5173enablesAccess-Control-Allow-Credentials: truefor exact origin matches (required for browser session cookies). Wildcard*never sendsAllow-Credentials.
1.3 Add a Custom Config Module (TaskConfig)
Let's add a custom configuration struct for task limits (e.g., TASK_MAX_PER_USER, default 100) using goforge gen:config:
goforge gen:config taskThis creates core/config/task.go and patches core/config/config.go.
Manual one-line check in core/config/config.go
Currently goforge gen:config adds the field to AllConfig and Load(), but does not add the package-level variable to var (...). Open core/config/config.go and ensure Task TaskConfig is inside the var (...) block:
var (
App AppConfig
DB DBConfig
GRPC GRPCConfig
HTTP HTTPConfig
Encrypt EncryptConfig
Log LogConfig
Task TaskConfig // <-- add this line
)Now replace core/config/task.go with our clean TaskConfig:
package config
import "github.com/spf13/viper"
type TaskConfig struct {
MaxPerUser int
}
func loadTaskConfig() (TaskConfig, error) {
viper.SetDefault("TASK_MAX_PER_USER", 100)
return TaskConfig{
MaxPerUser: viper.GetInt("TASK_MAX_PER_USER"),
}, nil
}Optionally add TASK_MAX_PER_USER=100 to .env.
Verify everything compiles cleanly so far:
go build ./app