|
| 1 | +import * as fs from "fs"; |
1 | 2 | import configuration from "./configuration/configuration"; |
2 | 3 | import defaultConfiguration from "./configuration/defaultConfiguration"; |
3 | 4 | import eventFinalHandler from "./event/eventFinalHandler"; |
@@ -29,13 +30,17 @@ export default class App implements IApp { |
29 | 30 | private _settings: object; |
30 | 31 | private _router: IRouter; |
31 | 32 |
|
32 | | - constructor() { |
| 33 | + constructor(settings?: object) { |
33 | 34 | this._settings = {}; |
34 | | - this._router = new Router(); |
35 | | - } |
36 | | - |
37 | | - public init(settings?: object): void { |
38 | | - this.initDefaultConfiguration(settings); |
| 35 | + this.initEnvConfiguration(); |
| 36 | + this.initParamsConfiguration(settings); |
| 37 | + this.initEnvFileConfiguration(); |
| 38 | + this.initDefaultFileConfiguration(); |
| 39 | + this.initDefaultConfiguration(); |
| 40 | + |
| 41 | + this._router = new Router({ |
| 42 | + subpath: this.get(configuration.PATH_CONTEXT) |
| 43 | + }); |
39 | 44 | } |
40 | 45 |
|
41 | 46 | public enable(key: string): void { |
@@ -107,8 +112,61 @@ export default class App implements IApp { |
107 | 112 | return this; |
108 | 113 | } |
109 | 114 |
|
110 | | - private initDefaultConfiguration(settings: object): void { |
111 | | - this._settings = settings ? settings : defaultConfiguration; |
| 115 | + private initEnvConfiguration(): void { |
| 116 | + const settings = {}; |
| 117 | + |
| 118 | + for (const key of Object.keys(process.env)) { |
| 119 | + settings[key] = process.env[key]; |
| 120 | + } |
| 121 | + |
| 122 | + this.initConfiguration(settings); |
| 123 | + } |
| 124 | + |
| 125 | + private initParamsConfiguration(settings: {[name: string]: any}): void { |
| 126 | + this.initConfiguration(settings); |
| 127 | + } |
| 128 | + |
| 129 | + private initEnvFileConfiguration(): void { |
| 130 | + const env = this.get(configuration.ENVIRONMENT) || defaultConfiguration[configuration.ENVIRONMENT]; |
| 131 | + |
| 132 | + this.initFileConfiguration(env); |
| 133 | + } |
| 134 | + |
| 135 | + private initDefaultFileConfiguration(): void { |
| 136 | + this.initFileConfiguration("application"); |
| 137 | + } |
| 138 | + |
| 139 | + private initFileConfiguration(fileName: string): void { |
| 140 | + const path = this.getProjectBasePath() + "/conf/" + fileName + ".json"; |
| 141 | + |
| 142 | + if (fs.existsSync(path)) { |
| 143 | + const rawSetting = fs.readFileSync(path, "utf8"); |
| 144 | + this.initConfiguration(JSON.parse(rawSetting)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private getProjectBasePath(): string { |
| 149 | + console.log(process.env.PWD); |
| 150 | + if (!process.env.PWD) { |
| 151 | + console.log(process.cwd()); |
| 152 | + return process.cwd(); |
| 153 | + } else { |
| 154 | + return process.env.PWD; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private initDefaultConfiguration(): void { |
| 159 | + this.initConfiguration(defaultConfiguration); |
| 160 | + } |
| 161 | + |
| 162 | + private initConfiguration(settings: {[name: string]: any}): void { |
| 163 | + if (settings) { |
| 164 | + for (const key of Object.keys(settings)) { |
| 165 | + if (!this._settings[key]) { |
| 166 | + this._settings[key] = settings[key]; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
112 | 170 | } |
113 | 171 |
|
114 | 172 | private logError(req: IHttpRequest|IEventRequest, err: Error): void { |
|
0 commit comments