Skip to content

Controllers

Ravi Teja Gudapati edited this page Jan 14, 2019 · 11 revisions

Controllers are declarative way to write route handlers in Jaguar (as an alternative to mux-based routing). Each route handler is implemented as a method in the Controller class. The route handlers shall be annotated with one of the HTTP method annotations for Jaguar to recognize them. The controller class itself shall be annotated with GenController.

@GenController(path: '/library')
class LibraryApi extends Controller {
  @Get(path: '/all')
  Future<List<Book>> getAll(Context ctx) => books;

  @Post(path: '/add')
  Future<void> add(Context ctx) {
    // TODO
  }
}

Interceptors

Controller class's before method is called before any of the route handlers in the controller is executed. Here, you can directly execute other interceptors, register interceptors to be executed before or after the route handler execution using before and after methods of Context class.

@GenController(path: '/library')
class LibraryApi extends Controller {
  @Get(path: '/all')
  Future<List<Book>> getAll(Context ctx) => books;

  Future<void> before(Context ctx) async {
    await mongoPool(ctx);
    await Authorizer.authorize(ctx);
    ctx.after((_) => print("After"));
  }
}

Reflected

Reflected easier of the two methods to add Controllers to Jaguar server, so it is best to use when you are getting started with Jaguar.

jaguar_reflect package provides reflect method which can be used to compile Route specs from your controller class. The returned Route specs can be added to Jaguar server using the add method.

import 'package:jaguar_reflect/jaguar_reflect';

main() async {
  final server = Jaguar();
  server.add(reflect(LibraryApi()));
}

Source generated

TODO

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally