Skip to content

Fennec-Framework/fennec

 
 

Repository files navigation

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_pg fennec_pg
fennec_jwt fennec_jwt

installation:

  1. create a simple dart project. you can use terminal for that dart create 'projectname'
  2. install the framework from pub.dev

make your first request:

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: []));

Middleware

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);
  }

dynamic routes

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']
    });
  }

File System Routing

an example how to handle files

Future fileSystems(Request request, Response response) async {
    response.json({
      'file1': request.files.first.toString(),
    });
}

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'});

Multithreading

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);

Start your Server and test your first Request

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();
}

deploy

import information

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.

License

MIT