Coder Social home page Coder Social logo

Add a Middleware-aware Router about raxx HOT 5 CLOSED

nietaki avatar nietaki commented on June 1, 2024
Add a Middleware-aware Router

from raxx.

Comments (5)

nietaki avatar nietaki commented on June 1, 2024

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.

nietaki avatar nietaki commented on June 1, 2024

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.

CrowdHailer avatar CrowdHailer commented on June 1, 2024

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.

nietaki avatar nietaki commented on June 1, 2024

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.

CrowdHailer avatar CrowdHailer commented on June 1, 2024

Released in https://hex.pm/packages/raxx/0.17.3

from raxx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.