runAppEngine function Null safety

Future runAppEngine(
  1. void handler(
    1. HttpRequest request
    ),
  2. {Function? onError,
  3. int port = 8080,
  4. bool shared = false,
  5. void onAcceptingConnections(
    1. InternetAddress address,
    2. int port
    )?}
)

Starts serving requests coming to this AppEngine application.

This function will start an HTTP server and will forward new HTTP requests to handler.

The handler will be executed inside a new request handler zone for every new request. This will isolate different requests from each other. Each handler has access to a ClientContext using the context getter in this library. It can be used to access appengine services, e.g. datastore.

In case an uncaught error occurs inside the request handler, the request will be closed with an "500 Internal Server Error", if possible, and the given onError handler will be called.

The onError function can take either the error object, or the error object and a stack as an argument. If onError was not provided, errors will get printed out to the stdout of this process.

You can provide a port if you want to run the HTTP server on a different port than the 8080 default.

The optional shared argument specifies whether additional AppEngine servers can bind to the same port. If shared is true and more AppEngine servers from this isolate or other isolates are bound to the port, then the incoming connections will be distributed among all the bound servers. Connections can be distributed over multiple isolates this way.

The optional onAcceptingConnections callback, if provided, will be notified when the server is accepting connections on port. The address and port arguments that are passed to the callback specify the address and port that the server is listening on.

The returned Future will complete when the HTTP server has been shutdown and is no longer serving requests.

Implementation

Future runAppEngine(
  void handler(HttpRequest request), {
  Function? onError,
  int port = 8080,
  bool shared = false,
  void onAcceptingConnections(InternetAddress address, int port)?,
}) {
  var errorHandler;
  if (onError != null) {
    if (onError is ZoneUnaryCallback) {
      errorHandler = (error, stack) => onError(error);
    } else if (onError is ZoneBinaryCallback) {
      errorHandler = onError;
    } else {
      throw ArgumentError(
          'The [onError] argument must take either one or two arguments.');
    }
  }

  return appengine_internal.runAppEngine(
      (HttpRequest request, ClientContext context) {
    ss.register(_APPENGINE_CONTEXT, context);
    handler(request);
  }, errorHandler,
      port: port,
      shared: shared,
      onAcceptingConnections: onAcceptingConnections);
}