Comments (5)
Additional thoughts, based on the Slack discussions:
Is it important to be able to build and transform routers at runtime?
I don't think so - I don't see a use-case for it and I'm afraid if we wanted to do it we might end up with a weird implementation
Is there a better way of defining the routes than matching on the requests?
Maybe, but I like matching on requests a lot - it's very easy to understand and surprisingly powerful. The only thing I dislike about it is that it's so verbose that the code formatter sometimes
ends up wrapping the lines with router entries, making them less readable. For example a route like
{%{method: :GET, path: ["users", _id, "contact_preferences"]}, UserPage},
could be replaced with a less verbose line with a very similar expressive power:
{{:GET, ["users", _id, "contact_preferences"]}, UserPage},
from raxx.
My initial thoughts are along the lines of this:
defmodule Foo.WWW do
use Raxx.Router
# different ways to define the middlewares for different sections
@compile_time_middlewares [{Foo.Static, "/some/path"}]
def api_pipeline() do
allowed_token = System.get_env("ALLOWED_TOKEN")
[{Foo.TokenAuthentication, [allowed_token: allowed_token]}]
end
def configurable_pipeline(options_passed_to_current_module) do
foo = Keyword.get(options_passed_to_current_module, :foo, :bar)
[{Foo.BusinessSpecificMiddleware, [foo: foo]}]
end
# routes
section &api_pipeline/0, [
{%{method: :GET, path: ["users", _id, "contact_preferences"]}, UserPreferences},
]
section [
{%{method: :GET, path: ["dashboard", "user_statistics"]}, Dashboard},
]
section @compile_time_middlewares, [
{%{method: :GET, path: []}, Foo.HomePage},
{_, Foo.NotFoundPage}
]
end
So in general:
- routes are grouped in sections (there might be a better name), and middleware pipelines are set per section.
- by the default middlewares list is an empty list
- the middlewares can be passed either as a list that's established at compile time, or a function that will be executed at runtime to return them
I'm not married to this idea, this is a jump-off point from my side.
from raxx.
My idea was to modify the tuple to include middlewares.
{%{method: :GET, path: ["users", _id, "contact_preferences"]}, UserPreferences, middlewares},
Although this is a very small change I no longer think it is a good idea:
- it is very verbose, as it needs to be applied to every route
- Supports compiletime only, unless middlewares was a function returning a list but that would be even more verbose.
For the above proposal:
- I like the introduction of sections, name is fine for me
- I would not (yet) separate configurable from unconfigurable stacks (would call them stacks instead of pipelines.
I would pass the options to all functions the same e.g. ignore when need be.# def api_pipeline() do def api_stack(_options) do
from raxx.
All good points. I like the standardisation of the middlewares creation functions to /1
.
I need to get my mind away from the word "pipeline" in general :D
from raxx.
Released in https://hex.pm/packages/raxx/0.17.3
from raxx.
Related Issues (20)
- Add middleware proof of concept HOT 10
- Remove handle_request from Raxx.Server behaviour HOT 2
- Should setting a body on a GET request raise an error. HOT 1
- Benchmark different Raxx.Stack state structures and switch to the fastest one HOT 1
- make `use Raxx.Middleware` a thing HOT 2
- Feature Proposal: Raxx.Context HOT 3
- allow to set multiple cookies HOT 5
- New 1.0 roadmap HOT 11
- Remove eex_html dep? HOT 5
- Handle unexpected HTTP verb HOT 6
- Incorrect parsing of request when path looks like an absolute url HOT 1
- Raxx.Session 1.0 Roadmap
- Raxx.View 1.0 Roadmap
- Raxx.Logger 1.0 Roadmap
- can we have both server streaming and simple request/response in same module?
- Fix specs, to show that Raxx.data can contain iodata
- Gleam HOT 6
- Raxx.View add a way to have page metadata in the layout
- Project status? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from raxx.