Big Bang
This commit is contained in:
17
app/routing/Middleware.js
Normal file
17
app/routing/Middleware.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Global Middleware Definitions
|
||||
* -------------------------------------------------------------
|
||||
* These middleware are applied, in order, before every request that
|
||||
* Flitter handles, regardless of request type. Each middleware class
|
||||
* can be referenced using the middleware's Flitter canonical name.
|
||||
*
|
||||
* Route-specific middleware should be specified in the corresponding
|
||||
* routes file.
|
||||
*/
|
||||
const Middleware = [
|
||||
|
||||
// 'MiddlewareName',
|
||||
|
||||
]
|
||||
|
||||
module.exports = exports = Middleware
|
||||
31
app/routing/middleware/HomeLogger.middleware.js
Normal file
31
app/routing/middleware/HomeLogger.middleware.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* HomeLogger Middleware
|
||||
* -------------------------------------------------------------
|
||||
* This is a sample middleware. It simply prints a console message when
|
||||
* the route that it is tied to is accessed. By default, it is called if
|
||||
* the '/' route is accessed. It can be injected in routes globally using
|
||||
* the global mw() function.
|
||||
*/
|
||||
const Middleware = require('libflitter/middleware/Middleware')
|
||||
class HomeLogger extends Middleware {
|
||||
static get services() {
|
||||
return [...super.services, 'output']
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the middleware test.
|
||||
* This method is required by all Flitter middleware.
|
||||
* It should either call the next function in the stack,
|
||||
* or it should handle the response accordingly.
|
||||
*/
|
||||
test(req, res, next, args) {
|
||||
this.output.debug('Home was accessed!')
|
||||
|
||||
/*
|
||||
* Call the next function in the stack.
|
||||
*/
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HomeLogger
|
||||
40
app/routing/middleware/util/Config.middleware.js
Normal file
40
app/routing/middleware/util/Config.middleware.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Config Middleware
|
||||
* -------------------------------------------------------------
|
||||
* Checks the specified configuration key (and optionally the value).
|
||||
* If the configuration matches the required value, the request can
|
||||
* proceed. Otherwise, a 404 will be returned.
|
||||
*
|
||||
* To use, add the call to your route's middleware:
|
||||
* _flitter.mw('util:Config', {
|
||||
* key: 'server.environment',
|
||||
* value: 'development'
|
||||
* })
|
||||
*
|
||||
* In this case, the request would be allowed to proceed in the case:
|
||||
* _flitter.config('server.environment') === 'development'
|
||||
*
|
||||
* The 'value' attribute is optional. If none is provided, the request
|
||||
* can proceed if the config value is truthy.
|
||||
*/
|
||||
const Middleware = require('libflitter/middleware/Middleware')
|
||||
class Config extends Middleware {
|
||||
static get services() {
|
||||
return [...super.services, 'configs']
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the middleware test.
|
||||
*/
|
||||
test(req, res, next, args = {}){
|
||||
if ( !args.key ) return res.error(500)
|
||||
|
||||
const config = this.configs.get(args.key)
|
||||
|
||||
if ( !args.value && !config ) return res.error(404)
|
||||
else if ( args.value && args.value !== config ) return res.error(404)
|
||||
else return next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Config
|
||||
63
app/routing/routers/index.routes.js
Normal file
63
app/routing/routers/index.routes.js
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Index Routes
|
||||
* -------------------------------------------------------------
|
||||
* This is a sample routes file. Routes and their handlers should be
|
||||
* defined here, but no logic should occur.
|
||||
*/
|
||||
const index = {
|
||||
|
||||
/*
|
||||
* Define the prefix applied to each of these routes.
|
||||
* For example, if prefix is '/auth':
|
||||
* '/' becomes '/auth'
|
||||
* '/login' becomes '/auth/login'
|
||||
*/
|
||||
prefix: '/',
|
||||
|
||||
/*
|
||||
* Define middleware that should be applied to all
|
||||
* routes defined in this file. Middleware should be
|
||||
* included using its non-prefixed canonical name.
|
||||
*
|
||||
* You can pass arguments along to a middleware by
|
||||
* specifying it as an array where the first element
|
||||
* is the canonical name of the middleware and the
|
||||
* second element is the argument passed to the
|
||||
* handler's exec() method.
|
||||
*/
|
||||
middleware: [
|
||||
['HomeLogger', {note: 'arguments can be specified as the second element in this array'}],
|
||||
// 'MiddlewareName', // Or without arguments
|
||||
],
|
||||
|
||||
/*
|
||||
* Define GET routes.
|
||||
* These routes are registered as GET methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of canonical references to controller methods
|
||||
* or middleware that are applied in order.
|
||||
*/
|
||||
get: {
|
||||
// handlers should be a list of either controller:: or middleware:: references
|
||||
// e.g. middleware::HomeLogger
|
||||
// e.g. controller::Home.welcome
|
||||
'/': [ 'controller::Home.welcome' ],
|
||||
|
||||
// Placeholder for auth dashboard. You'd replace this with
|
||||
// your own route protected by 'middleware::auth:UserOnly'
|
||||
'/dash': [ 'controller::Home.welcome' ],
|
||||
},
|
||||
|
||||
/*
|
||||
* Define POST routes.
|
||||
* These routes are registered as POST methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of canonical references to controller methods
|
||||
* or middleware that are applied in order.
|
||||
*/
|
||||
post: {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = exports = index
|
||||
Reference in New Issue
Block a user