Fennec is a dart web framework with the principal goal: make web server side more easy and fast to develop.
Packages
Packages | pub |
---|---|
fennec (core framework) | fennec |
fennec_jwt | fennec_jwt |
installation:
- create a simple dart project. you can use terminal for that dart create ‘projectname’
- install the framework from pub.dev
make your first request:
create your First RestController:
@RestController(path: '/example')
class Test {
@Route('/test', RequestMethod.get())
Future test(Request request, Response response) async {
response.send('hello world');
}
@AuthenticatedRoute('/template', RequestMethod.get(), MiddlewareHanlderImpl())
Future testMiddleWare(Request request, Response response) async {
response.render('template.html');
}
@AuthorizatedRoute('/authorization', RequestMethod.get(),
MiddlewareHanlderImpl(), ['ADMIN'], UserProviderImpl())
Future testAuthorization(Request request, Response response) async {
response.render('template.html');
}
}
Fennec Framework has three types of Routing:
- Route is simple Route without any post executions or middlewares before the request will be executed.
- AuthenticatedRoute is a Route that required also a milldware class where the deveveloper can define all Operations and Middlwares (for example authenticate the token) that must be executed before the request will be executed.
- AuthorizatedRoute is a Route that required also a milldware and class for Authorizate the user and a List of Roles of user that can use this request.
this Route will typically used for softwares with many roles of users.
Middleware
it must be a subtype of the class MiddlewareHandler , here an example how to implement it:
every Middlware inside the class must be annotated with @Middleware() and have this signature Future ‘methodname'(Request request, Response response). the middlware can also without async implemented. priority is for determining which Middlware should be firtly executed.
higher Priority will be first executed.
UserProvider
UserProvider is an interface that contains the function loadUser based and the request data and Roles used on the AuthorizatedRoute
Example of implementation of UserProvider
WebSocket
WebSocket is already integrated in the core of Framework.
how to use it :
WebSocketHandler webSocketHandler = WebSocketHandler();
webSocketHandler.registerWebSocketHandler(server);
webSocketHandler.clientsListener.stream.listen((event) {
if (event.headers!.value('token') != null) {
webSocketHandler.addClient(event);
} else {
event.webSocket.addError('not allowed');
}
});
//Send data to all registred Clients
webSocketHandler.sendToAllJson({'key': 'value'});