-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor]: add fastify support to subapp-server (#1642)
- Loading branch information
Showing
24 changed files
with
733 additions
and
81 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
"use strict"; | ||
|
||
/* eslint-disable no-magic-numbers, max-statements */ | ||
|
||
const _ = require("lodash"); | ||
const HttpStatus = require("./http-status"); | ||
const subAppUtil = require("subapp-util"); | ||
const HttpStatusCodes = require("http-status-codes"); | ||
|
||
const { makeErrorStackResponse } = require("./utils"); | ||
const { getSrcDir, setupRouteRender, searchRoutesFromFile } = require("./setup-hapi-routes"); | ||
|
||
module.exports = { | ||
fastifyPlugin: async (fastify, pluginOpts) => { | ||
const srcDir = getSrcDir(pluginOpts); | ||
|
||
// TODO: | ||
// const fromDir = await searchRoutesDir(srcDir, pluginOpts); | ||
// if (fromDir) { | ||
// // | ||
// } | ||
|
||
const { routes, topOpts } = searchRoutesFromFile(srcDir, pluginOpts); | ||
|
||
const subApps = await subAppUtil.scanSubAppsFromDir(srcDir); | ||
const subAppsByPath = subAppUtil.getSubAppByPathMap(subApps); | ||
|
||
const makeRouteHandler = (path, route) => { | ||
const routeOptions = Object.assign({}, topOpts, route); | ||
|
||
const routeRenderer = setupRouteRender({ subAppsByPath, srcDir, routeOptions }); | ||
const useStream = routeOptions.useStream !== false; | ||
|
||
return async (request, reply) => { | ||
try { | ||
const context = await routeRenderer({ | ||
content: { | ||
html: "", | ||
status: 200, | ||
useStream | ||
}, | ||
mode: "", | ||
request | ||
}); | ||
|
||
const data = context.result; | ||
const status = data.status; | ||
if (data instanceof Error) { | ||
// rethrow to get default error behavior below with helpful errors in dev mode | ||
throw data; | ||
} else if (status === undefined) { | ||
reply.type("text/html; charset=UTF-8").code(HttpStatusCodes.OK); | ||
return reply.send(data); | ||
} else if (HttpStatus.redirect[status]) { | ||
return reply.redirect(status, data.path); | ||
} else if ( | ||
HttpStatus.displayHtml[status] || | ||
(status >= HttpStatusCodes.OK && status < 300) | ||
) { | ||
reply.type("text/html; charset=UTF-8").code(status); | ||
return reply.send(data.html !== undefined ? data.html : data); | ||
} else { | ||
reply.code(status); | ||
return reply.send(data); | ||
} | ||
} catch (err) { | ||
reply.status(HttpStatusCodes.INTERNAL_SERVER_ERROR); | ||
if (process.env.NODE_ENV !== "production") { | ||
const responseHtml = makeErrorStackResponse(path, err); | ||
reply.type("text/html; charset=UTF-8"); | ||
return reply.send(responseHtml); | ||
} else { | ||
return reply.send("Internal Server Error"); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
for (const path in routes) { | ||
const route = routes[path]; | ||
|
||
const handler = makeRouteHandler(path, route); | ||
|
||
const defaultMethods = [].concat(route.methods || "get"); | ||
const paths = _.uniq([path].concat(route.paths).filter(x => x)).map(x => { | ||
if (typeof x === "string") { | ||
return { [x]: defaultMethods }; | ||
} | ||
return x; | ||
}); | ||
|
||
paths.forEach(pathObj => { | ||
_.each(pathObj, (method, xpath) => { | ||
fastify.route({ | ||
...route.settings, | ||
path: xpath, | ||
method: method.map(x => x.toUpperCase()), | ||
handler | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"use strict"; | ||
|
||
const hapiPlugin = require("./hapi-plugin"); | ||
const { fastifyPlugin } = require("./fastify-plugin"); | ||
|
||
module.exports = { hapiPlugin }; | ||
module.exports = { hapiPlugin, fastifyPlugin }; |
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
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
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
Oops, something went wrong.