diff --git a/README.md b/README.md index 2f93384..ee6a0fe 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,19 @@ 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= +# set to true to disable /api-Endpoint. will also diable frontend. +DISABLE_DEFAULT_API_ENDPOINT= +``` 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", diff --git a/backend/src/app.ts b/backend/src/app.ts index a5f0d96..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' } = process.env; +import appConfig from './config'; const app = express(); -app.set('trust proxy', true); +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) => { @@ -25,16 +32,16 @@ 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, () => { +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', + }; +} 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