2022-10-03 16:23:59 +04:00
|
|
|
import { loadConfig } from '@codex-team/config-loader';
|
2022-09-29 06:41:24 +08:00
|
|
|
import * as process from 'process';
|
|
|
|
import arg from 'arg';
|
|
|
|
import path from 'path';
|
2022-10-03 16:23:59 +04:00
|
|
|
import { z } from 'zod';
|
2022-09-29 06:41:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration for Hawk errors catcher
|
|
|
|
*/
|
|
|
|
const HawkConfig = z.object({
|
|
|
|
backendToken: z.string().optional(), // Hawk backend token
|
|
|
|
frontendToken: z.string().optional(), // Hawk frontend token
|
2022-10-03 16:23:59 +04:00
|
|
|
});
|
2022-09-29 06:41:24 +08:00
|
|
|
|
2022-10-08 14:26:11 +04:00
|
|
|
/**
|
|
|
|
* Config for local uploads driver
|
|
|
|
*/
|
|
|
|
const LocalUploadsConfig = z.object({
|
|
|
|
driver: z.literal('local'),
|
|
|
|
local: z.object({
|
|
|
|
path: z.string(), // path to the database directory
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config for S3 uploads driver
|
|
|
|
*/
|
|
|
|
const S3UploadsConfig = z.object({
|
|
|
|
driver: z.literal('s3'),
|
|
|
|
s3: z.object({
|
|
|
|
bucket: z.string(),
|
|
|
|
region: z.string(),
|
|
|
|
baseUrl: z.string(),
|
|
|
|
keyPrefix: z.string(),
|
|
|
|
accessKeyId: z.string(),
|
|
|
|
secretAccessKey: z.string(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type LocalUploadsConfig = z.infer<typeof LocalUploadsConfig>;
|
|
|
|
export type S3UploadsConfig = z.infer<typeof S3UploadsConfig>;
|
|
|
|
|
2022-10-03 16:23:59 +04:00
|
|
|
/**
|
|
|
|
* Config for local database driver
|
|
|
|
*/
|
2022-09-29 06:41:24 +08:00
|
|
|
const LocalDatabaseConfig = z.object({
|
|
|
|
driver: z.literal('local'),
|
|
|
|
local: z.object({
|
2022-10-03 16:23:59 +04:00
|
|
|
path: z.string(), // path to the database directory
|
|
|
|
}),
|
|
|
|
});
|
2022-09-29 06:41:24 +08:00
|
|
|
|
2022-10-03 16:23:59 +04:00
|
|
|
/**
|
|
|
|
* Config for MongoDB database driver
|
|
|
|
*/
|
|
|
|
const MongoDatabaseConfig = z.object({
|
|
|
|
driver: z.literal('mongodb'),
|
|
|
|
mongodb: z.object({
|
|
|
|
uri: z.string(), // MongoDB connection URI
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config for authentication
|
|
|
|
*/
|
2022-09-29 06:41:24 +08:00
|
|
|
const AuthConfig = z.object({
|
2022-10-03 16:23:59 +04:00
|
|
|
secret: z.string(), // Secret for JWT
|
|
|
|
});
|
2022-09-29 06:41:24 +08:00
|
|
|
|
2022-10-03 16:23:59 +04:00
|
|
|
/**
|
|
|
|
* Frontend configuration
|
|
|
|
*/
|
2022-09-29 06:41:24 +08:00
|
|
|
const FrontendConfig = z.object({
|
|
|
|
title: z.string(), // Title for pages
|
|
|
|
description: z.string(), // Description for pages
|
|
|
|
startPage: z.string(), // Start page
|
|
|
|
misprintsChatId: z.string().optional(), // Telegram chat id for misprints
|
|
|
|
yandexMetrikaId: z.string().optional(), // Yandex metrika id
|
|
|
|
carbon: z.object({
|
|
|
|
serve: z.string().optional(), // Carbon serve url
|
|
|
|
placement: z.string().optional(), // Carbon placement
|
|
|
|
}),
|
2022-10-03 16:23:59 +04:00
|
|
|
menu: z.array(z.union([z.string(), z.object({ title: z.string(),
|
|
|
|
uri: z.string() })])), // Menu for pages
|
|
|
|
});
|
2022-09-29 06:41:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Application configuration
|
|
|
|
*/
|
|
|
|
const AppConfig = z.object({
|
|
|
|
port: z.number(), // Port to listen on
|
|
|
|
host: z.string(), // Host to listen on
|
|
|
|
favicon: z.string().optional(), // Path or URL to favicon
|
2022-10-08 14:26:11 +04:00
|
|
|
uploads: z.union([LocalUploadsConfig, S3UploadsConfig]), // Uploads configuration
|
2022-09-29 06:41:24 +08:00
|
|
|
hawk: HawkConfig.optional().nullable(), // Hawk configuration
|
|
|
|
password: z.string(), // Password for admin panel
|
|
|
|
frontend: FrontendConfig, // Frontend configuration
|
|
|
|
auth: AuthConfig, // Auth configuration
|
2022-10-03 16:23:59 +04:00
|
|
|
database: z.union([LocalDatabaseConfig, MongoDatabaseConfig]), // Database configuration
|
|
|
|
});
|
2022-09-29 06:41:24 +08:00
|
|
|
|
|
|
|
export type AppConfig = z.infer<typeof AppConfig>;
|
|
|
|
|
|
|
|
const args = arg({ /* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
'--config': [ String ],
|
|
|
|
'-c': '--config',
|
|
|
|
});
|
|
|
|
|
|
|
|
const cwd = process.cwd();
|
2022-10-03 16:23:59 +04:00
|
|
|
const paths = (args['--config'] || [ './app-config.yaml' ]).map((configPath) => {
|
2022-09-29 06:41:24 +08:00
|
|
|
if (path.isAbsolute(configPath)) {
|
|
|
|
return configPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(cwd, configPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
const loadedConfig = loadConfig<AppConfig>(...paths);
|
|
|
|
|
2022-10-03 16:23:59 +04:00
|
|
|
const appConfig = AppConfig.parse(loadedConfig);
|
2022-09-29 06:41:24 +08:00
|
|
|
|
|
|
|
export default appConfig;
|