astra 1.0.0-dev.159 copy "astra: ^1.0.0-dev.159" to clipboard
astra: ^1.0.0-dev.159 copied to clipboard

A robust, multi-threaded shelf web server adapter designed for high performance and concurrent handling of multiple requests.

example/lib/example.dart

import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

import 'package:astra/core.dart';
import 'package:astra/middlewares.dart';

Application application() {
  return Counter();
}

final class Counter extends Application {
  int count = 0;

  @override
  Handler get entryPoint {
    return handler.use(logRequests()).use(error(debug: true));
  }

  Future<Response> handler(Request request) async {
    count += 1;

    messageHub?.add(1);

    if (request.url.path == '') {
      return Response.ok('You have requested this application $count time(s).');
    }

    if (request.url.path == 'isolate') {
      return Response.ok(Isolate.current.debugName);
    }

    if (request.url.path == 'send') {
      const headers = {'Content-Type': 'application/json; charset=utf-8'};

      var body = json.encode(request.url.queryParameters);
      return Response.ok(body, headers: headers);
    }

    if (request.url.path == 'throw') {
      throw Exception('Oh no!');
    }

    return Response.notFound('Not Found: ${request.url.path}');
  }

  void onMessage(Object? event) {
    if (event is int) {
      count += event;
    }
  }

  @override
  Future<void> prepare() async {
    messageHub?.listen(onMessage);
  }

  @override
  Future<void> reload() async {
    count = 0;
    print('Reset count.');
  }

  @override
  Future<void> close() async {
    print('Total count: $count.');
  }
}
1
likes
160
points
57
downloads

Publisher

unverified uploader

Weekly Downloads

A robust, multi-threaded shelf web server adapter designed for high performance and concurrent handling of multiple requests.

Homepage
Repository (GitHub)
View/report issues

Topics

#server #shelf

Documentation

API reference

License

MIT (license)

Dependencies

collection, http_parser, logging, meta, shelf, shelf_client, stack_trace, stream_channel

More

Packages that depend on astra