a learning purpose project, not for production use
A tiny abstraction around HttpServer (still in progress) Mostly for json requests
Provides simple usage for Router, Controller and Middleware Also a Json Validator
- Create a controller
class TodosControllerV0 implements Controller {
Future<Response> getTodoById(Request request) async {
final id = request.params['id'];
...
}
Future<Response> createTodo(Request request) async {
final body = request.body;
...
}
}
- Create a router than forwards the requests to a controller
class TodosRouterV0 extends Router {
final TodosControllerV0 controller;
TodosRouterV0(
this.controller,
): super(prefix: '/api/v0');
@Route.get('/todo/:id')
Future<Response> getTodoById(Request request) => controller.getTodoById(request);
@Route.post('/todo') createTodo(Request request) => controller.createTodo(request);
}
- Create the server
void main() {
...
final server = LetMeServe(host: 'localhost', port: 3000);
await server.listen(() => print('server running'));
}
- Add middlware (Optional)
class LogMiddleware extends Middleware {
@override
Future<Request?> onRequest(Request request, void Function(Response response) abort) {
}
@override
Future<Response> onResponse(Response response) {
}
}
class AuthMiddlware implements Middlware { ... }
/// applies to all methods annotated with @Route.[method]
@AddMiddlware(LogMiddleware)
class MyRouter extends Router {
/// applies to
@AddMiddlware(AuthMiddlware)
@Route.get('/path/to/my/route')
Future<Response> myRoute(Request request) { ... }
}
- Json Validator (Optional)
final validator = JsonValidator()..isString('name', minLength: 3)..isInt('age', min: 18);
final errors = validator.validate({'name': 'my name is', age: 26});