-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Fetch config from server in frontend
Instead of having it built into the application at webpack time, it is fetched at load time from the server. This adds an express route named `/config` that returns a Status with the project configuration. The client's entry page now needs to asynchronously fetch it from server using this route and the page is only rendered when the response is back. If there is an error while getting the configuration, the MaintenancePage is displayed. Because the initial frontend configuration is now asynchronous at the application start, the i18n.config is converted to a function, which initializes the i18n object that should be called once the configuration is fetched.
- Loading branch information
Showing
8 changed files
with
144 additions
and
60 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/chaire-lib-backend/src/api/__tests__/config.routes.test.ts
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,40 @@ | ||
/* | ||
* Copyright 2023, Polytechnique Montreal and contributors | ||
* | ||
* This file is licensed under the MIT License. | ||
* License text available at https://opensource.org/licenses/MIT | ||
*/ | ||
import express, { RequestHandler } from 'express'; | ||
import request from 'supertest'; | ||
import * as Status from 'chaire-lib-common/lib/utils/Status'; | ||
|
||
import configRoutes from '../config.routes'; | ||
|
||
jest.mock('../../config/server.config', () => ({ | ||
mapDefaultCenter: { lon: -3, lat: -3 }, | ||
separateAdminLoginPage: false, | ||
projectShortname: 'unitTest' | ||
})); | ||
|
||
const app = express(); | ||
// FIXME Since upgrading @types/node, the types are wrong and we get compilation error. It is documented for example https://github.com/DefinitelyTyped/DefinitelyTyped/issues/53584 the real fix would require upgrading a few packages and may have side-effects. Simple casting works for now. | ||
app.use(express.json({ limit: '500mb' }) as RequestHandler); | ||
app.use(express.urlencoded({extended: true}) as RequestHandler); | ||
configRoutes(app); | ||
|
||
test('Get config', async () => { | ||
const res = await request(app) | ||
.get(`/config`) | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', 'application/json; charset=utf-8') | ||
.expect(200); | ||
|
||
expect(Status.isStatusOk(res.body)).toEqual(true); | ||
const config = Status.unwrap(res.body); | ||
expect(config).toEqual({ | ||
mapDefaultCenter: { lon: -3, lat: -3 }, | ||
separateAdminLoginPage: false, | ||
projectShortname: 'unitTest' | ||
}); | ||
|
||
}); |
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,16 @@ | ||
/* | ||
* Copyright 2023, Polytechnique Montreal and contributors | ||
* | ||
* This file is licensed under the MIT License. | ||
* License text available at https://opensource.org/licenses/MIT | ||
*/ | ||
import express from 'express'; | ||
// TODO This config is both server and project config. It should only include the project config to be typed later | ||
import config from '../config/server.config'; | ||
import * as Status from 'chaire-lib-common/lib/utils/Status'; | ||
|
||
export default function (app: express.Express) { | ||
app.get('/config', (req, res) => { | ||
return res.status(200).json(Status.createOk(config)); | ||
}); | ||
} |
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
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