addRouting method

WaServer addRouting(
  1. Future<List<WebRoute>> router(
    1. WebRequest rq
    )
)

Adds a routing function to the server.

The router function returns a Future containing a list of WebRoute based on the provided WebRequest. This allows for dynamic routing based on the request.

Returns the WaServer instance to allow method chaining.

Implementation

WaServer addRouting(Future<List<WebRoute>> Function(WebRequest rq) router) {
  if (config.enableLocalDebugger && config.isLocalDebug) {
    debugger = SocketManager(
      this,
      routes: {
        'get_routes': SocketEvent(onMessage: (socket, data) async {
          var res = await exploreAllRoutes(socket.rq);
          debugger?.sendToAll({
            'routes': res,
          }, path: 'get_routes');
        }),
        'update_languages': SocketEvent(onMessage: (socket, data) async {
          appLanguages = await MultiLanguage(config.languagePath).init();
          await debugger?.sendToAll(
            {'message': 'Language updated'},
            path: 'update_languages',
          );
        }),
        'restart': SocketEvent(onMessage: (socket, data) async {
          await debugger?.sendToAll({}, path: 'restartStarted');
          await stop(force: true);
          await start();
        }),
        'get_data': SocketEvent(onMessage: (socket, data) async {
          debugger?.sendToAll({
            'error': {
              'params': socket.rq.getParams(),
              'uri': socket.rq.uri.toString(),
              'buffer': socket.rq.buffer.toString().split('\n'),
              'headers': socket.rq.headers.toString().split(';'),
              'session_cookies': socket.rq.getAllSession(),
            },
          }, path: 'console');
        }),
        'reinit': SocketEvent(onMessage: (socket, data) async {
          print("Server is restarting...");
          restart();
        }),
      },
    );

    Console.onError.add((error, type) {
      debugger?.sendToAll({
        'error': error.toString(),
        'type': type,
      }, path: "console");
    });

    Console.onLogging.add((error, type) {
      debugger?.sendToAll({
        'message': error.toString(),
        'type': type,
      }, path: "log");
    });

    WaCron(
      schedule: WaCron.evrySecond(1),
      delayFirstMoment: false,
      onCron: (index, cron) async {
        debugger?.sendToAll({
          'memory': ConvertSize.toLogicSizeString(ProcessInfo.currentRss),
          'max_memory': ConvertSize.toLogicSizeString(ProcessInfo.maxRss),
        }, path: "updateMemory");
      },
    ).start();

    _webRoutes.add((rq) async {
      rq.buffer.writeln(
          "<script src='${rq.url('/debugger/console.js')}'></script>");

      rq.addAsset(
        Asset(
          path: rq.url('/debugger/console.js'),
          rq: rq,
        ),
      );
      return [
        WebRoute(
          path: 'debugger',
          rq: rq,
          index: () async {
            await debugger?.requestHandel(rq, userId: "LOCAL_USER");
            debugger?.sendToAll({
              'type': 'user_connected',
              'userId': "LOCAL_USER",
            });
            return rq.renderSocket();
          },
          children: [
            WebRoute(
              path: 'console.js',
              rq: rq,
              index: () async {
                return rq.renderString(
                  text: ConsoleWidget().layout,
                  contentType: ContentType(
                    'text',
                    'javascript',
                    charset: 'utf-8',
                  ),
                );
              },
            )
          ],
        )
      ];
    });
  }
  _webRoutes.add(router);

  return this;
}