How jhonacodes/api_kit works
Project Type and Purpose
• This is not a monolithic application but an open‐source Dart framework (“api_kit”) for building production-ready REST APIs.
• It provides annotation-based routing, a middleware pipeline, JWT authentication/authorization, parameter injection, a result pattern for responses, structured logging, and security features.
High-Level Architectural Layers
• Annotation Layer (lib/src/annotations)
• Core Server Layer (lib/src/core)
• Middleware & Security Layer (lib/src/middleware & lib/src/security)
• Parameter Injection & Validation Layer (lib/src/validators + annotation visitor)
• Models & Utilities (lib/src/models, config, logger_rs)
• Consumer-written Controllers & Business Logic (user code)
Main Components
a. ApiServer
– Entry point: instantiates and configures HTTP server (SecureServer wraps Dart’s HttpServer).
– Reads ServerConfig and SecurityConfig.
b. Annotation Processor & Registry
– AnnotationVisitor, Detector scan code for @RestController, @Get/@Post, JWT annotations.
– ControllerRegistry holds discovered controllers and endpoints.
c. RouterBuilder & Dispatcher
– Builds routing table (static and dynamic routers).
– MethodDispatcher invokes the correct controller method.
d. Middleware Pipeline
– MiddlewareRegistry chains enhanced_auth_middleware, security/middleware, rate_limiter, custom middlewares.
– Each middleware can short-circuit or forward the request.
e. JWT Integration
– JwtIntegration hooks into pipeline; validates tokens via JWTValidatorBase implementations (MyAdminValidator, MyFinancialValidator, etc.).
– Token blacklisting, exclusion paths.
f. Parameter Injection Engine
– Injects @QueryParam, @PathParam, @RequestBody, @RequestHeader, @RequestContext values directly into method parameters.
g. Result Pattern & Response Model
– ApiResponse models, ApiKit.ok/err wrapper, transforms to HTTP response.
h. Logging & Security Headers
– logger_rs for structured logs; built-in security HTTP headers.
1. HTTP client → SecureServer (HTTP listener)
2. SecureServer → Global middleware chain (MiddlewareRegistry)
3. Middleware → JWT Integration → Rate Limiter → etc.
4. MiddlewareRegistry → RouterBuilder (match path + method)
5. RouterBuilder → Parameter Injection Engine
6. Parameter Injection Engine → Controller Method (business logic)
7. Controller Method → ApiKit.ok/err → Response Builder
8. Response Builder → SecureServer → HTTP client
Architectural Patterns & Principles
• Inversion of Control via annotations.
• Chain of Responsibility for middleware.
• Strategy pattern for pluggable JWT validators.
• Modular layering for separation of concerns.