perf: 自动生成jwtkey,无需手动配置

This commit is contained in:
xiaojunnuo
2024-07-15 01:29:19 +08:00
parent 485e603b51
commit 390e4853a5
8 changed files with 65 additions and 30 deletions
@@ -0,0 +1,24 @@
import fs from 'fs';
import yaml from 'js-yaml';
import * as _ from 'lodash-es';
import { nanoid } from 'nanoid';
const KEYS_FILE = './data/keys.yaml';
export class Keys {
jwtKey: string = nanoid();
cookieKeys: string[] = [nanoid()];
static load(): Keys {
const keys = new Keys();
if (fs.existsSync(KEYS_FILE)) {
const content = fs.readFileSync(KEYS_FILE, 'utf8');
const json = yaml.load(content);
_.merge(keys, json);
}
keys.save();
return keys;
}
save() {
fs.writeFileSync(KEYS_FILE, yaml.dump(this));
}
}