Fullstack Monolith (app)
The app workload is Krewire's foundation for building production web monoliths. It compiles the entire web surfaceβHTTP routing, server-rendered views (SSR), embedded static assets, typed dependency injection, configuration, and JSON API endpointsβinto a single, self-contained Go binary.
Built on standard Go primitives (net/http, embed.FS, context, slog), the app workload requires no external web servers like Nginx or Node.js runtimes for local development or basic deployment.
Architectural Characteristics
- Zero Runtime Dependencies: Everything needed to run the application is compiled into a single static executable.
- Standard Library First: The routing, middleware, and handler interfaces integrate directly with Go's
net/httpecosystem. - Embedded Static Assets: CSS, JavaScript, and templates are compiled into the binary using Go's
embedpackage, preventing missing-asset errors in production. - Deterministic Dependency Assembly: Leverages
pkg/appfor typed dependency injection, provider registration (RegisterandBoot), and observable lifecycles. - Clean Domain Boundaries: Built around modular monolith patterns, separating configuration, HTTP handlers, SSR presentation, and domain logic.
Scaffolding an app Project
Use the krew new command (which equips the app variant by default or via --app), or equip an existing folder with krew init --app:
krew new storefront --app
cd storefront
The resulting directory tree follows Krewire's canonical modular monolith structure:
storefront/
βββ krewire.yaml # Project configuration (dev.port: 8080)
βββ go.mod # Go module definition
βββ main.go # Thin application entry point
βββ internal/
β βββ app/
β β βββ app.go # Composition root: wires providers into *web.App
β βββ config/
β β βββ config.go # Typed configuration loaded from krewire.yaml and env
β βββ http/
β βββ http.go # HTTP JSON API handlers & route mounting
βββ web/
β βββ layouts/
β β βββ shell.go # HTML shell layout with theme toggles
β βββ pages/
β β βββ pages.go # SSR page registrations (web.PageSpec)
β βββ components/
β β βββ components.go # SSR UI components (e.g. Hero)
β βββ theme/
β βββ theme.go # Theme palettes (light / dark)
βββ assets/
β βββ embed.go # Embedded static filesystem (embed.FS)
β βββ public/
β βββ app.css # Client styling
β βββ app.js # Client script bundle
βββ README.md # Project documentation
βββ .gitignore # Git exclusion rules
βββ .env.example # Environment variable template
βββ .env # Local runtime environment file
Application Entry Point (main.go)
In Krewire's modular architecture, main.go remains an intentionally thin entry point that loads typed configuration, instantiates the application container, and initiates the HTTP runner:
// Thin entry point: load config, build app, run.
package main
import (
"fmt"
"os"
"storefront/internal/app"
"storefront/internal/config"
)
// Signature is the ecosystem watermark embedded into the binary at compile time via -ldflags.
var Signature = "build with krewire"
func main() {
meta, err := config.LoadMetadata("krewire.yaml")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
a, err := app.New(meta)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := a.Run(meta.Addr()); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Core Components & Architecture
1. Composition Root (internal/app/app.go)
The composition root wires all subsystem providers (configProvider, webProvider, http.Provider) through pkg/app:
package app
import (
"io/fs"
rvapp "github.com/krewire/krewire/pkg/app"
rvweb "github.com/krewire/krewire/pkg/web"
"storefront/assets"
"storefront/internal/config"
"storefront/internal/http"
"storefront/web/components"
"storefront/web/layouts"
"storefront/web/pages"
"storefront/web/theme"
)
// New assembles the full application and returns the ready *web.App
func New(meta *config.Metadata) (*rvweb.App, error) {
container, err := rvapp.NewApp(
&configProvider{meta: meta},
&webProvider{},
&http.Provider{},
).Build()
if err != nil {
return nil, err
}
return rvapp.Resolve[*rvweb.App](container)
}
2. HTTP Routing & API Handlers (internal/http/http.go)
HTTP API handlers bind input payloads, perform validation with pkg/validation, and mount onto the application router:
package http
import (
"net/http"
rvapp "github.com/krewire/krewire/pkg/app"
rvweb "github.com/krewire/krewire/pkg/web"
)
type Handler struct{}
func (h *Handler) Status(w http.ResponseWriter, _ *http.Request, _ rvweb.Params) {
rvweb.JSON(w, http.StatusOK, map[string]string{
"status": "ok",
"version": "0.1.0",
})
}
type Provider struct{}
func (p *Provider) Register(c *rvapp.Container) error {
return rvapp.Singleton[*Handler](c, func() *Handler { return &Handler{} })
}
func (p *Provider) Boot(c *rvapp.Container) error {
a, err := rvapp.Resolve[*rvweb.App](c)
if err != nil {
return err
}
h, err := rvapp.Resolve[*Handler](c)
if err != nil {
return err
}
a.Router().Get("/api/status", h.Status)
return nil
}
3. Server-Side Rendering (web/pages/pages.go)
Pages are registered with declarative web.PageSpec configurations supporting static data or dynamic per-request resolvers:
package pages
import (
"net/http"
"time"
"github.com/krewire/krewire/pkg/web"
)
func Register(a *web.App) {
a.Page(web.PageSpec{
Path: "/",
Title: "Storefront",
Layout: "Shell",
Root: "Hero",
Data: map[string]any{
"badge": "v0.1.0",
"title": "Welcome to Storefront",
"subtitle": "SSR, JSON API, and embedded assets from one Go binary.",
},
EmitProps: true,
Scripts: []string{"/static/app.js"},
})
a.Page(web.PageSpec{
Path: "/greet",
Title: "Greet",
Layout: "Shell",
Root: "Hero",
DataFunc: func(r *http.Request) (any, error) {
name := r.URL.Query().Get("name")
if name == "" {
name = "world"
}
return map[string]any{
"badge": time.Now().Format("15:04:05"),
"title": "Hello, " + name,
"subtitle": "This page is rendered per request.",
}, nil
},
})
}
Configuration (krewire.yaml)
Configure server ports, project metadata, and optional client WebAssembly targets in krewire.yaml:
project:
name: "storefront"
version: "0.1.0"
dev:
port: 8080
build:
output: ".krewire/build"
The runtime address can also be customized via the APP_ADDR environment variable or --addr CLI flag (e.g. krew run --addr :3000).
Development & Operations
Local Development (krew dev or krew run)
Start the application with live-recompilation on file changes:
krew dev
Or run the application directly:
krew run
Running Tests (krew test)
Execute package and integration tests:
krew test ./...
Building for Production (krew build)
Compile the production binary:
krew build
The compiled binary will be placed at .krewire/build/bin/storefront (or cross-compiled for any platform using standard Go env variables like GOOS=linux GOARCH=amd64).