route_provider 4.2.1 copy "route_provider: ^4.2.1" to clipboard
route_provider: ^4.2.1 copied to clipboard

outdated

Provides backend routing for http-servers. Split routes into separates parts: authentification, controller for data processing and response for outputting the results.

example/example.dart

import 'dart:async';
import 'dart:io';
import 'package:route_provider/route_provider.dart';

class RouteControllerError extends Controller {
  RouteControllerError();

  @override
  Future<Map> execute(HttpRequest request, Map params, {AuthResponse authResponse}) async {
    throw RouteError(HttpStatus.notFound, "ERROR");
  }
}

class APIController extends RestApiController {
  @override
  Future<Map> onGet(HttpRequest request, Map params, {AuthResponse authResponse}) async {
    throw RouteError(HttpStatus.internalServerError, 'Not supported');
  }
}

class MyAuth implements Auth {
  bool authed = false;
  MyAuth({this.authed = false});

  @override
  Future<AuthResponse> isAuthed(HttpRequest request, Map params) async => this.authed ? StaticAuthResponse() : null;
}

Future main() async {
  HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4040);

  Router(server)
    ..route(url: '/', responser: FileResponse("docroot/home.html"), auth: MyAuth(authed: true))
    ..route(url: '/', responser: FolderResponse("docroot/"), auth: MyAuth(authed: true))
    ..route(url: '/img', responser: FolderResponse("docroot/assets/img"), auth: MyAuth(authed: true))
    ..route(url: '/assets/**', responser: FolderResponse("docroot/assets/"), auth: MyAuth(authed: true))
    ..route(url: '/js/**', responser: FolderResponse("docroot/code/"), auth: MyAuth(authed: true))
    ..route(url: '/error', controller: RouteControllerError(), responser: FileResponse("docroot/home.html"), auth: MyAuth(authed: true))
    ..route(url: '/error2', controller: APIController(), responser: FileResponse("docroot/home.html"), auth: MyAuth(authed: true))
    ..route(url: '/noauth', controller: APIController(), responser: FileResponse("docroot/home.html"), auth: MyAuth(authed: false))
    ..start();

  print('listening on localhost, port ${server.port}');
}
1
likes
0
pub points
22%
popularity

Publisher

unverified uploader

Provides backend routing for http-servers. Split routes into separates parts: authentification, controller for data processing and response for outputting the results.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

mime_type

More

Packages that depend on route_provider