···3535You can then begin to implement your very own Laurali server.
36363737```ts
3838-import {
3939- Callback,
4040- callback,
4141- route,
4242- Server,
4343-} from "https://deno.land/x/laurali/mod.ts";
3838+import { route, Server } from "https://deno.land/x/laurali/mod.ts";
44394540class MyCoolServer extends Server {
4641 /** Visit `/hi` */
+4-4
examples/my_cool_server.ts
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919-import { callback, route, Server } from "../mod.ts";
1919+import { hook, route, Server } from "../mod.ts";
2020import * as optic from "https://deno.land/x/optic@1.3.5/mod.ts";
21212222/** Implement a new Laurali server */
···5050 return MyCoolServer.clicks;
5151 }
52525353- @callback()
5353+ @hook()
5454 override onPreRoute(ctx: Deno.TlsConn) {
5555 MyCoolServer.clicks += 1;
5656···6060 );
6161 }
62626363- @callback()
6363+ @hook()
6464 override onPostRoute() {
6565 MyCoolServer.logger.info("Closed connection.");
6666 }
67676868- @callback()
6868+ @hook()
6969 override onError() {
7070 return "hi";
7171 }
+2-2
laurali/callbacks.ts
laurali/hooks.ts
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919-/** One of few callbacks of a `Server` */
2020-export const enum Callback {
1919+/** One of few hooks of a `Server` */
2020+export const enum Hook {
2121 /** Called before a connection to a client has been responded to */
2222 ON_PRE_ROUTE = 0,
2323 /** Called after a connection to a client has concluded */
+12-12
laurali/decorators.ts
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919-import { Callback } from "./callbacks.ts";
1919+import { Hook } from "./hooks.ts";
20202121/**
2222 * Mark the function as a route and register it to the `Server`.
···3636};
37373838/**
3939- * Mark the function as a callback and register it to the `Server`.
4040- * @param callback The type of callback which the function will be called for.
3939+ * Mark the function as a hook and register it to the `Server`.
4040+ * @param hook The type of hook which the function will be called for.
4141 */
4242-export const callback = (callback?: Callback) => {
4242+export const hook = (hook?: Hook) => {
4343 return (
4444 // deno-lint-ignore no-explicit-any
4545 target: any,
···4848 ) => {
4949 let type;
50505151- if (callback) {
5252- type = callback;
5151+ if (hook) {
5252+ type = hook;
5353 } else {
5454 switch (key) {
5555 case "onPreRoute":
5656 {
5757- type = Callback.ON_PRE_ROUTE;
5757+ type = Hook.ON_PRE_ROUTE;
5858 }
5959 break;
6060 case "onPostRoute":
6161 {
6262- type = Callback.ON_POST_ROUTE;
6262+ type = Hook.ON_POST_ROUTE;
6363 }
6464 break;
6565 case "onError":
6666 {
6767- type = Callback.ON_ERROR;
6767+ type = Hook.ON_ERROR;
6868 }
6969 break;
7070 default: {
7171 throw new Error(
7272- `Unknown callback type: '${key.toString()}'. Did you forget to ` +
7373- "specify the callback type?`",
7272+ `Unknown hook type: '${key.toString()}'. Did you forget to ` +
7373+ "specify the hook type?`",
7474 );
7575 }
7676 }
7777 }
78787979- target.addCallback(type, descriptor.value);
7979+ target.addHook(type, descriptor.value);
80808181 return descriptor;
8282 };
+1-1
laurali/mod.ts
···1717// SPDX-License-Identifier: GPL-3.0-only
18181919export * from "./decorators.ts";
2020-export * from "./callbacks.ts";
2020+export * from "./hooks.ts";
2121export * from "./server.ts";
+12-12
laurali/server.ts
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919-import { Callback } from "./callbacks.ts";
1919+import { Hook } from "./hooks.ts";
20202121export interface ServerConfiguration {
2222 port?: number;
···3434 /** All registered route functions of the `Server` */
3535 // deno-lint-ignore no-explicit-any
3636 static #routes: Map<string, (ctx: Deno.TlsConn) => any> = new Map();
3737- /** All registered callback functions of the `Server` */
3838- static #callbacks: Map<Callback, (ctx: Deno.TlsConn) => void> = new Map();
3737+ /** All registered hook functions of the `Server` */
3838+ static #hooks: Map<Hook, (ctx: Deno.TlsConn) => void> = new Map();
3939 /** The port of the `Server` */
4040 static #port: number;
4141 /** The hostname of the `Server` */
···6565 addRoute(route: string, handler: () => any) {
6666 Server.#routes.set(route, handler);
6767 }
6868- /** Add a callback function to the `Server` */
6969- addCallback(callback: Callback, handler: () => void) {
7070- Server.#callbacks.set(callback, handler);
6868+ /** Add a hook function to the `Server` */
6969+ addHook(hook: Hook, handler: () => void) {
7070+ Server.#hooks.set(hook, handler);
7171 }
72727373 /** Get the `port` of the `Server` */
···98989999 /** Start listening and responding to client connections */
100100 async listen() {
101101- // If the `Server` has an `onListen` callback, call it.
101101+ // If the `Server` has an `onListen` hook, call it.
102102 if (this.onListen) this.onListen();
103103104104 // Listen for connections and handle them.
···115115 continue;
116116 }
117117118118- const onPreRoute = Server.#callbacks.get(Callback.ON_PRE_ROUTE);
119119- const onPostRoute = Server.#callbacks.get(Callback.ON_POST_ROUTE);
120120- const onError = Server.#callbacks.get(Callback.ON_ERROR);
118118+ const onPreRoute = Server.#hooks.get(Hook.ON_PRE_ROUTE);
119119+ const onPostRoute = Server.#hooks.get(Hook.ON_POST_ROUTE);
120120+ const onError = Server.#hooks.get(Hook.ON_ERROR);
121121122122 // Make sure that the client has sent a request.
123123 if (n === null) {
···132132 "",
133133 ).replace(/gemini:\/\//, "");
134134135135- // If the `Server` has an `onPreRoute` callback, call it.
135135+ // If the `Server` has an `onPreRoute` hook, call it.
136136 if (onPreRoute) onPreRoute(r);
137137138138 // Respond to index requests.
···178178 r.close();
179179 }
180180181181- // If the `Server` has an `onPostRoute` callback, call it.
181181+ // If the `Server` has an `onPostRoute` hook, call it.
182182 if (onPostRoute) onPostRoute(r);
183183184184 continue;