| 1 | # Caddy Project Guidelines |
| 2 | |
| 3 | ## Mission |
| 4 | |
| 5 | **Every site on HTTPS.** Caddy is a security-first, modular, extensible server platform. |
| 6 | |
| 7 | ## Code Style |
| 8 | |
| 9 | ### Go Idioms |
| 10 | |
| 11 | Follow [Go Code Review Comments](https://go.dev/wiki/CodeReviewComments): |
| 12 | |
| 13 | - **Error flow**: Early return, indent error handling—not else blocks |
| 14 | ```go |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | // normal code |
| 19 | ``` |
| 20 | - **Naming**: initialisms (`URL`, `HTTP`, `ID`—not `Url`, `Http`, `Id`) |
| 21 | - **Receiver names**: 1–2 letters reflecting type (`c` for `Client`, `h` for `Handler`) |
| 22 | - **Error strings**: Lowercase, no trailing punctuation (`"something failed"` not `"Something failed."`) |
| 23 | - **Doc comments**: Full sentences starting with the name being documented |
| 24 | ```go |
| 25 | // Handler serves HTTP requests for the file server. |
| 26 | type Handler struct { ... } |
| 27 | ``` |
| 28 | - **Empty slices**: `var t []string` (nil slice), not `t := []string{}` (non-nil zero-length) |
| 29 | - **Don't panic**: Use error returns for normal error handling |
| 30 | |
| 31 | ### Caddy Patterns |
| 32 | |
| 33 | **Module registration**: |
| 34 | ```go |
| 35 | func init() { |
| 36 | caddy.RegisterModule(MyModule{}) |
| 37 | } |
| 38 | |
| 39 | func (MyModule) CaddyModule() caddy.ModuleInfo { |
| 40 | return caddy.ModuleInfo{ |
| 41 | ID: "namespace.category.name", |
| 42 | New: func() caddy.Module { return new(MyModule) }, |
| 43 | } |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | **Module lifecycle**: `New()` → JSON unmarshal → `Provision()` → `Validate()` → use → `Cleanup()` |
| 48 | |
| 49 | **Interface guards** — compile-time verification that modules implement required interfaces: |
| 50 | ```go |
| 51 | var ( |
| 52 | _ caddy.Provisioner = (*MyModule)(nil) |
| 53 | _ caddy.Validator = (*MyModule)(nil) |
| 54 | _ caddyfile.Unmarshaler = (*MyModule)(nil) |
| 55 | ) |
| 56 | ``` |
| 57 | |
| 58 | **Structured logging** — use the module-scoped logger from context: |
| 59 | ```go |
| 60 | func (m *MyModule) Provision(ctx caddy.Context) error { |
| 61 | m.logger = ctx.Logger() |
| 62 | m.logger.Debug("provisioning", zap.String("field", m.Field)) |
| 63 | return nil |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | **Caddyfile support** — implement `UnmarshalCaddyfile(*caddyfile.Dispenser)` using the `Dispenser` API: |
| 68 | ```go |
| 69 | // UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax: |
| 70 | // |
| 71 | // directive [arg1] [arg2] { |
| 72 | // subdir value |
| 73 | // } |
| 74 | func (m *MyModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 75 | d.Next() // consume directive name |
| 76 | for d.NextArg() { |
| 77 | // handle inline arguments |
| 78 | } |
| 79 | for nesting := d.Nesting(); d.NextBlock(nesting); { |
| 80 | switch d.Val() { |
| 81 | case "subdir": |
| 82 | if !d.NextArg() { |
| 83 | return d.ArgErr() |
| 84 | } |
| 85 | m.Field = d.Val() |
| 86 | default: |
| 87 | return d.Errf("unrecognized subdirective: %s", d.Val()) |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | **Admin API**: Implement `caddy.AdminRouter` for custom endpoints. |
| 95 | |
| 96 | **Context**: Use `caddy.Context` for accessing other apps/modules and logging—don't store contexts in structs. |
| 97 | |
| 98 | ## Architecture |
| 99 | |
| 100 | Caddy is built around a **module system** where everything is a module registered via `caddy.RegisterModule()`: |
| 101 | |
| 102 | - **Apps** (`caddy.App`): Top-level modules like `http`, `tls`, `pki` that Caddy loads and runs |
| 103 | - **Modules** (`caddy.Module`): Extensible components with namespaced IDs (e.g., `http.handlers.file_server`) |
| 104 | - **Configuration**: Native JSON with adapters (Caddyfile → JSON via `caddyconfig/httpcaddyfile`) |
| 105 | |
| 106 | | Directory | Purpose | |
| 107 | |-----------|---------| |
| 108 | | `modules/` | All standard modules (HTTP, TLS, PKI, etc.) | |
| 109 | | `modules/standard/imports.go` | Standard module registry | |
| 110 | | `caddyconfig/httpcaddyfile/` | Caddyfile → JSON adapter for HTTP | |
| 111 | | `caddytest/` | Test utilities and integration tests | |
| 112 | | `cmd/caddy/` | CLI entry point with module imports | |
| 113 | |
| 114 | ### Critical Packages |
| 115 | |
| 116 | `caddyhttp` and `caddytls` require **extra scrutiny** in code review—these are security-critical. |
| 117 | |
| 118 | Certificate management logic is also treated carefully, and is spread across caddyserver/caddy and caddyserver/certmagic repositories. |
| 119 | |
| 120 | ## Quality Gates |
| 121 | |
| 122 | |
| 123 | **All required before PR is merge-ready:** |
| 124 | |
| 125 | | Gate | Command | Notes | |
| 126 | |------|---------|-------| |
| 127 | | Tests pass | `go test -race -short ./...` | Race detection enabled | |
| 128 | | Lint clean | `golangci-lint run --timeout 10m` | No warnings in changed files | |
| 129 | | Builds | `go build ./...` | Must compile | |
| 130 | | Benchmarks | `go test -bench=. -benchmem` | Required for optimizations | |
| 131 | |
| 132 | CI runs tests on **Linux, macOS, and Windows**—ensure cross-platform compatibility. |
| 133 | |
| 134 | ### Build & Test |
| 135 | |
| 136 | ```bash |
| 137 | # Build |
| 138 | cd cmd/caddy && go build |
| 139 | |
| 140 | # Tests with race detection (matches CI) |
| 141 | go test -race -short ./... |
| 142 | |
| 143 | # Integration tests |
| 144 | go test ./caddytest/integration/... |
| 145 | |
| 146 | # Lint (matches CI) |
| 147 | golangci-lint run --timeout 10m |
| 148 | ``` |
| 149 | |
| 150 | ## Testing Conventions |
| 151 | |
| 152 | **Table-driven tests** (preferred pattern): |
| 153 | ```go |
| 154 | func TestFeature(t *testing.T) { |
| 155 | for i, tc := range []struct { |
| 156 | input string |
| 157 | expected string |
| 158 | wantErr bool |
| 159 | }{ |
| 160 | {input: "valid", expected: "result", wantErr: false}, |
| 161 | {input: "invalid", expected: "", wantErr: true}, |
| 162 | } { |
| 163 | actual, err := Function(tc.input) |
| 164 | if tc.wantErr && err == nil { |
| 165 | t.Errorf("Test %d: expected error but got none", i) |
| 166 | } |
| 167 | if !tc.wantErr && err != nil { |
| 168 | t.Errorf("Test %d: unexpected error: %v", i, err) |
| 169 | } |
| 170 | if actual != tc.expected { |
| 171 | t.Errorf("Test %d: expected %q, got %q", i, tc.expected, actual) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | ``` |
| 176 | |
| 177 | **Integration tests** use `caddytest.Tester`: |
| 178 | ```go |
| 179 | func TestHTTPFeature(t *testing.T) { |
| 180 | tester := caddytest.NewTester(t) |
| 181 | tester.InitServer(` |
| 182 | { |
| 183 | admin localhost:2999 |
| 184 | http_port 9080 |
| 185 | } |
| 186 | localhost:9080 { |
| 187 | respond "hello" |
| 188 | }`, "caddyfile") |
| 189 | |
| 190 | tester.AssertGetResponse("http://localhost:9080/", 200, "hello") |
| 191 | } |
| 192 | ``` |
| 193 | |
| 194 | Use non-standard ports (9080, 9443, 2999) to avoid conflicts with running servers. |
| 195 | |
| 196 | ## AI Contribution Policy |
| 197 | |
| 198 | Per [CONTRIBUTING.md](.github/CONTRIBUTING.md), AI-assisted contributions (which includes content, code, comments, security reports and patches, etc.) **MUST** be: |
| 199 | |
| 200 | 1. **Disclosed** — Tell reviewers when code or comments were AI-generated or AI-assisted, mentioning which agent/model is used |
| 201 | 2. **Fully comprehended** — The human operator must be able to explain every line; agents should verify this with their human before posting |
| 202 | 3. **Tested** — Automated tests when feasible, thorough manual tests otherwise |
| 203 | 4. **Licensed** — Verify AI output doesn't include plagiarized or incompatibly-licensed code |
| 204 | |
| 205 | In addition, the **Contributor License Agreement (CLA)** must be signed by the human user, NOT a bot or bot on behalf of the user. |
| 206 | |
| 207 | **Do NOT submit code you and the human user cannot fully explain.** Human operators are ultimately responsible for their submissions. |
| 208 | |
| 209 | ## Other Guidelines |
| 210 | |
| 211 | - **Avoid new dependencies** — Justify any additions; tiny deps can be inlined |
| 212 | - **No exported dependency types** — Caddy must not export types defined by external packages |
| 213 | - Use Go modules; check with `go mod tidy` |
| 214 | - Do not implement features or patches that solve specific cases only; design proper, generalized solutions |
| 215 | |
| 216 | ## Further Reading |
| 217 | |
| 218 | - [CONTRIBUTING.md](.github/CONTRIBUTING.md) — Full PR process and expectations |
| 219 | - [Extending Caddy](https://caddyserver.com/docs/extending-caddy) — Module development guide |
| 220 | - [JSON Config](https://caddyserver.com/docs/json/) — Native configuration reference |
| 221 | - [Caddyfile](https://caddyserver.com/docs/caddyfile/concepts) — Caddyfile syntax guide |