fabrix-app / spool-router

Spool: Router for Fabrix
Other
4 stars 0 forks source link

Multiple Prefixes Support #13

Open scott-wyatt opened 6 years ago

scott-wyatt commented 6 years ago

Issue Description

Feature: Support Multiple Prefixes by providing a handler version.

Concept

{
  // ...
  '/example/test': {
    'GET': {
       versions: { 
           'ExampleController.get': {
              config: {
                 prefix: 'prefix.one'
                 pre: [ 'ExamplePolicy.get' ]
              }
          },
          'ExampleController.getTwo': {
                config: {
                 prefix: 'prefix.two'
                 pre: [ 'ExamplePolicy.get' ]
              }
           }
        }
    },
    'POST': {
      handler: 'ExampleController.post',
      config: {
        prefix: '/api/v2'
        pre: [ 'ExamplePolicy.post' ]
      }
    }
  }
  // ...
}
jaumard commented 5 years ago

is this live ? because PR was merged ^^

scott-wyatt commented 5 years ago

I wish! It’s tricky because of a function called splitRoute that handles generating all the routes based on the config, it’s started, but not fully working.

On Oct 23, 2019, at 7:00 AM, Jaumard notifications@github.com wrote:

 is this live ? because PR was merged ^^

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

jaumard commented 5 years ago

I think the router should pre-rebuild the URL from the config to have /api/v1/whatever /api/v2/whatever. And then go to splitRoute that should do his job just fine ^^

Another way to support this is with the default prefix, for example support prefix: '/api/v1 and prefix: '[/api/v1, /api/v2] like this globally all endpoint are available for both endpoints.

Later on the controller something like req.version will give v1 or v2 and you can manage that here. It's another way of seeing version support :) but maybe less open to fine tuning

scott-wyatt commented 5 years ago

So the first part, that's kind of already possible: you can make the route just

/api/v1/<route>: {
  GET: 'myController.<myMethod>',
  config: {
     app: {
        version: 1
     }
   }
},
/api/v2/<route>: {
  GET: 'myController.<myMethod>',
  config: {
     app: {
        version: 2
     }
   }
}

And then get the app.version in the controller from the match on the webserver.

The second part: auto making everything to have a 2nd prefix would kill some of my apps that already have 800 routes lol.

Right now, there are at least 5 different ways to configure the router, which is kind of crazy, which is what the function splitRoute handles.

The other thing I've been thinking a lot about is route scope, and how we could make routes scoped for things like oAuth standards etc. Authentication is something almost every app needs, and there's no real good support for it in fabrix unless you want to use spool-passport and spool-permissions, even then, I almost always rewrite that logic in my own apps because they are too general for what I want to do.