fennec 2.1.5 copy "fennec: ^2.1.5" to clipboard
fennec: ^2.1.5 copied to clipboard

Fennec is a multi-threaded, robust Dart Server-Side Framework.

example/example.dart

import 'dart:async';

import 'package:fennec/fennec.dart';

import 'package:path/path.dart' as path;

void main(List<String> arguments) async {
  Application application = Application();

  application.setNumberOfIsolates(1);
  application.useWebSocket(true);
  application.setPort(8000);
  application.addRouters([testRouter()]);
  application.addActor(CustomisedActor("customizedActor"));
  application.setViewPath('${path.current}/example');
  application.handleExceptionsAndErrors({
    ExceptionAndErrorResponse<FennecException, FennecError>(404,
        message: 'Error with 404'),
    ExceptionAndErrorResponse<FennecException, CustomFennecError>(400,
        message: 'Error with 400')
  });

  ServerInfo serverInfo = await application.runServer();
  print("Server is running at Port ${serverInfo.port}");
}

class CustomFennecError extends FennecError {}

Router testRouter() {
  Future<Middleware> testMiddleware(
      ServerContext serverContext, Request req, Response res) async {
    if (2 == 2) {
      return Next();
    }
    return Stop(res.forbidden(body: {"error": "not allowed"}).json());
  }

  Router router = Router();
  router.useMiddleware(testMiddleware);
  router.useMiddleware((serverContext, req, res) {
    if (2 == 2) {
      return Next();
    }
    return Stop(res.forbidden(body: {"error": "not allowed"}).json());
  });

  router.socketIO(
      socketIOHandler: (context, websocket) {
        print('Socket is coming');

        /// handle new connected websocket client.
      },
      path: "/socket.io/");

  router.get(
      path: "/test/{id}",
      requestHandler: (context, req, res) async {
        context.actorContainers['customizedActor']!.execute("insert");
        CustomisedActor customisedActor =
            await context.actorContainers['customizedActor']!.getInstance();
        print(customisedActor.get("get"));
        return res.ok(body: {"id": req.pathParams['id']}).json();
      });

  router.routerInitState(routerInitState: (ServerContext context) async {});

  router.get(
      path: "/test_template/{id}",
      requestHandler: (context, req, res) async {
        print(req.pathParams['id']);
        return res.renderHtmlAsString(
            "<!DOCTYPE html <html> <body><h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><h4>Heading 4</h4><h5>Heading 5</h5><h6>Heading 6</h6></body></html>");
      });
  router.get(
      path: "/file",
      requestHandler: (context, req, res) {
        return res.ok(body: {'ss': 12}).text();
      });
  router.any(
      path: "/",
      requestMethods: [RequestMethod.get(), RequestMethod.post()],
      requestHandler: (context, req, res) {
        if (req.body == null) {
          throw FennecException(message: 'your body is empty');
        }
        return res.ok(body: {'ss': 12}).text();
      });
  router.any(
      path: "/test",
      requestMethods: [RequestMethod.get(), RequestMethod.post()],
      requestHandler: (context, req, res) {
        if (1 == 1) {
          throw CustomFennecError();
        }
        return res.ok(body: {'ss': 12}).text();
      });
  router.ws(
      path: "/connect1",
      websocketHandler: (context, websocket) {
        /// handle new connected websocket client.
      });
  router.get(
      path: "/template",
      requestHandler: (context, req, res) {
        return res.render("file");
      });

  return router;
}

class CustomisedActor extends Actor {
  final List<String> _strings = [];

  CustomisedActor(String name) : super(name);

  @override
  FutureOr<void> execute(String action,
      {Map<String, dynamic> data = const {}}) {
    if (action == 'insert') {
      _strings.add(" new item");
    } else {
      if (_strings.isNotEmpty) {
        _strings.removeLast();
      }
    }
  }

  @override
  FutureOr get(String action, {Map<String, dynamic> data = const {}}) {
    if (action == "get") {
      return _strings;
    }
    return null;
  }
}
46
likes
140
pub points
27%
popularity

Publisher

verified publisherfennecframework.com

Fennec is a multi-threaded, robust Dart Server-Side Framework.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

mime, path

More

Packages that depend on fennec