X Tutup
Skip to content

[Proposal] Some thoughts on the Lapis router #710

@karai17

Description

@karai17

Accept strings as well as functions

Similar to how the return table of an action takes the require path of a view instead of the view itself, the router also taking the require path of an action instead of the action function itself feels like it would fit here.

Merge respond_to into match

Instead of needing to wrap actions with the helper function respond_to to extract the correct function for a request, it would be handy if this functionality was simply built in to match since it seems designed to be paired specifically with match anyway.

With both proposals in place, our routes could go from

local respond_to = require("lapis.application").respond_to
app:match("index", "/",      respond_to(require "actions.index"))
app:match("users", "/users", respond_to(require "actions.users"))
app:get(  "faq",   "/faq",   require "actions.faq")

to

app:match("index", "/",      "actions.index")
app:match("users", "/users", "actions.users")
app:get(  "faq",   "/faq",   "actions.faq")

Pseudocode

if type(handler) == "string" then
   handler = require(handler)
end

if type(handler) == "table" then
   handler = _respond_to(handler)
end

return handler and handler(req) or error()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup