mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2025-12-17 03:36: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.
|
* 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 = {
|
const config = {
|
||||||
internal: {
|
internal: {
|
||||||
database: {
|
database: {
|
||||||
name: 'elasticsearch',
|
name: 'elasticsearch',
|
||||||
version: '5.6',
|
version: '8.4.2',
|
||||||
query: {
|
query: {
|
||||||
minMatch: '75%',
|
minMatch: '75%',
|
||||||
queryType: 'dis_max',
|
queryType: 'dis_max',
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import {readFile, readdir} from 'fs/promises';
|
import {readFile, readdir} from 'fs/promises';
|
||||||
|
import url from 'url';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @example version(1, import.meta.url)
|
* @example version(1, import.meta.url)
|
||||||
@@ -27,10 +29,10 @@ export async function version(options, base) {
|
|||||||
* @returns {Promise<import('@openstapps/core').SCAppVersionInfo[]>}
|
* @returns {Promise<import('@openstapps/core').SCAppVersionInfo[]>}
|
||||||
*/
|
*/
|
||||||
export async function versions(base) {
|
export async function versions(base) {
|
||||||
const directory = await readdir(new URL(base));
|
const directory = await readdir(path.dirname(url.fileURLToPath(base)));
|
||||||
const versions = [...new Set(directory.map(it => it.replace(/\.\w+\.md$/, '')))].sort(
|
const versions = [
|
||||||
(a, b) => -a.localeCompare(b, undefined, {numeric: true}),
|
...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)));
|
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 {SCConfigFile} from '@openstapps/core';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import deepmerge from 'deepmerge';
|
import deepmerge from 'deepmerge';
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
|
|
||||||
const fallbackNamespace = 'default';
|
const fallbackNamespace = 'default';
|
||||||
const configPath = 'config';
|
const configPath = 'config';
|
||||||
@@ -23,31 +24,25 @@ function configLoader(moduleName: string): PublicExplorer {
|
|||||||
* Find and load a config file
|
* Find and load a config file
|
||||||
*/
|
*/
|
||||||
async function findConfig<T>(moduleName: string, namespace = fallbackNamespace): Promise<T> {
|
async function findConfig<T>(moduleName: string, namespace = fallbackNamespace): Promise<T> {
|
||||||
|
const config = await configLoader(moduleName).search(path.posix.join('.', configPath, namespace));
|
||||||
|
|
||||||
|
if (config) {
|
||||||
|
Logger.info(`Using ${namespace} config for ${moduleName}`);
|
||||||
|
return config.config;
|
||||||
|
} else {
|
||||||
|
Logger.info(`Using ${fallbackNamespace} config for ${moduleName}`);
|
||||||
return configLoader(moduleName)
|
return configLoader(moduleName)
|
||||||
.search(path.posix.join('.', configPath, namespace))
|
|
||||||
.then(it => it!.config as T)
|
|
||||||
.catch(() =>
|
|
||||||
configLoader(moduleName)
|
|
||||||
.search(path.posix.join('.', configPath, fallbackNamespace))
|
.search(path.posix.join('.', configPath, fallbackNamespace))
|
||||||
.then(it => it!.config),
|
.then(it => it!.config);
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a config file
|
|
||||||
*/
|
|
||||||
async function loadConfig<T>(moduleName: string): Promise<T> {
|
|
||||||
const namespace = process.env.NODE_APP_INSTANCE;
|
const namespace = process.env.NODE_APP_INSTANCE;
|
||||||
const database = process.env.NODE_CONFIG_ENV;
|
const database = process.env.NODE_CONFIG_ENV;
|
||||||
|
|
||||||
const config = await findConfig<T>(moduleName, namespace);
|
export const prometheusConfig = await findConfig<unknown>('prometheus', namespace);
|
||||||
if (database) {
|
|
||||||
const databaseConfig = await findConfig<T>(database, namespace);
|
|
||||||
return deepmerge(config, databaseConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
const backendConfigWithoutDatabase = await findConfig<SCConfigFile>('backend', namespace);
|
||||||
}
|
export const backendConfig = database
|
||||||
|
? deepmerge(backendConfigWithoutDatabase, await findConfig<never>(database, namespace))
|
||||||
export const backendConfig = await loadConfig<SCConfigFile>('backend');
|
: backendConfigWithoutDatabase;
|
||||||
export const prometheusConfig = await loadConfig<unknown>('prometheus');
|
|
||||||
|
|||||||
Reference in New Issue
Block a user