Use a class for the application routes#1116
Conversation
solnic
left a comment
There was a problem hiding this comment.
@waiting-for-dev could we have the DSL at the class level so that there's no need to use routes block within the class?
I agree that it'd look nicer. In that case, probably the simplest solution involves letting the user inherit from |
@solnic, I've been looking into it, and it's not that easy. We are now just capturing the block and forwarding it to a As to using |
|
@waiting-for-dev isn't it possible to create a router instance at a class level and forward DSL methods to it? |
9dc292a to
dac4992
Compare
Yup, you're right. We can do it. Please, take a look at the last commit. However, I feel a bit uneasy moving such a thing to the global state. WDYT? If you think it's a better idea, I'll polish everything and add tests to it. |
jodosha
left a comment
There was a problem hiding this comment.
@waiting-for-dev The code has been changed since my last review. 🙂
I'm concerned that we're complicating the loading process to save us from the ugly routes block.
Remember that we're doing this change not because it's needed but because of syntax consistency with application settings.
It's fine to have consistency, but if that has to come at the cost of complexity, we should stop and reflect.
I agree that routes is ugly, but better having it, than complicating our lives. 😃
/cc @solnic
| ), | ||
| **Hanami.application.configuration.router.options, | ||
| ) do | ||
| use Hanami.application[:rack_monitor] |
There was a problem hiding this comment.
@waiting-for-dev Please remove those middleware from here and add them to the Rack middleware stack of the application.
In depth explanation. We can have three nested layers of Rack middleware stacks. Here's them listed from the outermost to the innermost:
config.ru- Application
- Router
The less stack we setup the better, for the app performance. When adding default middleware to the stack, we should target Application.
There was a problem hiding this comment.
I think it's food for another PR 🙂
| # @since 2.0.0 | ||
| class Routes | ||
| def self.router | ||
| @router ||= Router.new( |
There was a problem hiding this comment.
@waiting-for-dev ||= isn't thread-safe, is this code executed within a context that guarantees thread-safety?
This configuration heavily depends on global state of Hanami.application. How do we ensure that all these components are ready when we're instantiating the router? Shouldn't this be the role of the caller instead?
There was a problem hiding this comment.
Not a concern in the new implementation
Yeah, @jodosha, the last commit is more like a POC so that we can see how it'd look if we take it that way. I agree with your concerns, so I think that something like a |
|
Can we then rename it to |
We move the definition of routes as a block provided to
`Hanami::Application.routes` to its own class
`Hanami::Application::Routes`.
Users need to create a subclass and define the routes through a block
applied to the `.define` class method. As before, the hanami application
captures the context of the block and forwards it to
`Hanami::Application::Router` for its evaluation.
E.g.:
```ruby
require "hanami/application/routes"
module MyApp
class Routes < Hanami::Application::Routes
define do
slice :main, at: "/" do
root to: "home.show"
end
end
end
end
```
For consistency with settings, we move the routes path configuration to
the main `Hanami::Configuration` class.
dac4992 to
cbfc025
Compare
Hey @solnic, I agree with you. It's done now. I'm still leaving the @jodosha, could you please review it again? @timriley I'd also like to have your eyes on it 🙂 |
jodosha
left a comment
There was a problem hiding this comment.
@waiting-for-dev Thank you very much! LGTM 👍
timriley
left a comment
There was a problem hiding this comment.
Looks great! Can't wait to see what we can do with this object now that we have an actual class backing it.
…f CSRFProtection module (#33) * Adapt to routes being configured from a regular class See hanami/hanami#1116 * Remove copy of upstream Hanami::Action::CSRFProtection module We use dry-inflector as inflector for Zeitwerk. As of dry-inflector 0.2.0, it didn’t support CSRF as an acronym. Zeitwerk was looking for `CsrfProtection`, so we had to temporarily add a module with this exact inflection in this repository. As of dry-inflector 0.2.1, it added that CSRF acronym. That means now Zeitwerk is able to look for `CSRFProtection` module from hanami-controller. As per this change, we can remove `CsrfProtection` from the app template.
We move the definition of routes as a block provided to
Hanami::Application.routesto its own classHanami::Application::Routes.Users need to create a subclass and define the routes through a block
applied to the
.routesclass method. As before, the hanami applicationcaptures the context of the block and forwards it to
Hanami::Application::Routerfor its evaluation.E.g.:
For consistency with settings, we move the routes path configuration to
the main
Hanami::Configurationclass.