startServer method

Future<void> startServer()

Implementation

Future<void> startServer() async {
  final jwtMiddleware = jwtKey != null ? JwtAuthMiddleware(jwtKey!) : null;
  var server = await HttpServer.bind(InternetAddress.anyIPv4, port);
  await for (HttpRequest request in server) {
    String path = request.uri.path;
    String method = request.method;
    try {
      Route route = RatelHandler.routes.firstWhere(
        (r) => r.path == path && r.method == method,
      );

      if (jwtMiddleware != null && route.isProtected) {
        final payload = await jwtMiddleware.validate(request);
        if (payload == null) {
          Response(
            statusCode: HttpStatus.unauthorized,
            data: {'error': 'Token inválido ou ausente'},
          ).send(request.response);
          continue;
        }
      }

      final responseData = await route.handler(request);
      Response.from(responseData).send(request.response);
    } on StateError catch (e) {
      Response(
        statusCode: HttpStatus.notFound,
        data: {'error': '$e'},
      ).send(request.response);
    } catch (e) {
      Response(
        statusCode: HttpStatus.internalServerError,
        data: {'error': e.toString()},
      ).send(request.response);
    }
  }
}