Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Hooks Basics

Eugen Hildt edited this page Aug 29, 2020 · 8 revisions

In Geoffrey everything is a hook! There is no need to create objects, extend classes, annotate their properties etc. In general most of Geoffrey's hooks can be used literally anywhere in your code. There are a few exceptions thou we will cover them as we go along with the documentation.

Table of Contents

  1. UseHttpServer

  2. UseSecureHttpServer

  3. UseSecurityContext

  4. Routing

    4.1. UseGet

    4.2. UsePut

    4.3. UsePatch

    4.4. UsePost

    4.5. UseDelete

    4.6. UseCustom

1. UseHttpServer

The useHttpServer does a few things for us.

  • It creates and returns a http server
  • Sets the default response headers
  • Handles the incoming Requests
  • Takes care of the middlewares
  • Serves files if a publicDir is provided
  • Gzips all the text based contents right out of the box

Here are the params that it takes:

Future<HttpServer> useHttpServer(
    String host, 
    int port,
{ 
    int backlog = 0,
    bool v60only = false,
    bool shared = false,
    bool autoClose = true
})
  • backlog: Can be used to specify the listen backlog for the underlying OS listen setup
  • v60only: Use IPv6 only
  • shared: Shares the port with other servers
  • autoClose: calls request.response.close() automatically

Example

HttpServer server = await useHttpServer('localhost', 8080);

2. UseSecureHttpServer

The useSecureHttpServer hook essentially does the same thing the useHttpServer hook does. However it comes with one additional required parameter, the security context!

Future<HttpServer> useSecureHttpServer(
String host, 
int port, 
SecurityContext sctx,
{...})

The SecurityContext is part of the dart:io package. You can either use the api or simply invoke the useSecurityContext hook to create a security context.

Example

HttpServer server = await useSecureHttpServer('localhost', 443, sctx);

3. UseSecurityContext

The UseSecurityContext creates a SecurityContext by simply setting the certificate, privateKey and the password. For a more advanced usage we recommend to use the SecurityContext from the dart:io api.

SecurityContext useSecurityContext({ 
    @required String cert, 
    @required String pkey, 
    String password 
})

Example

SecurityContext sctx = useSecurityContext(cert: 'cert.pem', pkey: 'key.pem');

4. Routing

All of the route hooks are pretty much identical. Let's take useGet as an example.

useGet(
  route: '/home',
  handleRequest: (req, res) => res.write('hello'),
  handleGuard: (req, res) => true); // optional

The route and handleRequest parameters are both required. handleRequest and handleGuard can be async! handleGuard is executed before handleRequest and must return a boolean. The useCustom route hook is slightly different since it takes one additional method parameter. You can remove a route by deleting all the methods assotiated to it. To remove a method just use the counterpart hook. For useGet the counterpart hook is useRemoveGet. Just like useCustom the useRemoveCustom hook takes one additional method parameter.

  • useGet / useRemoveGet
  • usePut / useRemovePut
  • usePatch / useRemovePatch
  • usePost / useRemovePost
  • useDelete / useRemoveDelete
  • useCustom / useRemoveCustom

5. UsePublicDir

to be continue..

Clone this wiki locally