|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +Cosmos is a Go monorepo with four HTTP modules: contract (interfaces), router (HTTP routing), problem (RFC 9457 errors), framework (complete framework). Go 1.25.0 workspace with replace directives. |
| 6 | + |
| 7 | +Dependency hierarchy: contract (zero deps) → router/problem (standalone) → framework (uses all) |
| 8 | + |
| 9 | +## Setup Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Verify workspace and run all tests |
| 13 | +go work sync && go test ./... |
| 14 | + |
| 15 | +# Test specific module |
| 16 | +go test ./contract/... |
| 17 | +go test ./router/... |
| 18 | +go test ./problem/... |
| 19 | +go test ./framework/... |
| 20 | + |
| 21 | +# Coverage and formatting |
| 22 | +go test -cover ./... |
| 23 | +go fmt ./... |
| 24 | +go vet ./... |
| 25 | + |
| 26 | +# Generate mocks (contract only) |
| 27 | +cd contract && go generate ./... |
| 28 | +``` |
| 29 | + |
| 30 | +## Module Navigation |
| 31 | + |
| 32 | +Always run tests from workspace root for proper module resolution. Each module has its own go.mod. Use full import paths: `github.com/studiolambda/cosmos/contract` |
| 33 | + |
| 34 | +## Code Style |
| 35 | + |
| 36 | +- Standard Go formatting with gofmt |
| 37 | +- Descriptive names, early returns, single-purpose functions |
| 38 | +- Doc comments starting with name being documented |
| 39 | +- Always check errors with errors.Is() and errors.As() |
| 40 | +- Table-driven tests with testify assertions |
| 41 | + |
| 42 | +### Framework Patterns |
| 43 | + |
| 44 | +Error-returning handlers: |
| 45 | +```go |
| 46 | +func handler(w http.ResponseWriter, r *http.Request) error { |
| 47 | + return response.JSON(w, http.StatusOK, data) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Middleware composition: |
| 52 | +```go |
| 53 | +func MyMiddleware() framework.Middleware { |
| 54 | + return func(next framework.Handler) framework.Handler { |
| 55 | + return func(w http.ResponseWriter, r *http.Request) error { |
| 56 | + err := next(w, r) |
| 57 | + return err |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Common Patterns |
| 64 | + |
| 65 | +Router: |
| 66 | +```go |
| 67 | +r := router.New[http.Handler]() |
| 68 | +r.Use(middleware) |
| 69 | +r.Get("/users/{id}", handler) |
| 70 | +``` |
| 71 | + |
| 72 | +Problem Details: |
| 73 | +```go |
| 74 | +ErrNotFound.With("resource_id", id).ServeHTTP(w, r) |
| 75 | +``` |
| 76 | + |
| 77 | +Cache: |
| 78 | +```go |
| 79 | +cache.Remember(ctx, key, ttl, func() (any, error) { |
| 80 | + return computeValue(), nil |
| 81 | +}) |
| 82 | +``` |
| 83 | + |
| 84 | +Database transactions: |
| 85 | +```go |
| 86 | +db.WithTransaction(ctx, func(tx contract.Database) error { |
| 87 | + return tx.Exec(ctx, query, args...) |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +Sessions: |
| 92 | +```go |
| 93 | +session := request.Session(r) |
| 94 | +session.Put("user_id", 123) |
| 95 | +session.Regenerate() // After auth |
| 96 | +``` |
| 97 | + |
| 98 | +## Common Gotchas |
| 99 | + |
| 100 | +- Framework handlers return errors, stdlib handlers don't |
| 101 | +- Middleware order matters: logger/recover first |
| 102 | +- Session middleware required before accessing sessions |
| 103 | +- Test from workspace root, not module directories |
| 104 | +- Database transactions cannot be nested |
| 105 | +- Problem methods return new instances (immutable) |
| 106 | +- Router generic type parameter required: `router.New[http.Handler]()` |
| 107 | + |
| 108 | +## Security |
| 109 | + |
| 110 | +- Use middleware.CSRF() for state-changing endpoints |
| 111 | +- Prefer Argon2 for password hashing |
| 112 | +- Use AES-GCM or ChaCha20-Poly1305 (authenticated encryption) |
| 113 | +- Regenerate sessions after authentication |
| 114 | +- Never log sensitive data |
| 115 | + |
| 116 | +## File Locations |
| 117 | + |
| 118 | +- Workspace: go.work |
| 119 | +- Module configs: */go.mod |
| 120 | +- Mock config: contract/.mockery.yml |
| 121 | +- CI workflows: .github/workflows/*.yml |
| 122 | +- Contracts: contract/*.go |
| 123 | +- Framework core: framework/handler.go, framework/framework.go |
| 124 | +- Router: router/router.go |
| 125 | +- Problem: problem/problem.go |
0 commit comments