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

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

example/example.dart

import 'dart:async';
import 'dart:isolate';

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

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

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.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
0
pub points
21%
popularity

Publisher

verified publisherfennecframework.com

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

mime, path

More

Packages that depend on fennec