Skip to content

Commit

Permalink
AppSettings are always loaded from the same file as ServerSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Oct 22, 2020
1 parent 84e504b commit 3a0bf8c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 240 deletions.
63 changes: 28 additions & 35 deletions src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Cluster } from "../common/models/cluster/cluster";
import { DataCube } from "../common/models/data-cube/data-cube";
import { arraySum } from "../common/utils/general/general";
import { appSettingsToYAML } from "../common/utils/yaml-helper/yaml-helper";
import { ServerSettings } from "./models/server-settings/server-settings";
import { ServerSettings, ServerSettingsJS } from "./models/server-settings/server-settings";
import { loadFileSync } from "./utils/file/file";
import { SettingsManager } from "./utils/settings-manager/settings-manager";
import { SettingsStore } from "./utils/settings-store/settings-store";
Expand Down Expand Up @@ -166,24 +166,26 @@ export const START_SERVER = !PRINT_CONFIG;
const logger = START_SERVER ? LOGGER : NULL_LOGGER;

// Load server settings
var serverSettingsFilePath = parsedArgs["config"];
let configPath = parsedArgs["config"];

if (parsedArgs["examples"]) {
serverSettingsFilePath = path.join(__dirname, "../../config-examples.yaml");
configPath = path.join(__dirname, "../../config-examples.yaml");
}

