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

outdated

Provides backend routing for http-server from dart:io. Split routes into separates parts: authentification, controller for data processing and response for outputting the results. It comes around with [...]

example/example.dart

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

class RouteControllerError extends RouteController {
  RouteControllerError();

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

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

class MyAuth implements Auth {
  bool authed = false;
  MyAuth({this.authed: false});
  Future<AuthResponse> isAuthed(HttpRequest request, Map params) async =>
      this.authed ? new AuthResponse() : null;
}

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

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

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

Publisher

unverified uploader

Provides backend routing for http-server from dart:io. Split routes into separates parts: authentification, controller for data processing and response for outputting the results. It comes around with a some default responsers for files, folders, json or redirects.

Repository
View/report issues

License

unknown (LICENSE)

Dependencies

mime_type

More

Packages that depend on route_provider