Created
November 9, 2017 20:08
-
-
Save israeleriston/53e46c3bf5132b70c333e38ae7489f63 to your computer and use it in GitHub Desktop.
Loaded routes files using fastify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const filterFiles = require('filter-files') | |
const isDir = require('is-directory') | |
const { flatten } = require('lodash') | |
const isRouteFile = fileName => /((Route)|(Routes))\.js$/.test(fileName) | |
/** | |
* @method getRoutesFilesFromDirname | |
* @param {String} dirName | |
* @return {Array<String>} | |
*/ | |
const getRoutesFilesFromDirname = dirName => { | |
return filterFiles.sync(dirName, (fp, dir, files, recurse) => { | |
if (isRouteFile(fp)) { | |
return true | |
} | |
return isDir.sync(path.join(dir, fp)) | |
}, true) | |
} | |
/** | |
* @method loadRoutesByPath | |
* @param {String} dirName | |
* @return {Array<Function>} array of routes | |
*/ | |
const loadRoutesByPath = dirName => { | |
const routes = getRoutesFilesFromDirname(dirName) | |
.map(require) | |
return flatten(routes) | |
} | |
module.exports = loadRoutesByPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { pick, isArray } = require('lodash') | |
const wrapMiddleware = require('./wrapMiddleware') | |
const toArray = value => isArray(value) ? value : [value] | |
/** | |
* @method applyWrap | |
* @param {Array<Function>} handlers array of middlewares | |
* @return {Array<Function>} array of middlewares | |
*/ | |
const applyWrap = handlers => { | |
const last = handlers.pop() | |
handlers.push(wrapMiddleware(last)) | |
return handlers | |
} | |
/** | |
* @method getHandlers | |
* @param {Object} route | |
* @return {Array<Function>} array of middlewares | |
*/ | |
const getHandlers = route => { | |
const { useWrap } = route | |
console.log(' tem handler nesse cabra? ' + route.handler) | |
let handlers = toArray(route.handler) | |
if (useWrap) { | |
handlers = applyWrap(handlers) | |
} | |
return handlers | |
} | |
/** | |
* @method registerRoute | |
* @param {RestifyServer} server restofy instance | |
* @param {Object} route route object | |
*/ | |
const registerRoutes = (server, route) => { | |
const { method, url } = route | |
let options = pick(route, ['name', 'version']) | |
const handler = getHandlers(route) | |
// server.register(route) | |
server[method](url, options, handler) | |
} | |
module.exports = registerRoutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const loadRoutesByPath = require('../support/loadFiles') | |
const registerRoutes = require('../support/registerRoutes') | |
// registerRouter it method is call, to loaded files routes | |
const registerRouter = (server, dirname) => { | |
const routes = loadRoutesByPath(dirname) | |
routes.forEach(route => registerRoutes(server, route)) | |
} | |
module.exports = registerRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// all logic here | |
const fastify = require('fastify')() | |
const path = require('path') | |
const { registerRouter } = require('./src/config') | |
registerRouter(fastify, path.join(__dirname, './src/routes')) | |
// fastify.register(require('./src/routes/users/usersRoute')) | |
fastify.listen(3000, (err) => { | |
if (err) throw err | |
console.log(`server listening on ${fastify.server.address().port}`) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Boom = require('boom') | |
/** | |
* call a middleware | |
* @method tryCatch | |
* @param {Function} fn | |
* @return {Promise} | |
*/ | |
const tryCatch = fn => { | |
try { | |
return Promise.resolve(fn()) | |
} catch (e) { | |
return Promise.reject(e) | |
} | |
} | |
/** | |
* @method sendError | |
* @param {ResponseRestify} res response instance | |
* @param {Error} e | |
*/ | |
const sendError = (res, e) => { | |
const output = Boom.boomify(e).output | |
res.send(output.statusCode, output) | |
} | |
/** | |
* @method wrapMiddleware | |
* @param {Function} middleware route middleware | |
* @return {Function} | |
*/ | |
const wrapMiddleware = middleware => { | |
return (req, res) => { | |
const handler = () => middleware(req, res) | |
tryCatch(handler) | |
.then(result => { | |
res.json(result) | |
}) | |
.catch(sendError) | |
} | |
} | |
module.exports = wrapMiddleware |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment