1function mcp_config_pools()
2    local b1 = mcp.backend('b1', '127.0.0.1', 12172)
3    return mcp.pool({b1})
4end
5
6function mcp_config_routes(p)
7    local mgsepkey = mcp.funcgen_new()
8    local mgsepkeyh = mgsepkey:new_handle(p)
9
10    local mgsepkey_ins = mcp.req_inspector_new(
11        { t = "sepkey", sep = "/", pos = 2, map = {
12            foo = 1, bar = 2,
13        }},
14        { t = "keybegin", str = "sepkey" }
15    )
16
17    local mgsepkeynomap_ins = mcp.req_inspector_new(
18        { t = "sepkey", sep = "/", pos = 2 }
19    )
20
21    local mgsepkey1_ins = mcp.req_inspector_new(
22        { t = "sepkey", sep = "/", pos = 1, map = {
23            baz = 1, foo = 2, sepkey = 3,
24        }}
25    )
26
27    local mgsepkey2_ins = mcp.req_inspector_new(
28        { t = "sepkey", sep = "/", pos = 2, map = {
29            baz = 1, foo = 2,
30        }}
31    )
32
33    local mgsepkey3_ins = mcp.req_inspector_new(
34        { t = "sepkey", sep = "/", pos = 3, map = {
35            baz = 1, foo = 2, three = 3,
36        }}
37    )
38
39    mgsepkey:ready({
40        n = "sepkey", f = function(rctx)
41            return function(r)
42                local key = r:key()
43                if key == "sepkey/baz" then
44                    local idx = mgsepkey2_ins(r)
45                    return string.format("SERVER_ERROR idx: %q\r\n", idx)
46                elseif key == "sepkey/nomap" then
47                    local str = mgsepkeynomap_ins(r)
48                    return string.format("SERVER_ERROR str: %s\r\n", str)
49                elseif key == "sepkey/one/two" then
50                    local idx = mgsepkey1_ins(r)
51                    return string.format("SERVER_ERROR idx: %q\r\n", idx)
52                elseif key == "sepkey/two/three" then
53                    local idx = mgsepkey3_ins(r)
54                    return string.format("SERVER_ERROR idx: %q\r\n", idx)
55                else
56                    local idx, b = mgsepkey_ins(r)
57                    local begin = "false"
58                    if b then
59                        begin = "true"
60                    end
61                    if idx == nil then
62                        return "SERVER_ERROR nil index\r\n"
63                    else
64                        return string.format("SERVER_ERROR idx: %d %s\r\n", idx, begin)
65                    end
66                end
67            end
68        end
69    })
70
71    local mgreshasf = mcp.funcgen_new()
72    local mgreshasfh = mgreshasf:new_handle(p)
73
74    local mgreshasf_ins = mcp.res_inspector_new(
75        { t = "hasflag", flag = "f" },
76        { t = "hasflag", flag = "t" }
77    )
78    -- TODO: also test req version
79    local mgresflaga_ins = mcp.res_inspector_new(
80        { t = "flagtoken", flag = "O" },
81        { t = "flagint", flag = "t" }
82    )
83
84    local mgreqflaga_ins = mcp.req_inspector_new(
85        { t = "flagtoken", flag = "O" },
86        { t = "flagint", flag = "T" }
87    )
88
89    local mgresflagis_ins = mcp.res_inspector_new(
90        { t = "flagis", flag = "O", str = "baz" }
91    )
92    mgreshasf:ready({
93        n = "reshasflag", f = function(rctx)
94            return function(r)
95                local key = r:key()
96                local res = rctx:enqueue_and_wait(r, mgreshasfh)
97                if key == "reshasf/flagis" then
98                    local exists, matches = mgresflagis_ins(res)
99                    return string.format("SERVER_ERROR exists[%q] matches[%q]\r\n", exists, matches)
100                elseif key == "reshasf/tokenint" then
101                    local has_O, O, has_t, t = mgresflaga_ins(res)
102                    return string.format("SERVER_ERROR O[%q]: %s t[%q]: %q\r\n",
103                        has_O, O, has_t, t)
104                elseif key == "reshasf/reqhasf" then
105                    local has_O, O, has_T, T = mgreqflaga_ins(r)
106                    return string.format("SERVER_ERROR O[%q]: %s T[%q]: %q\r\n",
107                        has_O, O, has_T, T)
108                else
109                    local f, t = mgreshasf_ins(res)
110                    return "SERVER_ERROR f: " .. tostring(f) .. " t: " .. tostring(t) .. "\r\n"
111                end
112            end
113        end
114    })
115
116    local mgreqkey = mcp.funcgen_new()
117    local mgreqkeyh = mgreqkey:new_handle(p)
118
119    local mgreqkeyis_ins = mcp.req_inspector_new(
120        { t = "keyis", str = "reqkey/one" },
121        { t = "keyis", str = "reqkey/two" },
122        { t = "keyis", str = "reqkey/three" }
123    )
124
125    mgreqkey:ready({
126        n = "reqkey", f = function(rctx)
127            return function(r)
128                local one, two, three = mgreqkeyis_ins(r)
129                return string.format("SERVER_ERROR one[%q] two[%q] three[%q]\r\n",
130                    one, two, three)
131            end
132        end
133    })
134
135    local mgintres = mcp.funcgen_new()
136    -- no handle: using mcp.internal()
137    mgintres:ready({
138        n = "intres", f = function(rctx)
139            return function(r)
140                --local key = r:key()
141                local res = mcp.internal(r)
142                local has_O, O, has_t, t = mgresflaga_ins(res)
143                return string.format("SERVER_ERROR O[%q]: %s t[%q]: %q\r\n",
144                    has_O, O, has_t, t)
145            end
146        end
147    })
148
149    local mgr = mcp.router_new({ map = {
150        sepkey = mgsepkey,
151        reshasf = mgreshasf,
152        reqkey = mgreqkey,
153        intres = mgintres,
154    }})
155    mcp.attach(mcp.CMD_MG, mgr)
156end
157