mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2025-12-12 17:26:22 +00:00
fix: throw error if config import fails
feat: log used config in backend
This commit is contained in:
@@ -11,13 +11,13 @@
|
||||
*
|
||||
* To get more information about the meaning of specific fields, please use your IDE to read the TSDoc documentation.
|
||||
*
|
||||
* @type {import('../../src/storage/elasticsearch/types/elasticsearch-config.js')}
|
||||
* @type {import('../../src/storage/elasticsearch/types/elasticsearch-config.js').ElasticsearchConfigFile}
|
||||
*/
|
||||
const config = {
|
||||
internal: {
|
||||
database: {
|
||||
name: 'elasticsearch',
|
||||
version: '5.6',
|
||||
version: '8.4.2',
|
||||
query: {
|
||||
minMatch: '75%',
|
||||
queryType: 'dis_max',
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// @ts-check
|
||||
import {readFile, readdir} from 'fs/promises';
|
||||
import url from 'url';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* @example version(1, import.meta.url)
|
||||
@@ -27,10 +29,10 @@ export async function version(options, base) {
|
||||
* @returns {Promise<import('@openstapps/core').SCAppVersionInfo[]>}
|
||||
*/
|
||||
export async function versions(base) {
|
||||
const directory = await readdir(new URL(base));
|
||||
const versions = [...new Set(directory.map(it => it.replace(/\.\w+\.md$/, '')))].sort(
|
||||
(a, b) => -a.localeCompare(b, undefined, {numeric: true}),
|
||||
);
|
||||
const directory = await readdir(path.dirname(url.fileURLToPath(base)));
|
||||
const versions = [
|
||||
...new Set(directory.filter(it => it.endsWith('.md')).map(it => it.replace(/\.\w+\.md$/, ''))),
|
||||
].sort((a, b) => -a.localeCompare(b, undefined, {numeric: true}));
|
||||
|
||||
return Promise.all(versions.map(versionName => version({version: versionName}, base)));
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {cosmiconfig, PublicExplorer} from 'cosmiconfig';
|
||||
import {SCConfigFile} from '@openstapps/core';
|
||||
import path from 'path';
|
||||
import deepmerge from 'deepmerge';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
|
||||
const fallbackNamespace = 'default';
|
||||
const configPath = 'config';
|
||||
@@ -23,31 +24,25 @@ function configLoader(moduleName: string): PublicExplorer {
|
||||
* Find and load a config file
|
||||
*/
|
||||
async function findConfig<T>(moduleName: string, namespace = fallbackNamespace): Promise<T> {
|
||||
return configLoader(moduleName)
|
||||
.search(path.posix.join('.', configPath, namespace))
|
||||
.then(it => it!.config as T)
|
||||
.catch(() =>
|
||||
configLoader(moduleName)
|
||||
.search(path.posix.join('.', configPath, fallbackNamespace))
|
||||
.then(it => it!.config),
|
||||
);
|
||||
}
|
||||
const config = await configLoader(moduleName).search(path.posix.join('.', configPath, namespace));
|
||||
|
||||
/**
|
||||
* Loads a config file
|
||||
*/
|
||||
async function loadConfig<T>(moduleName: string): Promise<T> {
|
||||
const namespace = process.env.NODE_APP_INSTANCE;
|
||||
const database = process.env.NODE_CONFIG_ENV;
|
||||
|
||||
const config = await findConfig<T>(moduleName, namespace);
|
||||
if (database) {
|
||||
const databaseConfig = await findConfig<T>(database, namespace);
|
||||
return deepmerge(config, databaseConfig);
|
||||
if (config) {
|
||||
Logger.info(`Using ${namespace} config for ${moduleName}`);
|
||||
return config.config;
|
||||
} else {
|
||||
Logger.info(`Using ${fallbackNamespace} config for ${moduleName}`);
|
||||
return configLoader(moduleName)
|
||||
.search(path.posix.join('.', configPath, fallbackNamespace))
|
||||
.then(it => it!.config);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
export const backendConfig = await loadConfig<SCConfigFile>('backend');
|
||||
export const prometheusConfig = await loadConfig<unknown>('prometheus');
|
||||
const namespace = process.env.NODE_APP_INSTANCE;
|
||||
const database = process.env.NODE_CONFIG_ENV;
|
||||
|
||||
export const prometheusConfig = await findConfig<unknown>('prometheus', namespace);
|
||||
|
||||
const backendConfigWithoutDatabase = await findConfig<SCConfigFile>('backend', namespace);
|
||||
export const backendConfig = database
|
||||
? deepmerge(backendConfigWithoutDatabase, await findConfig<never>(database, namespace))
|
||||
: backendConfigWithoutDatabase;
|
||||
|
||||
Reference in New Issue
Block a user