-
Notifications
You must be signed in to change notification settings - Fork 0
Hooks Basics
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.
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);
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);
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');
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
to be continue..