1 /*
2  * Archttp - A highly performant web framework written in D.
3  *
4  * Copyright (C) 2021-2022 Kerisy.com
5  *
6  * Website: https://www.kerisy.com
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module archttp.Route;
13 
14 import archttp.HttpMethod;
15 
16 import std.stdio;
17 
18 class Route(RoutingHandler, MiddlewareHandler)
19 {
20     private
21     {
22         string _path;
23 
24         RoutingHandler[HttpMethod] _handlers;
25         MiddlewareHandler[] _middlewareHandlers;
26     }
27 
28     public
29     {
30         // like uri path
31         string pattern;
32 
33         // use regex?
34         bool regular;
35 
36         // Regex template
37         string urlTemplate;
38 
39         string[uint] paramKeys;
40     }
41     
42     this(string path, HttpMethod method, RoutingHandler handler)
43     {
44         _path = path;
45 
46         bindMethod(method, handler);
47     }
48 
49     Route bindMethod(HttpMethod method, RoutingHandler handler)
50     {
51         _handlers[method] = handler;
52         return this;
53     }
54 
55     Route use(MiddlewareHandler handler)
56     {
57         _middlewareHandlers ~= handler;
58         return this;
59     }
60 
61     MiddlewareHandler[] middlewareHandlers()
62     {
63         return _middlewareHandlers;
64     }
65 
66     RoutingHandler find(HttpMethod method)
67     {
68         auto handler = _handlers.get(method, null);
69 
70         return cast(RoutingHandler) handler;
71     }
72 
73     string path()
74     {
75         return _path;
76     }
77 }