Skip to content

Commit

Permalink
Add hook for initializing subapp Server code (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuverma authored Jun 15, 2021
1 parent 8630828 commit 04dcdd6
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/subapp-server/lib/fastify-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function registerFastifyRoutesFromFile({ fastify, srcDir, routes, topOpts
routeOptions.uiConfig = _.get(fastify, "settings.app.config.ui", {});

const routeRenderer = routesFromFile.setupRouteTemplate({
server: fastify,
subAppsByPath,
srcDir,
routeOptions
Expand Down
26 changes: 25 additions & 1 deletion packages/subapp-server/lib/routes-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ const {

const { getDefaultRouteOptions, updateFullTemplate } = require("./utils");

function setupRouteTemplate({ subAppsByPath, srcDir, routeOptions }) {
/**
* hook to tap into subapp server before server starts.
* Good place to register new api routes or any other enhancements of fastify server.
* @param {object} server Underlying framework's server instance
* @param {object} subAppsByPath map of absolute path and subapp's manifest
* @returns {Void} Returns nothing.
*/
function _setupSubappServer(server, subAppsByPath) {
const subAppServers = Object.keys(subAppsByPath).map((subAppPath) => {
const subAppName = subAppsByPath[subAppPath].name;
return subAppUtil.loadSubAppServerByName(subAppName, false);
}).filter(x => x && x.setup);

//invoke the setup method of subapp's server code
if (subAppServers && subAppServers.length > 0) {
for (const subAppServer of subAppServers) {
subAppServer.setup(server);
}
}
}

function setupRouteTemplate({ server, subAppsByPath, srcDir, routeOptions }) {
updateFullTemplate(routeOptions.dir, routeOptions);
const chunkSelector = resolveChunkSelector(routeOptions);
routeOptions.__internals = { chunkSelector };
Expand Down Expand Up @@ -43,6 +64,9 @@ function setupRouteTemplate({ subAppsByPath, srcDir, routeOptions }) {
options: options || {}
};
});

// Call setup method if subappServer exposes the same
_setupSubappServer(server, subAppsByPath);
}

const routeHandler = templateRouting.makeRouteTemplateSelector(routeOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/subapp-server/lib/setup-hapi-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async function registerRoutesFromFile({ server, srcDir, routes, topOpts }) {

// setup the template for rendering the route
const routeRenderer = routesFromFile.setupRouteTemplate({
server,
subAppsByPath,
srcDir,
routeOptions
Expand Down
17 changes: 17 additions & 0 deletions packages/subapp-server/test/data/fastify-plugin-test/demo3/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { loadSubApp } from "subapp-react";

const Demo3 = () => {
return (
<div style={{ padding: "5px", border: "solid", marginLeft: "15%", marginRight: "15%" }}>
<p>subapp demo3</p>
<p>
<a href="https://wmlink.wal-mart.com/electrode">Electrode Docs</a>
</p>
</div>
);
};

export default loadSubApp({
Component: Demo3,
name: "Demo3"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
setup(server) {
server.route({
path: "/api/demo3/testsetup",
method: "GET",
handler: function (request, reply) {
reply.send({
msg: "Route set by subappServer"
});
}
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
type: "app",
entry: "./app.js",
serverEntry: "./server.js",
name: "demo3"
};
58 changes: 56 additions & 2 deletions packages/subapp-server/test/spec/fastify-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ describe("fastify-plugin", function () {
connection: { port: 0, host: "localhost" }
});

const srcDir = Path.join(__dirname, "../data/fastify-plugin-test");

/**
* Patch APP_SRC_DIR as subapp-util relies on this env variable
* else it takes relative path of lib or src
*
* srcDir as PluginOpts doesn't get used in subapp-util
*/
process.env.APP_SRC_DIR = srcDir;

const opt = {
srcDir: Path.join(__dirname, "../data/fastify-plugin-test"),
srcDir,
loadRoutesFrom: "routes.js",
stats: Path.join(__dirname, "../data/fastify-plugin-test/stats.json")
};
Expand All @@ -32,7 +42,51 @@ describe("fastify-plugin", function () {
});
});
},
runFinally(() => server.close())
runFinally(() => {
server.close();
delete process.env.APP_SRC_DIR;
})
);
}).timeout(5000);

it("invokes subappServer's setup if it exists", async () => {
const server = await require("@xarc/fastify-server")({
deferStart: true,
connection: { port: 0, host: "localhost" }
});

const srcDir = Path.join(__dirname, "../data/fastify-plugin-test");

/**
* Patch APP_SRC_DIR as subapp-util relies on this env variable
* else it takes relative path of lib or src
*
* srcDir as PluginOpts doesn't get used in subapp-util
*/
process.env.APP_SRC_DIR = srcDir;

const opt = {
srcDir,
loadRoutesFrom: "routes.js",
stats: Path.join(__dirname, "../data/fastify-plugin-test/stats.json")
};

return asyncVerify(
runTimeout(4500),
() => fastifyPlugin(server, opt),
() => server.start(),
async () => {
const result = await server.inject({
method: "GET",
url: `http://localhost:${server.server.address().port}/api/demo3/testsetup`
});
expect(result.statusCode).to.equal(200);
expect(result.json()).to.deep.equal({ msg: "Route set by subappServer" });
},
runFinally(async () => {
server.close();
delete process.env.APP_SRC_DIR;
})
);
}).timeout(5000);

Expand Down
5 changes: 5 additions & 0 deletions packages/subapp-server/test/spec/setup-hapi-routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe("setupSubAppHapiRoutes", () => {
let server;
let stubPathResolve;
let stubRouteHandler;
let cachedAppSrcDir;

const stubWithArgs = (lib, method, argsFakeCallPairs) => {
const stubbed = sinon.stub(lib, method);
argsFakeCallPairs.forEach(([args, fakeCall]) =>
Expand Down Expand Up @@ -70,12 +72,15 @@ describe("setupSubAppHapiRoutes", () => {
logLevel: "none"
}
});
cachedAppSrcDir = process.env.APP_SRC_DIR;
delete process.env.APP_SRC_DIR;
});

afterEach(async () => {
await server.stop();
if (stubPathResolve) stubPathResolve.restore();
if (stubRouteHandler) stubRouteHandler.restore();
process.env.APP_SRC_DIR = cachedAppSrcDir;
});

it.skip("should setup subapp routes with `templateFile` specified in options", async () => {
Expand Down

0 comments on commit 04dcdd6

Please sign in to comment.