startServer method

Future<void> startServer()

Implementation

Future<void> startServer() async {
  Get.log('Server started on: $host:$port');

  _server = await _getHttpServer();

  _server?.listen(
    (req) {
      if (useLog) Get.log('Method ${req.method} on ${req.uri}');
      final route = RouteConfig.i.findRoute(req);

      route?.binding?.dependencies();
      if (cors) {
        addCorsHeaders(req.response, corsUrl);
        if (req.method.toLowerCase() == 'options') {
          var msg = {'status': 'ok'};
          req.response.write(json.encode(msg));
          req.response.close();
        }
      }
      if (route != null) {
        route.handle(req);
      } else {
        if (_public != null) {
          _virtualDirectory ??= VirtualDirectory(
            _public!.folder,
            // pathPrefix: public.path,
          )
            ..allowDirectoryListing = _public!.allowDirectoryListing
            ..jailRoot = _public!.jailRoot
            ..followLinks = _public!.followLinks
            ..errorPageHandler = (callback) {
              _onNotFound(
                callback,
                onNotFound,
              );
            }
            ..directoryHandler = (dir, req) {
              var indexUri = Uri.file(dir.path).resolve('index.html');
              _virtualDirectory!.serveFile(File(indexUri.toFilePath()), req);
            };

          _virtualDirectory!.serveRequest(req);
        } else {
          _onNotFound(
            req,
            onNotFound,
          );
        }
      }
    },
  );
}