run static method

Future<Winter> run({
  1. BuildContext? context,
  2. ServerConfig? config,
  3. AbstractWinterRouter? router,
  4. FilterConfig? globalFilterConfig,
})

Implementation

static Future<Winter> run({
  BuildContext? context,
  ServerConfig? config,
  AbstractWinterRouter? router,
  FilterConfig? globalFilterConfig,
}) async {
  if (isRunning) {
    throw StateError('Server already starter');
  }

  final startTime = DateTime.now();

  BuildContext nonNullContext = context ?? BuildContext();
  ServerConfig nonNullConfig = config ?? ServerConfig();
  AbstractWinterRouter nonNullRouter = router ?? WinterRouter();
  FilterConfig nonNullGlobalFilterConfig =
      globalFilterConfig ?? const FilterConfig([]);

  HttpServer rawServer = await shelf_io.serve(
    poweredByHeader: 'Winter-Server',
    (request) => _handleRunRequest(
      router: nonNullRouter,
      globalFilterConfig: nonNullGlobalFilterConfig,
      request: request,
    ),
    nonNullConfig.ip,
    nonNullConfig.port,
  );

  Winter nextRunningServer = Winter._(
    context: nonNullContext,
    config: nonNullConfig,
    router: nonNullRouter,
    globalFilterConfig: nonNullGlobalFilterConfig,
    rawServer: rawServer,
  );

  _server = nextRunningServer;

  final endTime = DateTime.now();
  double timeDiff = endTime.difference(startTime).inMilliseconds / 1000;
  log('Server started on port ${rawServer.port} ($timeDiff sec)');

  return nextRunningServer;
}