Fennec is a dart web framework with the principal goal: make web server side more easy and fast to develop.
Packages | pub |
---|---|
fennec (core framework) | fennec |
fennec_pg | fennec_pg |
fennec_jwt | fennec_jwt |
- create a simple dart project. you can use terminal for that dart create 'projectname'
- install the framework from pub.dev
create your First Router:
class TestRouter extends Router {
@override
String getRoutePath() {
return '/Test';
}
}
create your First Controller:
class TestController {
void test(Request request, Response response) {
response.ok().send('hellow world');
}
}
call your Router and Controller
TestRouter testRouter = TestRouter();
testRouter.get(
path: '/simple', requestHandler: TestController().test, middlewares: []);
testRouter.get(
path: '/simple1',
requestHandler: (Request req, Response res) {
res.send(req.body);
},
middlewares: []);
applicationCofiguration.addRouter(testRouter);
applicationCofiguration.addRoute(Route(
path: '/show',
requestMethod: RequestMethod.get(),
requestHandler: (Request req, Response res) {
res.ok().send('show received');
},
middlewares: []));
it must be a typedef MiddlewareHandler and must return always MiddleWareResponse. here an example how to implement it:
Future<MiddleWareResponse> testMiddleware(Request req, Response res) async {
res.html("You are not allowed to do that");
return MiddleWareResponse(MiddleWareResponseEnum.stop);
}
here is an example hot to use dynamic routes
Future dynamicRoutes(Request request, Response response) async {
response.json({
'userId': request.pathParams!['user_id'],
'docId': request.pathParams!['doc_id']
});
}
an example how to handle files
Future fileSystems(Request request, Response response) async {
response.json({
'file1': request.files.first.toString(),
});
}
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'});
Fennec Framework supports also Multithreading over isolates. To increate the number of used isolates just call the function setNumberOfIsolates. the default number of isolates is 1
example
applicationCofiguration.setNumberOfIsolates(1);
import 'package:fennec/fennec.dart';
import 'package:path/path.dart' as path;
import 'test.dart';
void main(List<String> arguments) async {
ApplicationConfiguration applicationCofiguration = ApplicationConfiguration();
applicationCofiguration
.addControllers([Test])
.setPort(8000)
.setHost(InternetAddress.loopbackIPv4);
Application application = Application(applicationCofiguration);
Server server = Server(application);
await server.startServer();
}
-
heroku cloud: here is heroku-buildpack for dart and inside it an example how to deploy Fennec Framework on heroku cloud for free. to test an example you can try this two endpoints:
-
for next days, another example for aws and goolge run will be uploaded.
Fennec Framework after with version >= 1.0.0 doesn't support more annotations because the big discussions about the library mirrors. as alernatives you can use Route or Route as showed in this example.