var anchorPath: string;
var serverSettingsJS: any;
if (serverSettingsFilePath) {
anchorPath = path.dirname(serverSettingsFilePath);
let serverSettingsJS: ServerSettingsJS;
let configDirPath;
let configContent;
if (configPath) {
configDirPath = path.dirname(configPath);
try {
serverSettingsJS = loadFileSync(serverSettingsFilePath, "yaml");
logger.log(`Using config ${serverSettingsFilePath}`);
configContent = loadFileSync(configPath, "yaml");
serverSettingsJS = configContent;
logger.log(`Using config ${configPath}`);
} catch (e) {
exitWithError(`Could not load config from '${serverSettingsFilePath}': ${e.message}`);
exitWithError(`Could not load config from '${configPath}': ${e.message}`);
}
} else {
anchorPath = process.cwd();
configDirPath = process.cwd();
serverSettingsJS = {};
}

Expand All @@ -196,11 +198,12 @@ if (parsedArgs["server-host"]) {
if (parsedArgs["server-root"]) {
serverSettingsJS.serverRoot = parsedArgs["server-root"];
}
if (parsedArgs["auth"]) {
serverSettingsJS.auth = parsedArgs["auth"];
if (parsedArgs["verbose"]) {
serverSettingsJS.verbose = parsedArgs["verbose"];
}

export const VERBOSE = Boolean(parsedArgs["verbose"] || serverSettingsJS.verbose);
// TODO: Remove this export
export const VERBOSE = Boolean(serverSettingsJS.verbose);
export const SERVER_SETTINGS = ServerSettings.fromJS(serverSettingsJS);

// --- Sign of Life -------------------------------
Expand All @@ -210,28 +213,18 @@ if (START_SERVER) {

// --- Location -------------------------------

var settingsStore: SettingsStore = null;

if (serverSettingsFilePath) {
var settingsLocation = SERVER_SETTINGS.getSettingsLocation();
if (settingsLocation) {
switch (settingsLocation.getLocation()) {
case "file":
var settingsFilePath = path.resolve(anchorPath, settingsLocation.uri);
settingsStore = SettingsStore.fromReadOnlyFile(settingsFilePath, settingsLocation.getFormat());
break;
default:
exitWithError(`unknown location '${settingsLocation.location}'`);
}

} else {
settingsStore = SettingsStore.fromReadOnlyFile(serverSettingsFilePath, "yaml");
}
let settingsStore: SettingsStore;

if (configContent) {
const appSettings = AppSettings.fromJS(configContent, {});
// TODO: this validation should be done via #365
appSettings.validate();
settingsStore = SettingsStore.create(appSettings);
} else {
var initAppSettings = AppSettings.BLANK;
let initAppSettings = AppSettings.BLANK;

// If a file is specified add it as a dataCube
var fileToLoad = parsedArgs["file"];
const fileToLoad = parsedArgs["file"];
if (fileToLoad) {
initAppSettings = initAppSettings.addDataCube(new DataCube({
name: path.basename(fileToLoad, path.extname(fileToLoad)),
Expand All @@ -253,13 +246,13 @@ if (serverSettingsFilePath) {
}));
}

settingsStore = SettingsStore.fromTransient(initAppSettings);
settingsStore = SettingsStore.create(initAppSettings);
}

export const SETTINGS_MANAGER = new SettingsManager(settingsStore, {
logger,
verbose: VERBOSE,
anchorPath,
anchorPath: configDirPath,
initialLoadTimeout: SERVER_SETTINGS.getPageMustLoadTimeout()
});

Expand Down
14 changes: 0 additions & 14 deletions src/server/models/server-settings/server-settings.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ describe("ServerSettings", () => {
it("is an immutable class", () => {
testImmutableClass(ServerSettings, [
{},
{
port: 9090
},
{
port: 9091
},
Expand All @@ -50,17 +47,6 @@ describe("ServerSettings", () => {
serverRoot: "/swivs",
readinessEndpoint: "/status/readiness",
pageMustLoadTimeout: 901
},
{
port: 9091,
auth: "my_auth.js"
},
{
port: 9091,
settingsLocation: {
location: "file",
uri: "path/to/my/file.yaml"
}
}
]);
});
Expand Down
10 changes: 4 additions & 6 deletions src/server/models/server-settings/server-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

import { BackCompat, BaseImmutable } from "immutable-class";
import { SettingsLocation } from "../settings-location/settings-location";

export type Iframe = "allow" | "deny";
export type TrustProxy = "none" | "always";
Expand All @@ -30,10 +29,10 @@ export interface ServerSettingsValue {
livenessEndpoint?: string;
requestLogFormat?: string;
pageMustLoadTimeout?: number;
verbose?: boolean;
iframe?: Iframe;
trustProxy?: TrustProxy;
strictTransportSecurity?: StrictTransportSecurity;
settingsLocation?: SettingsLocation;
}

export interface ServerSettingsJS extends ServerSettingsValue {
Expand Down Expand Up @@ -69,13 +68,13 @@ export class ServerSettings extends BaseImmutable<ServerSettingsValue, ServerSet
{ name: "requestLogFormat", defaultValue: ServerSettings.DEFAULT_REQUEST_LOG_FORMAT },
{ name: "pageMustLoadTimeout", defaultValue: ServerSettings.DEFAULT_PAGE_MUST_LOAD_TIMEOUT },
{ name: "iframe", defaultValue: ServerSettings.DEFAULT_IFRAME, possibleValues: ServerSettings.IFRAME_VALUES },
{ name: "verbose", defaultValue: false, possibleValues: [false, true] },
{ name: "trustProxy", defaultValue: ServerSettings.DEFAULT_TRUST_PROXY, possibleValues: ServerSettings.TRUST_PROXY_VALUES },
{
name: "strictTransportSecurity",
defaultValue: ServerSettings.DEFAULT_STRICT_TRANSPORT_SECURITY,
possibleValues: ServerSettings.STRICT_TRANSPORT_SECURITY_VALUES
},
{ name: "settingsLocation", defaultValue: null, immutableClass: SettingsLocation }
}
];

static BACK_COMPATS: BackCompat[] = [{
Expand All @@ -94,9 +93,9 @@ export class ServerSettings extends BaseImmutable<ServerSettingsValue, ServerSet
public requestLogFormat: string;
public pageMustLoadTimeout: number;
public iframe: Iframe;
public verbose: boolean;
public trustProxy: TrustProxy;
public strictTransportSecurity: StrictTransportSecurity;
public settingsLocation: SettingsLocation;

constructor(parameters: ServerSettingsValue) {
super(parameters);
Expand All @@ -111,7 +110,6 @@ export class ServerSettings extends BaseImmutable<ServerSettingsValue, ServerSet
public getIframe: () => Iframe;
public getTrustProxy: () => TrustProxy;
public getStrictTransportSecurity: () => StrictTransportSecurity;
public getSettingsLocation: () => SettingsLocation;
}

BaseImmutable.finalize(ServerSettings);
53 changes: 0 additions & 53 deletions src/server/models/settings-location/settings-location.mocha.ts

This file was deleted.

96 changes: 0 additions & 96 deletions src/server/models/settings-location/settings-location.ts

This file was deleted.

Loading

0 comments on commit 3a0bf8c

Please sign in to comment.