This package was created after the deprecation of Aqueduct. It is designed to be more developer-friendly

Package objectives

  • Simple to use
  • Lightweight
  • Very low time-to-deploy

Features

  • Separate classes for each endpoint and method
  • Fully-configurable middlewares that manages the request before and after reaches the endpoint
  • Native performance: every API created with SparkREST can be compiled using Dart's AOT compiler
  • Plugins: everyone can create and publish a plugin on pub.dev

How to use

  • This is not a production server; must be used with Nginx as a reverse-proxy (or every other server that has the reverse-proxy capability)
  • This API is designed to be passionless and single threaded. If you have more CPU threads, you have to run multiple instances of your API and enable load balancing on your production server
     

Use this package as a library

Depend on it

Run this command:

With Dart:

 $ dart pub add spark_rest

With Flutter:

 $ flutter pub add spark_rest

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):


dependencies:
  spark_rest: ^0.3.0

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:spark_rest/spark_rest.dart';

example/main.dart

import 'dart:io';

import 'package:spark_rest/spark_rest.dart';

class RootEndpoint extends Endpoint {
  RootEndpoint()
      : super(
          uri: '/',
          method: Method.get,
        );

  @override
  Future<Response> onHandle(Request request) async {
    return Response(
      request: request,
      statusCode: 200,
      headers: {},
      contentType: ContentType.text,
      body: request.container['Test'] ?? 'No var',
    );
  }
}

class TestEndpoint extends Endpoint {
  TestEndpoint()
      : super(
          uri: '/test',
          method: Method.get,
        );

  @override
  Future<Response> onHandle(Request request) async {
    return Response(
      request: request,
      statusCode: 200,
      headers: {},
      contentType: ContentType.text,
      body: request.container['Test'] ?? 'No var',
    );
  }
}

class MyRequestMiddleware extends Middleware<Request> {
  @override
  Future<void> onInit(Context context) async {
    print(MethodRouter.of(context, '/test'));
  }

  @override
  Future<Request> onHandle(Request request) async {
    request.container['Test'] = 'Test';
    return request;
  }

  @override
  bool Function(String uri, Method method) get attachTo =>
      (uri, method) => uri == '/';
}

class MyPlugin extends Plugin {
  @override
  Iterable<Middleware<Request>> get requestMiddlewares =>
      [MyRequestMiddleware()];
}

Future main() => boot(
    application: Application(
        requestMiddlewares: [],
        endpoints: [RootEndpoint(), TestEndpoint()],
        plugins: [MyPlugin()]));


 

16.15 GEEK