File size: 1,582 Bytes
072e993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import YAML from 'yaml'
import fs from 'node:fs'
import chokidar from 'chokidar'

/** 配置文件 */
class Cfg {
  /** 初始化配置 */
  constructor () {
    let file = 'config/config.yaml'
    let fileDef = 'config/default_config.yaml'
    if (!fs.existsSync(file)) {
      fs.copyFileSync(fileDef, file)
    }
    if (!fs.existsSync('data')) fs.mkdirSync('data')
    this.config = {}
  }

  get http () {
    return this.getConfig('config')
  }

  get http_listen () {
    return [this.http.HTTPS ? this.http.HTTPS_PORT : this.http.HTTP_PORT, this.http.HOST]
  }

  https_address (originalUrl, hostname) {
    let Url = new URL(originalUrl, `https://${hostname}`)
    Url.port = this.http.HTTPS_PORT
    return Url.href
  }

  get redis () {
    return this.getConfig('redis')
  }

  get cert() {
    return {
      cert: fs.readFileSync(this.http.CA_CERTIFICATE, 'utf8'),
      key: fs.readFileSync(this.http.CA_PRIVATE, 'utf8')
    }
  }

  /** package.json */
  get package () {
    if (this._package) return this._package

    this._package = JSON.parse(fs.readFileSync('package.json', 'utf8'))
    return this._package
  }

  /** 用户配置 */
  getConfig (name) {
    return this.getYaml(name)
  }

  /**
   * 获取配置yaml
   * @param type 默认跑配置-defSet,用户配置-config
   * @param name 名称
   */
  getYaml (name) {
    let file = `config/${name}.yaml`
    if (this.config[name]) return this.config[name]

    this.config[name] = YAML.parse(
      fs.readFileSync(file, 'utf8')
    )

    return this.config[name]
  }
}

export default new Cfg()