Skip to content

Commit 2fcf67e

Browse files
author
Olivier Bonnaure
committedOct 7, 2024·
fix: routing
1 parent c63f75e commit 2fcf67e

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed
 

‎.init.lua

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package.path = package.path .. ";config/?.lua;/zip/config/?.lua"
55
-- OTP = require("otp") -- OTP functions
66
require("utilities")
77
require("routes")
8-
print(EncodeJson(Routes))
98

109
ProgramMaxPayloadSize(10485760) -- 10 MB
1110

‎.lua/db2rest.lua

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
RestApiUrl = ""
22

33
local function init(db_config)
4-
print(EncodeJson(db_config))
54
RestApiUrl = db_config["url"] .. db_config["db_name"] .. "/"
65
end
76

‎.lua/luaonbeans.lua

+7
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,25 @@ function DefineRoutes(path, method)
181181
local route_found = false
182182
local final_route = false
183183

184+
local last_route_found = nil
185+
184186
Splat = {}
185187
if path == "/" then
186188
recognized_route = recognized_route[""]
187189
else
188190
for _, value in pairs(string.split(path, "/")) do
191+
189192
if final_route == false then
190193
if recognized_route[value] or recognized_route[value .. "*"] then
191194
if recognized_route[value .. "*"] then final_route = true end
192195
recognized_route = recognized_route[value] or recognized_route[value .. "*"]
196+
if recognized_route[""] then last_route_found = recognized_route[""] end
193197
route_found = true
194198
else
195199
route_found = false
196200
if recognized_route[":var"] then
197201
recognized_route = recognized_route[":var"]
202+
if recognized_route[""] then last_route_found = recognized_route[""] end
198203
local parser = Re.compile(recognized_route[":regex"])
199204
local matcher = { parser:search(value) }
200205
for i, match in ipairs(matcher) do
@@ -219,6 +224,8 @@ function DefineRoutes(path, method)
219224
Splat = tableSplat(Splat)
220225
end
221226

227+
if recognized_route == nil and last_route_found ~= nil then recognized_route = last_route_found end
228+
222229
if recognized_route ~= nil then
223230
recognized_route = string.split(recognized_route, "#")
224231
Params = table.merge(

‎config/routes.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Routes = { ["GET"] = { [""] = "welcome#index" } } --define root
33
CustomRoute("POST", "/upload/:collection/:key/:field", "uploads#upload")
44
CustomRoute("GET", "/o/:uuid/:format", "uploads#original_image")
55
CustomRoute("GET", "/r/:uuid/:width/:format", "uploads#resized_image_x", { [":width"] = "([0-9]+)" })
6-
CustomRoute("GET", "/r/:uuid/:width/:height/:format", "uploads#resized_image_x_y", { [":width"] = "([0-9]+)", [":height"] = "([0-9]+)" })
6+
CustomRoute("GET", "/xy/:uuid/:width/:height/:format", "uploads#resized_image_x_y", { [":width"] = "([0-9]+)", [":height"] = "([0-9]+)" })
77

88
-- CustomRoute("GET", "demo/with/:id/nested/:demo/route", "welcome#ban", {
99
-- [":demo"] = "([0-9]+)" -- you can define regex per params

‎specs/router_spec.lua

+73
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,79 @@ return {
4242
expect.equal(Params.controller, "customers")
4343
expect.equal(Params.action, "index")
4444

45+
Params = {}
46+
DefineRoutes("/customers", "POST")
47+
expect.equal(Params.controller, "customers")
48+
expect.equal(Params.action, "create")
49+
50+
Params = {}
51+
DefineRoutes("/customers/new", "GET")
52+
expect.equal(Params.controller, "customers")
53+
expect.equal(Params.action, "new")
54+
55+
Params = {}
56+
DefineRoutes("/customers/1", "GET")
57+
expect.equal(Params.controller, "customers")
58+
expect.equal(Params.action, "show")
59+
expect.equal(Params.customer_id, "1")
60+
61+
Params = {}
62+
DefineRoutes("/customers/1", "DELETE")
63+
expect.equal(Params.controller, "customers")
64+
expect.equal(Params.action, "delete")
65+
expect.equal(Params.customer_id, "1")
66+
67+
Params = {}
68+
DefineRoutes("/customers/1/edit", "GET")
69+
expect.equal(Params.controller, "customers")
70+
expect.equal(Params.action, "edit")
71+
expect.equal(Params.customer_id, "1")
72+
73+
Params = {}
74+
DefineRoutes("/customers/1", "PUT")
75+
expect.equal(Params.controller, "customers")
76+
expect.equal(Params.action, "update")
77+
expect.equal(Params.customer_id, "1")
78+
79+
Params = {}
80+
DefineRoutes("/customers/1/comments", "GET")
81+
expect.equal(Params.controller, "comments")
82+
expect.equal(Params.action, "index")
83+
expect.equal(Params.customer_id, "1")
84+
85+
Params = {}
86+
DefineRoutes("/customers/1/comments", "GET")
87+
expect.equal(Params.controller, "comments")
88+
expect.equal(Params.action, "index")
89+
expect.equal(Params.customer_id, "1")
90+
91+
Params = {}
92+
DefineRoutes("/customers/1/comments/1", "GET")
93+
expect.equal(Params.controller, "comments")
94+
expect.equal(Params.action, "show")
95+
expect.equal(Params.customer_id, "1")
96+
expect.equal(Params.comment_id, "1")
97+
98+
Params = {}
99+
DefineRoutes("/customers/1/comments/1", "PUT")
100+
expect.equal(Params.controller, "comments")
101+
expect.equal(Params.action, "update")
102+
expect.equal(Params.customer_id, "1")
103+
expect.equal(Params.comment_id, "1")
104+
105+
Params = {}
106+
DefineRoutes("/customers/1/comments", "POST")
107+
expect.equal(Params.controller, "comments")
108+
expect.equal(Params.action, "create")
109+
expect.equal(Params.customer_id, "1")
110+
111+
Params = {}
112+
DefineRoutes("/customers/1/comments/1", "DELETE")
113+
expect.equal(Params.controller, "comments")
114+
expect.equal(Params.action, "delete")
115+
expect.equal(Params.customer_id, "1")
116+
expect.equal(Params.comment_id, "1")
117+
45118
expect.equal(Routes["GET"]["customers"][""], "customers#index")
46119
expect.equal(Routes["GET"]["customers"]["new"], "customers#new")
47120
expect.equal(Routes["GET"]["customers"]["refresh"], "customers#refresh")

0 commit comments

Comments
 (0)
Please sign in to comment.