craftgo

module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: MIT

README ¶

craftgo

Go Report Card

Write the spec. Generate everything.

craftgo is a design-first framework for Go HTTP services. You describe your API once in a small DSL — types, validators, endpoints, errors — and craftgo gen produces typed structs, request validation, HTTP handlers, route wiring, and an OpenAPI 3.1 spec. The generated code is plain net/http: no custom router, no reflection, no runtime struct tags. It reads like code you would have written by hand.

📖 Documentation · 🤖 AI-ready reference (llms.md)


Quickstart

Requires Go 1.24+.

# 1. Install the CLI
go install github.com/craftgodotdev/craftgo/cmd/craftgo@latest

# 2. New project
mkdir hello && cd hello
go mod init example.com/hello
go get github.com/craftgodotdev/craftgo
craftgo init design

Write design/users/service.craftgo:

package design

type CreateUserReq {
    name  string @length(1, 80)
    email string @format(email)
    age   int?   @gte(0) @lte(150)
}

type User {
    id    string
    name  string
    email string
}

@prefix("/v1")
service UserService {
    post CreateUser /users {
        request  CreateUserReq
        response User
    }
}

Generate, then fill the one logic stub:

craftgo gen design
// internal/service/user-service/create-user.go  (the only file you edit)
func (l *CreateUserService) CreateUser(req *types.CreateUserReq) (*types.User, error) {
    return &types.User{ID: "u1", Name: req.Name, Email: req.Email}, nil
}
go run .
# listening on :8080 (api)
curl -X POST localhost:8080/api/v1/users \
  -H 'Content-Type: application/json' \
  -d '{"name":"","email":"nope"}'
# name: length out of range [1, 80]

Validation ran with zero hand-written code. The handler decoded JSON, called req.Validate(), dispatched to your function, and encoded the response.

What you get

  • One source of truth — types, validators, handlers, routes, and the OpenAPI spec all come from the same .craftgo files. Change the DSL, regenerate, done.
  • Plain net/http — generated handlers are http.HandlerFunc registered on *http.ServeMux. Middleware is func(http.Handler) http.Handler. Nothing to learn beyond the standard library.
  • Declarative validation — @length, @format(email), @gte, @pattern, @requiresOneOf, … compile to plain Go if statements. No reflection, no runtime tags.
  • OpenAPI 3.1 — emitted from the same source, renders in Swagger UI, feeds openapi-generator for clients in any language.
  • Rich type system — scalars with inherited validators, enums, generics (Page<User>), cross-package composition, mixins, typed error categories.
  • First-class tooling — an LSP server (completion, hover, go-to-definition, live diagnostics, formatting) and a VS Code extension.
  • Regenerate-safe — your business logic lives in gen-once stubs the CLI never overwrites; everything else regenerates on every craftgo gen.

How it fits together

design/*.craftgo  ──craftgo gen──▶  internal/types/      typed structs + Validate()
                                    internal/transport/  HTTP handlers
                                    internal/routes/      route registration
                                    internal/service/     logic stubs (you edit these)
                                    docs/openapi.yaml      OpenAPI 3.1 spec
                                    main.go                wired entry point

Documentation

Getting Started Build and run your first endpoint in 5 minutes
DSL Basics The full syntax: types, services, decorators
Decorator Registry Every decorator, its arguments, and where it applies
Runtime API pkg/server — the net/http wrapper your code runs on
Codegen Output Exactly what craftgo gen produces, file by file
llms.md Single-page reference built for pasting into an LLM prompt

License

See LICENSE.

Directories ¶

Path Synopsis
cmd
craftgo command
craftgo gen subcommand: design parse, semantic analysis, per-package + project-wide codegen.
craftgo gen subcommand: design parse, semantic analysis, per-package + project-wide codegen.
craftgo-lsp command
Command craftgo-lsp implements a Language Server Protocol server for the CraftGo DSL.
Command craftgo-lsp implements a Language Server Protocol server for the CraftGo DSL.
internal
ast
Package ast defines the abstract syntax tree produced by the parser.
Package ast defines the abstract syntax tree produced by the parser.
bench
Package bench houses bind-path microbenchmarks.
Package bench houses bind-path microbenchmarks.
codegen
OpenAPI codegen uses `github.com/getkin/kin-openapi` to build a strongly- typed openapi3.T document and `sigs.k8s.io/yaml` to render it as YAML.
OpenAPI codegen uses `github.com/getkin/kin-openapi` to build a strongly- typed openapi3.T document and `sigs.k8s.io/yaml` to render it as YAML.
config
Package config loads and validates `craftgo.design.yaml`, the project manifest read by every CLI command.
Package config loads and validates `craftgo.design.yaml`, the project manifest read by every CLI command.
errcat
Package errcat holds the canonical catalogue of reserved HTTP error categories.
Package errcat holds the canonical catalogue of reserved HTTP error categories.
format
Package format renders an ast.File back to canonical CraftGo source.
Package format renders an ast.File back to canonical CraftGo source.
idents
Package idents holds the Go-identifier conversion helpers used by both the semantic analyser and the codegen pass.
Package idents holds the Go-identifier conversion helpers used by both the semantic analyser and the codegen pass.
lexer
Package lexer tokenizes craftgo DSL source files.
Package lexer tokenizes craftgo DSL source files.
lsp
Decorator-arg/name LSP completions: dispatcher, context detection, error categories.
Decorator-arg/name LSP completions: dispatcher, context detection, error categories.
parser
Package parser turns a craftgo source buffer into an ast.File.
Package parser turns a craftgo source buffer into an ast.File.
route
Package route is the leaf authority for craftgo's HTTP routes: how a method's final route is assembled (base path + @prefix + method path), the string form of a DSL path, the shape key two colliding routes share, and net/http's pattern-overlap rule.
Package route is the leaf authority for craftgo's HTTP routes: how a method's final route is assembled (base path + @prefix + method path), the string form of a DSL path, the shape key two colliding routes share, and net/http's pattern-overlap rule.
semantic
Package semantic — @default literal validation: type/element support, primitive-kind map, helpers.
Package semantic — @default literal validation: type/element support, primitive-kind map, helpers.
strfmt
Package strfmt holds the canonical catalogue of named string formats the `@format` decorator accepts.
Package strfmt holds the canonical catalogue of named string formats the `@format` decorator accepts.
wire
Package wire is the leaf vocabulary of craftgo's wire bindings: where a field's value rides (path / query / header / cookie / form / body / sensitive), how its wire name is derived, and the request auto-binding rule.
Package wire is the leaf vocabulary of craftgo's wire bindings: where a field's value rides (path / query / header / cookie / form / body / sensitive), how its wire name is derived, and the request auto-binding rule.
pkg
log
Package log defines the Logger interface used across craftgo together with a default zap adapter.
Package log defines the Logger interface used across craftgo together with a default zap adapter.
metrics
Package metrics is the OpenTelemetry-backed metrics surface for craftgo runtimes.
Package metrics is the OpenTelemetry-backed metrics surface for craftgo runtimes.
otel
Package otel exposes the OpenTelemetry HTTP middleware + tracer bootstrap helpers for craftgo runtimes.
Package otel exposes the OpenTelemetry HTTP middleware + tracer bootstrap helpers for craftgo runtimes.
server
Package server is the thin `net/http` wrapper that craftgo's generated routes register against.
Package server is the thin `net/http` wrapper that craftgo's generated routes register against.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL