create method

Future<DartnessServer> create({
  1. Iterable<DartnessController> controllers = const [],
  2. Iterable<DartnessMiddleware> middlewares = const [],
  3. Iterable<DartnessInterceptor> interceptors = const [],
  4. Iterable<DartnessErrorHandler> errorHandlers = const [],
  5. DartnessApplicationOptions? options,
})

Starts the _server.

If logRequest is true prints the time of the request, the elapsed time for the inner handlers, the response's status code and the request URI.

Implementation

Future<DartnessServer> create({
  final Iterable<DartnessController> controllers = const [],
  final Iterable<DartnessMiddleware> middlewares = const [],
  final Iterable<DartnessInterceptor> interceptors = const [],
  final Iterable<DartnessErrorHandler> errorHandlers = const [],
  final DartnessApplicationOptions? options,
}) async {
  _options = options ?? DartnessApplicationOptions();

  _server = DefaultDartnessServer(
    _options.port,
    internetAddress: _options.internetAddress,
  );

  for (final controller in controllers) {
    _addController(controller);
  }
  for (final middleware in middlewares) {
    _addMiddleware(middleware);
  }
  for (final interceptor in interceptors) {
    _addInterceptor(interceptor);
  }
  for (final errorHandler in errorHandlers) {
    _addErrorHandler(errorHandler);
  }

  if (options?.logRequest == true) {
    _addInterceptor(LogRequestsInterceptor());
  }
  await _server.start();
  print('Server listening on port ${_server.getPort()}');
  return _server;
}