summaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
authorAya Morisawa <AyaMorisawa4869@gmail.com>2019-02-06 22:44:55 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2019-02-06 22:44:55 +0900
commit96bc17aa1014983d5e6bf8b4c05d898156995a0d (patch)
treefdf7c68bdbf3784988351004127db988d7c40c29 /src/config
parentFix bug (diff)
downloadmisskey-96bc17aa1014983d5e6bf8b4c05d898156995a0d.tar.gz
misskey-96bc17aa1014983d5e6bf8b4c05d898156995a0d.tar.bz2
misskey-96bc17aa1014983d5e6bf8b4c05d898156995a0d.zip
Check config on load (#4170)
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>
Diffstat (limited to 'src/config')
-rw-r--r--src/config/load.ts157
-rw-r--r--src/config/types.ts65
2 files changed, 140 insertions, 82 deletions
diff --git a/src/config/load.ts b/src/config/load.ts
index 5bb01f3d41..29e4a66f91 100644
--- a/src/config/load.ts
+++ b/src/config/load.ts
@@ -4,9 +4,10 @@
import * as fs from 'fs';
import { URL } from 'url';
+import $ from 'cafy';
import * as yaml from 'js-yaml';
-import { Source, Mixin } from './types';
import * as pkg from '../../package.json';
+import { fromNullable } from '../prelude/maybe';
/**
* Path of configuration directory
@@ -21,30 +22,148 @@ const path = process.env.NODE_ENV == 'test'
: `${dir}/default.yml`;
export default function load() {
- const config = yaml.safeLoad(fs.readFileSync(path, 'utf-8')) as Source;
+ const config = yaml.safeLoad(fs.readFileSync(path, 'utf-8'));
- const mixin = {} as Mixin;
+ if (typeof config.url !== 'string') {
+ throw 'You need to configure the URL.';
+ }
const url = validateUrl(config.url);
- config.url = normalizeUrl(config.url);
- mixin.host = url.host;
- mixin.hostname = url.hostname;
- mixin.scheme = url.protocol.replace(/:$/, '');
- mixin.ws_scheme = mixin.scheme.replace('http', 'ws');
- mixin.ws_url = `${mixin.ws_scheme}://${mixin.host}`;
- mixin.api_url = `${mixin.scheme}://${mixin.host}/api`;
- mixin.auth_url = `${mixin.scheme}://${mixin.host}/auth`;
- mixin.dev_url = `${mixin.scheme}://${mixin.host}/dev`;
- mixin.docs_url = `${mixin.scheme}://${mixin.host}/docs`;
- mixin.stats_url = `${mixin.scheme}://${mixin.host}/stats`;
- mixin.status_url = `${mixin.scheme}://${mixin.host}/status`;
- mixin.drive_url = `${mixin.scheme}://${mixin.host}/files`;
- mixin.user_agent = `Misskey/${pkg.version} (${config.url})`;
+ if (typeof config.port !== 'number') {
+ throw 'You need to configure the port.';
+ }
+
+ if (config.https != null) {
+ if (typeof config.https.key !== 'string') {
+ throw 'You need to configure the https key.';
+ }
+ if (typeof config.https.cert !== 'string') {
+ throw 'You need to configure the https cert.';
+ }
+ }
+
+ if (config.mongodb == null) {
+ throw 'You need to configure the MongoDB.';
+ }
+
+ if (typeof config.mongodb.host !== 'string') {
+ throw 'You need to configure the MongoDB host.';
+ }
+
+ if (typeof config.mongodb.port !== 'number') {
+ throw 'You need to configure the MongoDB port.';
+ }
+
+ if (typeof config.mongodb.db !== 'string') {
+ throw 'You need to configure the MongoDB database name.';
+ }
+
+ if (config.drive == null) {
+ throw 'You need to configure the drive.';
+ }
+
+ if (typeof config.drive.storage !== 'string') {
+ throw 'You need to configure the drive storage type.';
+ }
+
+ if (!$.str.or(['db', 'minio']).ok(config.drive.storage)) {
+ throw 'Unrecognized drive storage type is specified.';
+ }
+
+ if (config.drive.storage === 'minio') {
+ if (typeof config.drive.storage.bucket !== 'string') {
+ throw 'You need to configure the minio bucket.';
+ }
+
+ if (typeof config.drive.storage.prefix !== 'string') {
+ throw 'You need to configure the minio prefix.';
+ }
+
+ if (config.drive.storage.prefix.config == null) {
+ throw 'You need to configure the minio.';
+ }
+ }
+
+ if (config.redis != null) {
+ if (typeof config.redis.host !== 'string') {
+ throw 'You need to configure the Redis host.';
+ }
+
+ if (typeof config.redis.port !== 'number') {
+ throw 'You need to configure the Redis port.';
+ }
+ }
+
+ if (config.elasticsearch != null) {
+ if (typeof config.elasticsearch.host !== 'string') {
+ throw 'You need to configure the Elasticsearch host.';
+ }
+
+ if (typeof config.elasticsearch.port !== 'number') {
+ throw 'You need to configure the Elasticsearch port.';
+ }
+ }
+
+ const source = {
+ url: normalizeUrl(config.url as string),
+ port: config.port as number,
+ https: fromNullable(config.https).map(x => ({
+ key: x.key as string,
+ cert: x.cert as string,
+ ca: fromNullable<string>(x.ca)
+ })),
+ mongodb: {
+ host: config.mongodb.host as string,
+ port: config.mongodb.port as number,
+ db: config.mongodb.db as string,
+ user: fromNullable<string>(config.mongodb.user),
+ pass: fromNullable<string>(config.mongodb.pass)
+ },
+ redis: fromNullable(config.redis).map(x => ({
+ host: x.host as string,
+ port: x.port as number,
+ pass: fromNullable<string>(x.pass)
+ })),
+ elasticsearch: fromNullable(config.elasticsearch).map(x => ({
+ host: x.host as string,
+ port: x.port as number,
+ pass: fromNullable<string>(x.pass)
+ })),
+ disableHsts: typeof config.disableHsts === 'boolean' ? config.disableHsts as boolean : false,
+ drive: {
+ storage: config.drive.storage as string,
+ bucket: config.drive.bucket as string,
+ prefix: config.drive.prefix as string,
+ baseUrl: fromNullable<string>(config.drive.baseUrl),
+ config: config.drive.config
+ },
+ autoAdmin: typeof config.autoAdmin === 'boolean' ? config.autoAdmin as boolean : false,
+ proxy: fromNullable<string>(config.proxy),
+ clusterLimit: typeof config.clusterLimit === 'number' ? config.clusterLimit as number : Infinity,
+ };
+
+ const host = url.host;
+ const scheme = url.protocol.replace(/:$/, '');
+ const ws_scheme = scheme.replace('http', 'ws');
- if (config.autoAdmin == null) config.autoAdmin = false;
+ const mixin = {
+ host: url.host,
+ hostname: url.hostname,
+ scheme: scheme,
+ ws_scheme: ws_scheme,
+ ws_url: `${ws_scheme}://${host}`,
+ api_url: `${scheme}://${host}/api`,
+ auth_url: `${scheme}://${host}/auth`,
+ dev_url: `${scheme}://${host}/dev`,
+ docs_url: `${scheme}://${host}/docs`,
+ stats_url: `${scheme}://${host}/stats`,
+ status_url: `${scheme}://${host}/status`,
+ drive_url: `${scheme}://${host}/files`,
+ user_agent: `Misskey/${pkg.version} (${config.url})`
+ };
- return Object.assign(config, mixin);
+ return Object.assign(source, mixin);
}
function tryCreateUrl(url: string) {
diff --git a/src/config/types.ts b/src/config/types.ts
index 2ce9c0c80d..b133e64c25 100644
--- a/src/config/types.ts
+++ b/src/config/types.ts
@@ -1,64 +1,3 @@
-/**
- * ユーザーが設定する必要のある情報
- */
-export type Source = {
- repository_url?: string;
- feedback_url?: string;
- url: string;
- port: number;
- https?: { [x: string]: string };
- disableHsts?: boolean;
- mongodb: {
- host: string;
- port: number;
- db: string;
- user: string;
- pass: string;
- };
- redis: {
- host: string;
- port: number;
- pass: string;
- };
- elasticsearch: {
- host: string;
- port: number;
- pass: string;
- };
- drive?: {
- storage: string;
- bucket?: string;
- prefix?: string;
- baseUrl?: string;
- config?: any;
- };
+import load from "./load";
- autoAdmin?: boolean;
-
- proxy?: string;
-
- accesslog?: string;
-
- clusterLimit?: number;
-};
-
-/**
- * Misskeyが自動的に(ユーザーが設定した情報から推論して)設定する情報
- */
-export type Mixin = {
- host: string;
- hostname: string;
- scheme: string;
- ws_scheme: string;
- api_url: string;
- ws_url: string;
- auth_url: string;
- docs_url: string;
- stats_url: string;
- status_url: string;
- dev_url: string;
- drive_url: string;
- user_agent: string;
-};
-
-export type Config = Source & Mixin;
+export type Config = ReturnType<typeof load>;