feat(router): add a router plugin
This builds on top of #130
Adds a router plugin.
I got tricked for a while, as I had forgotten that the string paths were regular expressions. This post is left for other users who may wonder why pegas is not serving matching the expected path.
Any of the "magic characters"
( ) . % + - * ? [ ^ $
Need to be escaped with %, for example the route "/catch-fish"
local routes = {
["/catch%-fish"] = {
GET = function(req, resp)
resp:statusCode(200)
resp:addHeader("Content-Type","text/html")
resp:write("catch")
end,
},
I do not consider this a bug, its likely a bug in my comprehension.
@wmealing I think that's actually a bug, we should (for simplicity sake) escape the path before matching. Such that a path "/my-cool-path/{parameter}" can be used without manually escaping the "-" characters.
Other things:
- I think the shortcut on the path level (passing a function instead of a table) should go. Require users to be explicit and add a wildcard method handler "*" instead.
- The examples currently refer to a JSON lib, which was removed, so they must be updated.
- The last segment should be configurable as
"{parameter+}"where the "+" means matching more than 1 segment (normally parameters are limited to 1 segment) - Test that multiple router plugins can be added, such that multiple independent API components can be served from their own plugin. For example:
- first plugin on: GET "/my/app"
- second plugin on: POST "/my/app"
- Test that we unescape the path before matching, eg incoming
"/my%20path"should match a configured router path"/my path"(also applies to the path-prefix on router level) - Per router a list of hosts to match (currently only matches on a path prefix)
Any input appreciated.