From c87e8ee70b4bcce32482eb23091e3fb53664ad1b Mon Sep 17 00:00:00 2001 From: Clemens Moritz Date: Tue, 18 Apr 2023 21:45:55 +0200 Subject: [PATCH 1/5] Make trust proxy option configurable trust proxy option is now configurable via the TRUST_PROXY_IP environment variable. --- backend/src/app.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index a5f0d96..c962c46 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -4,11 +4,11 @@ import morgan from 'morgan'; import router from './router'; import wxService from './services/wx.service'; -const { PORT = 3000, BASE_PATH = '/api' } = process.env; +const { PORT = 3000, BASE_PATH = '/api', TRUST_PROXY_IP = false } = process.env; const app = express(); -app.set('trust proxy', true); +app.set('trust proxy', TRUST_PROXY_IP); app.use(morgan('combined')); app.use(BASE_PATH, router.router); From 5bcd588b0f55a0e3f5ca6a19657b0f661aba6877 Mon Sep 17 00:00:00 2001 From: Fionn Date: Wed, 19 Apr 2023 22:25:38 +0200 Subject: [PATCH 2/5] auto formatting --- backend/src/app.ts | 2 +- backend/src/controllers/region.controller.ts | 2 +- backend/src/services/config.service.ts | 2 +- backend/src/services/regions.service.ts | 4 +- backend/src/services/wx.service.ts | 46 ++++++++++---------- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index c962c46..1a42cd4 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -25,7 +25,7 @@ app.use((err, req: Request, res: Response, next: NextFunction) => { res.status(500).json({ msg: 'an error occurred' }); }); -nodesched.scheduleJob('regenerate data', '*/30 * * * * *', wxService.wrappedGenerateData) +nodesched.scheduleJob('regenerate data', '*/30 * * * * *', wxService.wrappedGenerateData); wxService.wrappedGenerateData(); const server = app.listen(PORT, () => { diff --git a/backend/src/controllers/region.controller.ts b/backend/src/controllers/region.controller.ts index 19a3fc1..c718144 100644 --- a/backend/src/controllers/region.controller.ts +++ b/backend/src/controllers/region.controller.ts @@ -15,7 +15,7 @@ export async function getRegion(req: express.Request, res: express.Response, nex const regionData = regionsService.getRegion(region); - if(!regionData) { + if (!regionData) { return next(); } diff --git a/backend/src/services/config.service.ts b/backend/src/services/config.service.ts index 94c7460..233e5b8 100644 --- a/backend/src/services/config.service.ts +++ b/backend/src/services/config.service.ts @@ -23,4 +23,4 @@ export function getConfig(): WxConfig { export default { getConfig, -} +}; diff --git a/backend/src/services/regions.service.ts b/backend/src/services/regions.service.ts index 4e15f8b..09b03f7 100644 --- a/backend/src/services/regions.service.ts +++ b/backend/src/services/regions.service.ts @@ -1,6 +1,6 @@ -import configService, { WxConfig, WxRegion } from "./config.service"; +import configService, { WxConfig, WxRegion } from './config.service'; -export function getRegions(): WxConfig["regions"] { +export function getRegions(): WxConfig['regions'] { return configService.getConfig().regions; } diff --git a/backend/src/services/wx.service.ts b/backend/src/services/wx.service.ts index 59e82b5..551b4d3 100644 --- a/backend/src/services/wx.service.ts +++ b/backend/src/services/wx.service.ts @@ -1,8 +1,8 @@ -import axios from "axios"; -import { WxFix } from "./config.service"; -import regionsService from "./regions.service"; +import axios from 'axios'; +import { WxFix } from './config.service'; +import regionsService from './regions.service'; -const cachedData: {[key: string]: WxData} = {}; +const cachedData: { [key: string]: WxData } = {}; const qnhLevelMapping = { 200: 390, @@ -12,7 +12,7 @@ const qnhLevelMapping = { 500: 180, 600: 140, 700: 100, - 850: 50 + 850: 50, }; const necessaryDatapoints = [ @@ -34,10 +34,10 @@ for (const qnh of Object.keys(qnhLevelMapping)) { } interface WxLevelData { - "T(K)": string; + 'T(K)': string; windspeed: string; windhdg: string; -}; +} export interface WxFixData { coords: { @@ -68,27 +68,27 @@ export async function getDataAtFix(fix: WxFix, index: number): Promise Date: Wed, 19 Apr 2023 22:29:09 +0200 Subject: [PATCH 3/5] documentation on environment variables --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 2f93384..8a1b337 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,17 @@ Tags: ## Configuration IASsure-WX can be configured using the `wx-config.json`-file. For now it contains test data but will include production data for at least the Langen FIR. It necessary, another file can be mounted on top of it (`/opt/wx-config.json`). You may also choose to make the necessary changes to the file in this repository. The file is documented in the schema definition file (`wx-config.schema.json`). + +## Environment Variables + +Some options can be defined using environment variables: + +```bash +# defines the port, the application will listen on +PORT=3000 +# defines the base path used for the api +BASE_PATH=/api +# defines ips that are allowed as proxy ips +# See http://expressjs.com/en/guide/behind-proxies.html +TRUST_PROXY_ID= +``` From 049f4d75b96d1fcd64092921806237e0d4432f9b Mon Sep 17 00:00:00 2001 From: Fionn Date: Wed, 19 Apr 2023 23:09:38 +0200 Subject: [PATCH 4/5] move config options to config file, add option to diable frontend --- README.md | 4 +++- backend/src/app.ts | 32 +++++++++++++++++++------------- backend/src/config.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 backend/src/config.ts diff --git a/README.md b/README.md index 8a1b337..ee6a0fe 100644 --- a/README.md +++ b/README.md @@ -28,5 +28,7 @@ PORT=3000 BASE_PATH=/api # defines ips that are allowed as proxy ips # See http://expressjs.com/en/guide/behind-proxies.html -TRUST_PROXY_ID= +TRUST_PROXY= +# set to true to disable /api-Endpoint. will also diable frontend. +DISABLE_DEFAULT_API_ENDPOINT= ``` diff --git a/backend/src/app.ts b/backend/src/app.ts index 1a42cd4..82184db 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -3,19 +3,26 @@ import nodesched from 'node-schedule'; import morgan from 'morgan'; import router from './router'; import wxService from './services/wx.service'; - -const { PORT = 3000, BASE_PATH = '/api', TRUST_PROXY_IP = false } = process.env; +import appConfig from './config'; const app = express(); -app.set('trust proxy', TRUST_PROXY_IP); +const config = appConfig(); + +app.set('trust proxy', config.trustProxy); app.use(morgan('combined')); -app.use(BASE_PATH, router.router); +if (config.apiBasePath) { + app.use(config.apiBasePath, router.router); +} + +if (!config.disableDefaultApiEndpoint) { + app.use('/api', router.router); -const frontendRoot = '/opt/frontend/dist'; -app.use(express.static(frontendRoot)); -app.use((req, res) => res.sendFile(`${frontendRoot}/index.html`)); + const frontendRoot = '/opt/frontend/dist'; + app.use(express.static(frontendRoot)); + app.use((req, res) => res.sendFile(`${frontendRoot}/index.html`)); +} // eslint-disable-next-line @typescript-eslint/no-unused-vars app.use((err, req: Request, res: Response, next: NextFunction) => { @@ -28,13 +35,13 @@ app.use((err, req: Request, res: Response, next: NextFunction) => { nodesched.scheduleJob('regenerate data', '*/30 * * * * *', wxService.wrappedGenerateData); wxService.wrappedGenerateData(); -const server = app.listen(PORT, () => { +const server = app.listen(config.port, () => { console.log( - `application is listening on port ${PORT}`, + `application is listening on port ${config.port}`, ); }); -const shutdown = (signal: string) => { +function processShutdown(signal: string) { console.log(`${signal} signal received. Shutting down.`); server.close((err) => { if (err) { @@ -45,7 +52,6 @@ const shutdown = (signal: string) => { console.log('Server closed'); process.exit(0); }); -}; +} -process.on('SIGTERM', () => shutdown('SIGTERM')); -process.on('SIGINT', () => shutdown('SIGINT')); +['SIGTERM', 'SIGINT'].map(signal => process.on(signal, processShutdown.bind(undefined, signal))); diff --git a/backend/src/config.ts b/backend/src/config.ts new file mode 100644 index 0000000..6e920bc --- /dev/null +++ b/backend/src/config.ts @@ -0,0 +1,28 @@ +export interface Config { + port: number; + apiBasePath: string; + disableDefaultApiEndpoint: boolean; + trustProxy: string | boolean; +} + +export default function appConfig(): Config { + const { + PORT, + BASE_PATH, + TRUST_PROXY, + DISABLE_DEFAULT_API_ENDPOINT, + } = process.env; + + let trustProxy: string | boolean = false; + + if (TRUST_PROXY == '*') { + trustProxy = true; + } + + return { + port: Number(PORT ?? 3000), + apiBasePath: BASE_PATH ?? '', + trustProxy, + disableDefaultApiEndpoint: DISABLE_DEFAULT_API_ENDPOINT == 'true', + }; +} From f64b200dcc7e8453356de79351388d16e37fa364 Mon Sep 17 00:00:00 2001 From: Fionn Date: Wed, 19 Apr 2023 23:11:39 +0200 Subject: [PATCH 5/5] bump verison to 1.2.0 --- backend/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/package.json b/backend/package.json index 9edd42e..50facd0 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "app", - "version": "1.1.0", + "version": "1.2.0", "description": "", "main": "index.js", "type": "module",