Giter Club home page Giter Club logo

letmeserve's Introduction

a learning purpose project, not for production use

LetMeServe

A tiny abstraction around HttpServer (still in progress) Mostly for json requests

Provides simple usage for Router, Controller and Middleware Also a Json Validator

Example usage

  1. 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;
        ...
    }
}
  1. 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);
}
  1. Create the server
void main() {
    ...

    final server = LetMeServe(host: 'localhost', port: 3000);

    await server.listen(() => print('server running'));
}
  1. 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) { ... }
}
  1. Json Validator (Optional)
    final validator = JsonValidator()..isString('name', minLength: 3)..isInt('age', min: 18);

    final errors = validator.validate({'name': 'my name is', age: 26});

letmeserve's People

Contributors

spideythewebhead avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.