File size: 1,957 Bytes
813eca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import path from 'path';

import fs from 'fs-extra';
import yaml from 'yaml';
import _ from 'lodash';

import environment from '../environment.ts';
import util from '../util.ts';

const CONFIG_PATH = path.join(path.resolve(), 'configs/', environment.env, "/service.yml");

/**
 * 服务配置
 */
export class ServiceConfig {

    /** 服务名称 */
    name: string;
    /** @type {string} 服务绑定主机地址 */
    host;
    /** @type {number} 服务绑定端口 */
    port;
    /** @type {string} 服务路由前缀 */
    urlPrefix;
    /** @type {string} 服务绑定地址(外部访问地址) */
    bindAddress;

    constructor(options?: any) {
        const { name, host, port, urlPrefix, bindAddress } = options || {};
        this.name = _.defaultTo(name, 'deepseek-free-api');
        this.host = _.defaultTo(host, '0.0.0.0');
        this.port = _.defaultTo(port, 5566);
        this.urlPrefix = _.defaultTo(urlPrefix, '');
        this.bindAddress = bindAddress;
    }

    get addressHost() {
        if(this.bindAddress) return this.bindAddress;
        const ipAddresses = util.getIPAddressesByIPv4();
        for(let ipAddress of ipAddresses) {
            if(ipAddress === this.host)
                return ipAddress;
        }
        return ipAddresses[0] || "127.0.0.1";
    }

    get address() {
        return `${this.addressHost}:${this.port}`;
    }

    get pageDirUrl() {
        return `http://127.0.0.1:${this.port}/page`;
    }

    get publicDirUrl() {
        return `http://127.0.0.1:${this.port}/public`;
    }

    static load() {
        const external = _.pickBy(environment, (v, k) => ["name", "host", "port"].includes(k) && !_.isUndefined(v));
        if(!fs.pathExistsSync(CONFIG_PATH)) return new ServiceConfig(external);
        const data = yaml.parse(fs.readFileSync(CONFIG_PATH).toString());
        return new ServiceConfig({ ...data, ...external });
    }

}

export default ServiceConfig.load();