Architecture & Lifecycle
GoForge is organized around a strict separation between framework runtime packages (app/, boot/, core/) and user application packages (internal/), wired together by a pure-Go reflection Dependency Injection (DI) container.
Directory Structure & Ownership
text
<project>/
├── app/
│ └── main.go # Entry point: calls console.Execute()
├── boot/ # Framework-owned: runtime wiring, servers, clients
│ ├── kernel.go # App, New, NewWithConfig, Start, Stop, buildServers
│ ├── client/
│ │ ├── http_client.go # Non-mutating HttpClient with X-App-Key
│ │ └── grpc_client.go # GrpcClient (grpc.NewClient) + AppKey interceptors
│ └── server/
│ ├── http.go # HTTPServer (Gin, probes, TrustedProxies, timeouts, TLS)
│ ├── grpc.go # GRPCServer (TLS, grpc_health_v1, reflection)
│ ├── docs.go # NewHumaConfig (Huma v2 OpenAPI 3.1 + AppKey scheme)
│ └── middleware/ # Recovery, RequestID, Logger, CORS, RateLimit, ErrorHandler, Auth, gRPC
├── core/ # Framework-owned: reusable infrastructure packages
│ ├── config/ # Viper loader, AllConfig, Validate / ValidateForServe
│ ├── console/ # Cobra root/serve/db commands, pretty logger, TUI dashboard
│ ├── database/ # Connect, safe DSNs, drivers/, Seeder registry, atlas.hcl, sqlc.yaml
│ ├── di/ # Container, Module, Bind[I], AutoCollect[I], Lifecycle
│ ├── errors/ # AppError, constructors, Respond, inspect helpers
│ ├── events/ # Generic EventBus, On[T], Off[T], Dispatch, DispatchSync, Shutdown
│ └── validator/ # Dedicated validator/v10 (validate: tags) & custom rules
└── internal/ # User-owned application code
├── database/
│ ├── migrations/ # Atlas SQL migrations & atlas.sum
│ ├── queries/ # SQLC .sql query definitions
│ ├── gen/ # SQLC generated Go package (gitignored)
│ └── seeders/ # DatabaseSeeder & custom seeders
├── proto/ # .proto files & generated pb.go
├── services/
│ ├── kernel.go # Modules() []di.Module & Model() []any
│ └── <service>/ # 9-file vertical slice
└── tests/ # unit/, http/, grpc/, integration/Startup & Graceful Shutdown Lifecycle
text
app/main.go
└─▶ console.Execute()
├─ PersistentPreRunE:
│ ├─ config.Load() # Viper reads .env + env vars + defaults
│ ├─ cfg.Validate() # Port ranges, TLS paths, DB_CONNECTION
│ └─ initLogger(cfg.Log) # Zerolog (terminal / file / both)
│
├─ serveCmd.RunE (default command when no args given):
│ ├─ setupGinDebugOutput() # Redirects Gin route/debug logs to zerolog
│ ├─ cfg.ValidateForServe() # Fails fast if APP_KEY is empty
│ ├─ startDashboard() # Bubble Tea TUI (unless --no-tui / non-TTY / CI)
│ └─ boot.NewWithConfig(cfg, services.Modules()...)
│ ├─ _ core/database/drivers (registers sqlite/postgres/mysql/sqlserver)
│ ├─ coreModule:
│ │ ├─ *config.AllConfig
│ │ ├─ *database.Database (lc.OnStop -> db.Close)
│ │ └─ *events.EventBus (lc.OnStop -> bus.Shutdown)
│ ├─ di.New(AutoCollect[Router], AutoCollect[GRPCRegistry], WithModules(...))
│ ├─ container.Build() # Topological init + Invokes (e.g. RegisterListeners)
│ └─ buildServers() # HTTPServer (WithReadinessCheck(db.Ping)) + optional GRPCServer
│
├─ app.Start():
│ ├─ container.Start(30s) # Runs OnStart hooks in registration order
│ ├─ signal.NotifyContext(SIGINT, SIGTERM)
│ └─ errgroup:
│ ├─ srv.Start() # HTTP (:3000) & gRPC (:50051)
│ └─ <-ctx.Done() -> srv.Stop(10s)
│
└─ app.Stop(30s):
├─ container.Stop(30s) # LIFO OnStop: EventBus.Shutdown -> Database.Close
└─ PersistentPostRun -> closeLogger()Why LIFO OnStop Order Matters
In boot/kernel.go, *database.Database registers its OnStop hook (db.Close()) before *events.EventBus registers bus.Shutdown(ctx). Because core/di executes OnStop hooks in reverse (LIFO) order:
- HTTP and gRPC servers stop accepting new requests (10s shutdown budget).
EventBus.Shutdown(ctx)runs first — draining all buffered and in-flight async event listeners (which may still write audit logs to the database).Database.Close()runs after the event bus has drained, ensuring no listener fails on a closed DB pool.
Server Extensibility
Every server managed by boot.App implements server.Server (boot/server/http.go):
go
type Server interface {
Name() string
Start() error
Stop(ctx context.Context) error
}boot.NewWithConfig constructs HTTPServer (always enabled) and GRPCServer (when cfg.GRPC.Enable is true), running all servers concurrently under an errgroup.Group.
