1 module mood.hook;
2 
3 import vibe.http.server: HTTPServerRequest, HTTPServerResponse;
4 import std.stdio: writeln;
5 
6 alias MoodHookFn = void function(HTTPServerRequest, HTTPServerResponse);
7 alias MoodHookDg = void delegate(HTTPServerRequest, HTTPServerResponse);
8 
9 private enum HookType
10 {
11     Function,
12     Delegate
13 }
14 
15 private struct Hook
16 {
17     HookType type;
18     MoodHookFn fn;
19     MoodHookDg dg;
20 }
21 
22 private Hook[] hooks;
23 
24 /**
25  * Register a hook.
26  * 
27  * Registers a hook that is called whenever a page is loaded.
28  * Params:
29  *  fn = The function that is going to be called on page load
30  */
31 public void moodRegisterHook(MoodHookFn fn)
32 {
33     hooks ~= Hook(HookType.Function, fn, null);
34 }
35 
36 /**
37  * Register a hook.
38  * 
39  * Registers a hook that is called whenever a page is loaded.
40  * Params:
41  *  dg = The delegate that is going to be called on page load
42  */
43 public void moodRegisterHook(MoodHookDg dg)
44 {
45     hooks ~= Hook(HookType.Delegate, null, dg);
46 }
47 
48 /**
49  * Calls all hooks.
50  *
51  * Called whenever a page is loaded. There is no need to call this yourself.
52  * Params:
53  *  req = The HTTPServerRequest
54  *  res = The HTTPServerResponse
55  */
56 public void moodCallHooks(HTTPServerRequest req, HTTPServerResponse res)
57 {
58     foreach(hook; hooks)
59     {
60         writeln(hook.type);
61         if (hook.type == HookType.Function)
62             hook.fn(req, res);
63         else
64             hook.dg(req, res);
65     }
66 }