relic 0.5.0 copy "relic: ^0.5.0" to clipboard
relic: ^0.5.0 copied to clipboard

A lightweight and flexible web server inspired by Shelf for building APIs and backend services.

example/example.dart

import 'dart:io';

import 'package:relic/io_adapter.dart';
import 'package:relic/relic.dart';

/// A simple 'Hello World' server
Future<void> main() async {
  // Setup router
  final router = Router<Handler>()..get('/user/:name/age/:age', hello);

  // Setup a handler.
  //
  // A [Handler] is function consuming [NewContext]s and producing [HandledContext]s,
  // but if you are mostly concerned with converting [Request]s to [Response]s
  // (known as a [Responder] in relic parlor) you can use [respondWith] to
  // wrap a [Responder] into a [Handler]
  final handler = const Pipeline()
      .addMiddleware(logRequests())
      .addMiddleware(routeWith(router))
      .addHandler(respondWith((final _) => Response.notFound(
          body: Body.fromString("Sorry, that doesn't compute"))));

  // Start the server with the handler
  await serve(handler, InternetAddress.anyIPv4, 8080);

  // Check the _example_ directory for other examples.
}

ResponseContext hello(final NewContext ctx) {
  final name = ctx.pathParameters[#name];
  final age = int.parse(ctx.pathParameters[#age]!);

  return ctx.respond(Response.ok(
      body: Body.fromString('Hello $name! To think you are $age years old.')));
}
42
likes
160
points
19.5k
downloads

Publisher

verified publisherserverpod.dev

Weekly Downloads

A lightweight and flexible web server inspired by Shelf for building APIs and backend services.

Repository (GitHub)
Contributing

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

async, collection, convert, crypto, http_parser, meta, mime, path, stack_trace, stream_channel, web_socket, web_socket_channel

More

Packages that depend on relic