|
| 1 | +# Zentrox Simplification Changes |
| 2 | + |
| 3 | +This document summarizes the major simplifications made to make Zentrox easier to integrate into other projects. |
| 4 | + |
| 5 | +## Summary |
| 6 | + |
| 7 | +Zentrox has been streamlined to be minimal, easy to understand, and simple to integrate with existing projects. The focus is on providing essential functionality while allowing developers to customize and extend as needed. |
| 8 | + |
| 9 | +## Major Changes |
| 10 | + |
| 11 | +### 1. Middleware System Simplified |
| 12 | + |
| 13 | +**Before:** Middleware required calling `c.Forward()` to pass control |
| 14 | +```go |
| 15 | +func MyMiddleware() zentrox.Handler { |
| 16 | + return func(c *zentrox.Context) { |
| 17 | + // do something |
| 18 | + c.Forward() // Required! |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +**After:** Middleware uses `c.Next()` (automatically called if not explicitly invoked) |
| 24 | +```go |
| 25 | +func MyMiddleware() zentrox.Handler { |
| 26 | + return func(c *zentrox.Context) { |
| 27 | + // do something |
| 28 | + c.Next() // Optional in many cases |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### 2. JWT Middleware Simplified |
| 34 | + |
| 35 | +**Before:** Complex configuration with many fields |
| 36 | +```go |
| 37 | +middleware.JWT(middleware.JWTConfig{ |
| 38 | + Secret: secret, |
| 39 | + RequireExp: true, |
| 40 | + RequireNbf: false, |
| 41 | + Issuer: "https://example.com", |
| 42 | + Audience: "api://myapp", |
| 43 | + ClockSkew: 60 * time.Second, |
| 44 | + AllowedAlgs: []string{"HS256"}, |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +**After:** Simple config with custom validation callback |
| 49 | +```go |
| 50 | +middleware.JWT(middleware.JWTConfig{ |
| 51 | + Secret: secret, |
| 52 | + ValidateFunc: func(claims map[string]any) error { |
| 53 | + // Custom validation logic |
| 54 | + if exp, ok := claims["exp"].(float64); ok { |
| 55 | + if time.Now().Unix() > int64(exp) { |
| 56 | + return errors.New("token expired") |
| 57 | + } |
| 58 | + } |
| 59 | + return nil |
| 60 | + }, |
| 61 | +}) |
| 62 | +``` |
| 63 | + |
| 64 | +### 3. CORS Simplified |
| 65 | + |
| 66 | +**Before:** Two separate middlewares (CORS and StrictCORS) |
| 67 | +```go |
| 68 | +app.Plug(middleware.CORS(...)) |
| 69 | +app.Plug(middleware.StrictCORS(...)) // separate middleware |
| 70 | +``` |
| 71 | + |
| 72 | +**After:** Single CORS middleware |
| 73 | +```go |
| 74 | +app.Plug(middleware.CORS(middleware.CORSConfig{ |
| 75 | + AllowOrigins: []string{"*"}, |
| 76 | + AllowMethods: []string{"GET", "POST", "PUT", "DELETE"}, |
| 77 | + AllowHeaders: []string{"*"}, |
| 78 | +})) |
| 79 | +``` |
| 80 | + |
| 81 | +### 4. Logger Supports Custom Integration |
| 82 | + |
| 83 | +**Before:** Only built-in logger |
| 84 | +```go |
| 85 | +app.Plug(middleware.Logger()) |
| 86 | +``` |
| 87 | + |
| 88 | +**After:** Easy custom logger integration |
| 89 | +```go |
| 90 | +// Use your existing logger (zap, logrus, etc.) |
| 91 | +app.Plug(middleware.LoggerWithFunc(func(method, path string, status int, dur time.Duration, err error) { |
| 92 | + myLogger.Info("request", "method", method, "path", path, "status", status) |
| 93 | +})) |
| 94 | +``` |
| 95 | + |
| 96 | +### 5. Removed Non-Essential Middleware |
| 97 | + |
| 98 | +The following middleware was removed to keep the framework minimal: |
| 99 | +- **AccessLog** - Use LoggerWithFunc instead |
| 100 | +- **Metrics** - Use your own metrics library |
| 101 | +- **SimpleTrace** - Use your own tracing solution |
| 102 | +- **Timeout** - Implement at handler level if needed |
| 103 | +- **RequestID** - Implement as custom middleware if needed |
| 104 | +- **Helpers** - Functionality moved to core where needed |
| 105 | + |
| 106 | +### 6. Examples Over Documentation |
| 107 | + |
| 108 | +All features are demonstrated in the `examples/` directory with practical, working code: |
| 109 | +- `minimal/` - Basic setup with custom logger |
| 110 | +- `jwt_custom/` - Custom JWT validation |
| 111 | +- `basic/` - Simple routing |
| 112 | +- `binding/` - Request binding |
| 113 | +- `gzip/` - Compression |
| 114 | +- `graceful/` - Graceful shutdown |
| 115 | +- And more... |
| 116 | + |
| 117 | +## Kept Middleware (Essential) |
| 118 | + |
| 119 | +These middleware remain because they provide core functionality: |
| 120 | +- **Logger** - Request logging with custom function support |
| 121 | +- **Recovery** - Panic recovery |
| 122 | +- **ErrorHandler** - Standardized error responses |
| 123 | +- **JWT** - Token authentication |
| 124 | +- **CORS** - Cross-origin requests |
| 125 | +- **Gzip** - Response compression |
| 126 | + |
| 127 | +## Migration Guide |
| 128 | + |
| 129 | +### If you were using c.Forward() |
| 130 | +Replace all `c.Forward()` calls with `c.Next()` |
| 131 | + |
| 132 | +### If you were using complex JWT config |
| 133 | +Replace with ValidateFunc: |
| 134 | +```go |
| 135 | +ValidateFunc: func(claims map[string]any) error { |
| 136 | + // Your validation logic here |
| 137 | + return nil |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### If you were using deleted middleware |
| 142 | +- AccessLog → Use `middleware.LoggerWithFunc(yourLogger)` |
| 143 | +- Metrics → Use Prometheus or your metrics library directly |
| 144 | +- RequestID → Create custom middleware if needed |
| 145 | +- Timeout → Use `http.TimeoutHandler` or context timeout |
| 146 | + |
| 147 | +## Benefits |
| 148 | + |
| 149 | +1. **Smaller codebase** - Removed ~500 lines of middleware code |
| 150 | +2. **Easier to learn** - Familiar patterns from popular frameworks |
| 151 | +3. **Flexible** - Custom validators instead of rigid configs |
| 152 | +4. **Integrates easily** - Works with existing logger/metrics/tracing |
| 153 | +5. **Practical examples** - Learn from working code, not docs |
| 154 | + |
| 155 | +## Philosophy |
| 156 | + |
| 157 | +Zentrox now follows these principles: |
| 158 | +- **Minimal core** - Essential features only |
| 159 | +- **Easy integration** - Works with your existing tools |
| 160 | +- **Examples first** - Show, don't tell |
| 161 | +- **No magic** - Clear, understandable code |
| 162 | +- **Extensible** - Easy to add custom middleware |
| 163 | + |
| 164 | +## Testing |
| 165 | + |
| 166 | +All changes are covered by tests: |
| 167 | +```bash |
| 168 | +go test ./... |
| 169 | +``` |
| 170 | + |
| 171 | +All examples can be run: |
| 172 | +```bash |
| 173 | +go run examples/minimal/main.go |
| 174 | +go run examples/jwt_custom/main.go |
| 175 | +# etc. |
| 176 | +``` |
0 commit comments