Skip to content

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, RequireAuth DI middleware)
  • User-Owned Task CRUD (GORM models, custom validator/v10 rules, canonical AppError responses)
  • Database Tooling (di.CollectModels, Atlas migrations, SQLC type-safe queries, transactional seeders)
  • In-Process Event Bus (async Dispatch with context.WithoutCancel, DispatchSync, audit log listeners)
  • Interactive OpenAPI 3.1 Docs (Huma v2 docs for both auth and task services)
  • gRPC & Typed Clients (protobuf CRUD + streaming, grpcurl, boot/client HTTP & gRPC clients)
  • TUI Dashboard & Custom Commands (gen:command, live server TUI, Air hot reload)
  • Automated Tests & Production Docker (unit, HTTP, gRPC, integration tests, -tags prod container)

1.1 Scaffold the Project ​

Create the taskforge project with module path github.com/acme/taskforge:

bash
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:

text
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
└── Dockerfile

1.2 Configure .env and Generate APP_KEY ​

Copy .env.example to .env and generate a 32-byte base64 APP_KEY:

bash
cp .env.example .env
goforge gen:key

Open .env and configure it for TaskForge:

ini
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=auto

Why APP_KEY and CORS_ALLOWED_ORIGINS matter

  • boot/server/middleware/auth.go enforces X-App-Key on all routes (except /health, /ready, and /api/docs* when APP_DEBUG=true).
  • core/config fails fast in ValidateForServe() if APP_KEY is empty.
  • CORS_ALLOWED_ORIGINS=http://localhost:5173 enables Access-Control-Allow-Credentials: true for exact origin matches (required for browser session cookies). Wildcard * never sends Allow-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:

bash
goforge gen:config task

This 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:

go
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:

go
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:

bash
go build ./app

Next: 2. Authentication with Limen →

Released under the Apache-2.0 License.