From ec906825e522ec49090d519b32a89ffc92d07be9 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 4 Nov 2022 18:24:43 +0200 Subject: [PATCH] chore: move http headers to separate config variable --- README.md | 2 +- src/config/app.ts | 26 ++++++++++++++------------ src/http/index.ts | 4 ++-- src/misc/load-config.ts | 6 ------ src/types/config/app.ts | 3 --- 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 0ea02b3..fb6af99 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ By default, server sends the following HTTP headers: - Various CORS headers, allowing access from anywhere. - Cache headers to cache responses for 604800 seconds (7 days). -To change headers, edit `headers` property in `src/config/app.ts`, then rebuild script. +To change headers, edit `httpHeaders` variable in `src/config/app.ts`, then rebuild script. ## Node vs PHP diff --git a/src/config/app.ts b/src/config/app.ts index a5e3e62..e58c937 100644 --- a/src/config/app.ts +++ b/src/config/app.ts @@ -15,18 +15,6 @@ export const appConfig: AppConfig = { // Cache root directory cacheRootDir: 'cache', - // HTTP headers to send - headers: [ - // CORS - 'Access-Control-Allow-Origin: *', - 'Access-Control-Allow-Methods: GET, OPTIONS', - 'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding', - 'Access-Control-Max-Age: 86400', - 'Cross-Origin-Resource-Policy: cross-origin', - // Cache - 'Cache-Control: public, max-age=604800, min-refresh=604800, immutable', - ], - // Host and port for server host: '0.0.0.0', port: 3000, @@ -59,6 +47,20 @@ export const appConfig: AppConfig = { allowFilterIconsByStyle: true, }; +/** + * HTTP headers to send to visitors + */ +export const httpHeaders: string[] = [ + // CORS + 'Access-Control-Allow-Origin: *', + 'Access-Control-Allow-Methods: GET, OPTIONS', + 'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding', + 'Access-Control-Max-Age: 86400', + 'Cross-Origin-Resource-Policy: cross-origin', + // Cache + 'Cache-Control: public, max-age=604800, min-refresh=604800, immutable', +]; + /** * Splitting icon sets */ diff --git a/src/http/index.ts b/src/http/index.ts index 274d9e1..86d7e6c 100644 --- a/src/http/index.ts +++ b/src/http/index.ts @@ -1,5 +1,5 @@ import fastify from 'fastify'; -import { appConfig } from '../config/app'; +import { appConfig, httpHeaders } from '../config/app'; import { runWhenLoaded } from '../data/loading'; import { iconNameRoutePartialRegEx, iconNameRouteRegEx, splitIconName } from '../misc/name'; import { generateAPIv1IconsListResponse } from './responses/collection-v1'; @@ -28,7 +28,7 @@ export async function startHTTPServer() { value: string; } const headers: Header[] = []; - appConfig.headers.forEach((item) => { + httpHeaders.forEach((item) => { const parts = item.split(':'); if (parts.length > 1) { headers.push({ diff --git a/src/misc/load-config.ts b/src/misc/load-config.ts index 4857d46..7a7a452 100644 --- a/src/misc/load-config.ts +++ b/src/misc/load-config.ts @@ -33,12 +33,6 @@ export function loadEnvConfig(env = process.env) { case 'string': cfg[key] = value; break; - - case 'object': - if (defaultValue instanceof Array) { - // Append one entry to array - defaultValue.push(value); - } } } } diff --git a/src/types/config/app.ts b/src/types/config/app.ts index df4c63b..827bec5 100644 --- a/src/types/config/app.ts +++ b/src/types/config/app.ts @@ -12,9 +12,6 @@ export interface AppConfig { // Without trailing '/' cacheRootDir: string; - // HTTP headers to send - headers: string[]; - // Host host: string;