ionian/src/setting.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-01-09 20:29:01 +09:00
import { Settings } from '@material-ui/icons';
2021-01-09 23:23:37 +09:00
import { randomBytes } from 'crypto';
2021-01-10 03:04:30 +09:00
import { existsSync, readFileSync, writeFileSync } from 'fs';
2021-01-10 18:56:28 +09:00
import { Permission } from './permission/permission';
2020-12-31 03:06:16 +09:00
export type Setting = {
2021-01-10 16:53:54 +09:00
/**
* if true, server will bind on '127.0.0.1' rather than '0.0.0.0'
*/
2021-01-09 20:29:01 +09:00
localmode: boolean,
2021-01-10 18:56:28 +09:00
/**
* guest permission
*/
guest: (Permission)[],
2021-01-10 16:53:54 +09:00
/**
* JWT secret key. if you change its value, all access tokens are invalidated.
*/
2021-01-09 23:38:44 +09:00
jwt_secretkey: string,
2021-01-10 16:53:54 +09:00
/**
* the port which running server is binding on.
*/
2021-01-09 23:38:44 +09:00
port:number,
2021-01-10 16:53:54 +09:00
2021-01-10 03:04:30 +09:00
mode:"development"|"production",
2021-01-10 16:53:54 +09:00
/**
* if true, do not show 'electron' window and show terminal only.
*/
2021-01-10 03:04:30 +09:00
cli:boolean,
2021-01-10 16:53:54 +09:00
/** forbid to login admin from remote client. but, it do not invalidate access token.
* if you want to invalidate access token, change 'jwt_secretkey'.*/
forbid_remote_admin_login:boolean,
2021-01-09 20:29:01 +09:00
}
const default_setting:Setting = {
2021-01-10 16:53:54 +09:00
2021-01-09 20:29:01 +09:00
localmode: true,
2021-01-10 18:56:28 +09:00
guest:[],
2021-01-09 23:23:37 +09:00
jwt_secretkey:"itsRandom",
2021-01-09 23:38:44 +09:00
port:8080,
2021-01-10 03:04:30 +09:00
mode:"production",
2021-01-10 16:53:54 +09:00
cli:false,
forbid_remote_admin_login:true,
2020-12-31 03:06:16 +09:00
}
let setting: null|Setting = null;
2021-01-09 23:23:37 +09:00
const setEmptyToDefault = (target:any,default_table:Setting)=>{
2021-01-09 20:29:01 +09:00
let diff_occur = false;
for(const key in default_table){
if(key === undefined || key in target){
continue;
}
2021-01-09 23:23:37 +09:00
target[key] = default_table[key as keyof Setting];
2021-01-09 20:29:01 +09:00
diff_occur = true;
}
return diff_occur;
}
2020-12-31 03:06:16 +09:00
export const read_setting_from_file = ()=>{
2021-01-10 03:04:30 +09:00
let ret = existsSync("settings.json") ? JSON.parse(readFileSync("settings.json",{encoding:"utf8"})) : {};
2021-01-09 23:23:37 +09:00
const partial_occur = setEmptyToDefault(ret,default_setting);
if(partial_occur){
2021-01-09 20:29:01 +09:00
writeFileSync("settings.json",JSON.stringify(ret));
}
2021-01-10 03:04:30 +09:00
return ret as Setting;
2020-12-31 03:06:16 +09:00
}
export function get_setting():Setting{
if(setting === null){
setting = read_setting_from_file();
2021-01-10 03:04:30 +09:00
const env = process.env.NODE_ENV || 'development';
if(env != "production" && env != "development"){
throw new Error("process unknown value in NODE_ENV: must be either \"development\" or \"production\"");
}
setting.mode = env || setting.mode;
2020-12-31 03:06:16 +09:00
}
return setting;
}