Http class

Http class used to bootstrap your Http server You need to use one of the server adapters. Currently only Shelf adapter is available

Example:

void main() async {
  final address = InternetAddress.anyIPv4;
  final port = Http.getEnv('PORT', 8080);
  final app = Http(ShelfServer(address, port), threads: 8);
  // setup routes
  app.get('/').inject('request').inject('response').action(
    (Request request, Response response) {
      response.text('Hello world');
      return response;
    },
  );
  // sart the server
  await app.start();
}

Constructors

Http(Server server, {String? path, int threads = 1})

Properties

hashCode int
The hash code for this object.
no setterinherited
isDevelopment bool
Is application running in development mode
no setter
isProduction bool
Is application running in production mode
no setter
isStage bool
Is application running in staging mode
no setter
mode AppMode?
Application mode
getter/setter pair
path String?
Path to server static files from
final
route Route?
Memory cached result for chosen route
getter/setter pair
routes Map<String, Map<String, Route>>
Configured routes for different methods
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
server Server
Server adapter, currently only shelf server is supported
final
servers List<HttpServer>
List of servers running
no setter
threads int
Number of threads (isolates) to spawn
final

Methods

addRoute(String method, String path) Route
Initialize route
closeServer({bool force = false}) Future<void>
Close all the servers
delete(String url) Route
Initialize a DELETE route
error() → Hook
Initialize error hooks Error hooks are ran for each errors
execute(Route route, Request request, String context) FutureOr<Response>
Execute request
get(String url) Route
Initialize a GET route
getResource<T>(String name, {bool fresh = false, String context = 'utopia'}) → T
Get a resource
init() → Hook
Initialize a init hook Init hooks are ran before executing each request
match(Request request) Route?
Match route based on request
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
options() → Hook
Initialize options hook Options hooks are ran for OPTIONS requests
patch(String url) Route
Initialize a PATCH route
post(String url) Route
Initialize a POST route
put(String url) Route
Initialize a PUT route
reset() → void
Reset various resources
resetResources([String? context]) → void
Reset dependencies
run(Request request, String context) FutureOr<Response>
Run the execution for given request
setResource(String name, Function callback, {String context = 'utopia', List<String> injections = const []}) → void
Set resource Once set, you can use inject to inject these resources to set other resources or in the hooks and routes
shutdown() → Hook
Initialize shutdown hook Shutdown hooks are ran after executing the request, before the response is sent
start() Future<List<HttpServer>>
Start the servers
toString() String
A string representation of this object.
inherited
wildcard() Route
Initialize a wildcard route

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

getEnv(String key, [dynamic def]) → dynamic
Get environment variable