Blame


1 ed8cff62 2023-12-21 op // This is free and unencumbered software released into the public domain.
2 ed8cff62 2023-12-21 op //
3 ed8cff62 2023-12-21 op // Anyone is free to copy, modify, publish, use, compile, sell, or
4 ed8cff62 2023-12-21 op // distribute this software, either in source code form or as a compiled
5 ed8cff62 2023-12-21 op // binary, for any purpose, commercial or non-commercial, and by any
6 ed8cff62 2023-12-21 op // means.
7 ed8cff62 2023-12-21 op //
8 ed8cff62 2023-12-21 op // In jurisdictions that recognize copyright laws, the author or authors
9 ed8cff62 2023-12-21 op // of this software dedicate any and all copyright interest in the
10 ed8cff62 2023-12-21 op // software to the public domain. We make this dedication for the benefit
11 ed8cff62 2023-12-21 op // of the public at large and to the detriment of our heirs and
12 ed8cff62 2023-12-21 op // successors. We intend this dedication to be an overt act of
13 ed8cff62 2023-12-21 op // relinquishment in perpetuity of all present and future rights to this
14 ed8cff62 2023-12-21 op // software under copyright law.
15 ed8cff62 2023-12-21 op //
16 ed8cff62 2023-12-21 op // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ed8cff62 2023-12-21 op // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ed8cff62 2023-12-21 op // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 ed8cff62 2023-12-21 op // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 ed8cff62 2023-12-21 op // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ed8cff62 2023-12-21 op // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 ed8cff62 2023-12-21 op // OTHER DEALINGS IN THE SOFTWARE.
23 ed8cff62 2023-12-21 op
24 ed8cff62 2023-12-21 op use fnmatch;
25 ed8cff62 2023-12-21 op
26 ed8cff62 2023-12-21 op export type route = struct {
27 ed8cff62 2023-12-21 op pattern: str,
28 ed8cff62 2023-12-21 op routefn: routefn,
29 ed8cff62 2023-12-21 op };
30 ed8cff62 2023-12-21 op
31 ed8cff62 2023-12-21 op export type mux = []route;
32 ed8cff62 2023-12-21 op
33 ed8cff62 2023-12-21 op let global: mux = [];
34 ed8cff62 2023-12-21 op
35 ed8cff62 2023-12-21 op export fn newmux() mux = [];
36 ed8cff62 2023-12-21 op
37 ed8cff62 2023-12-21 op export fn handle(pattern: str, routefn: routefn) void = {
38 ed8cff62 2023-12-21 op handle_mux(&global, pattern, routefn);
39 ed8cff62 2023-12-21 op };
40 ed8cff62 2023-12-21 op
41 ed8cff62 2023-12-21 op export fn handle_mux(mux: *mux, pattern: str, routefn: routefn) void = {
42 ed8cff62 2023-12-21 op append(mux, route {
43 ed8cff62 2023-12-21 op pattern = pattern,
44 ed8cff62 2023-12-21 op routefn = routefn,
45 ed8cff62 2023-12-21 op });
46 ed8cff62 2023-12-21 op };
47 ed8cff62 2023-12-21 op
48 ed8cff62 2023-12-21 op export fn mux_match(mux: *mux, req: *request, default: routefn) routefn = {
49 ed8cff62 2023-12-21 op for (let i = 0z; i < len(mux); i += 1) {
50 ed8cff62 2023-12-21 op if (fnmatch::fnmatch(mux[i].pattern, req.path)) {
51 ed8cff62 2023-12-21 op return mux[i].routefn;
52 ed8cff62 2023-12-21 op };
53 ed8cff62 2023-12-21 op };
54 ed8cff62 2023-12-21 op
55 ed8cff62 2023-12-21 op return default;
56 ed8cff62 2023-12-21 op };