Skip to content

Commit 137133c

Browse files
authored
Merge pull request #31 from rogelio-o/develop
Develop
2 parents c6db508 + 9fa2826 commit 137133c

9 files changed

Lines changed: 253 additions & 55 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambda-framework",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Framework to create web apps with any provider. This is the core, you can use it with an official provider implementation or you can implement your own provider.",
55
"main": "dist/src/index.js",
66
"types": "dist/src/index.d.ts",

src/lib/App.ts

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from "fs";
12
import configuration from "./configuration/configuration";
23
import defaultConfiguration from "./configuration/defaultConfiguration";
34
import eventFinalHandler from "./event/eventFinalHandler";
@@ -29,13 +30,17 @@ export default class App implements IApp {
2930
private _settings: object;
3031
private _router: IRouter;
3132

32-
constructor() {
33+
constructor(settings?: object) {
3334
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+
});
3944
}
4045

4146
public enable(key: string): void {
@@ -107,8 +112,61 @@ export default class App implements IApp {
107112
return this;
108113
}
109114

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+
}
112170
}
113171

114172
private logError(req: IHttpRequest|IEventRequest, err: Error): void {

src/lib/Router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class Router implements IRouter {
6060
this._eventStack = [];
6161
this._caseSensitive = options.caseSensitive || false;
6262
this._strict = options.strict || false;
63+
this._subpath = options.subpath;
6364
}
6465

6566
get subrouters(): IRouter[] {

src/lib/configuration/configuration.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*/
44

55
const configuration = {
6-
DEFAULT_MYME_TYPE: "default_myme_type",
7-
ENVIRONMENT: "environment",
8-
TRUST_PROXY: "trus_proxy",
9-
ETAG_FN: "etag_fn",
10-
COOKIE_SECRET: "cookie_secret",
11-
JSON_REPLACER: "json_replacer",
12-
JSON_SPACES: "json_spaces",
13-
JSON_ESCAPE: "json_escape"
6+
DEFAULT_MYME_TYPE: "DEFAULT_MYME_TYPE",
7+
ENVIRONMENT: "ENVIRONMENT",
8+
TRUST_PROXY: "TRUST_PROXY",
9+
ETAG_FN: "ETAG_FN",
10+
COOKIE_SECRET: "COOKIE_SECRET",
11+
JSON_REPLACER: "JSON_REPLACER",
12+
JSON_SPACES: "JSON_SPACES",
13+
JSON_ESCAPE: "JSON_ESCAPE",
14+
PATH_CONTEXT: "PATH_CONTEXT"
1415
};
1516

1617
export default configuration;

src/lib/http/HttpRequest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ export default class HttpRequest implements IHttpRequest {
3636
this.params = mergeParams(event);
3737

3838
this._headers = {};
39-
for (const key of Object.keys(this._event.headers)) {
40-
this._headers[key.toLowerCase()] = this._event.headers[key];
39+
if (this._event.headers) {
40+
for (const key of Object.keys(this._event.headers)) {
41+
this._headers[key.toLowerCase()] = this._event.headers[key];
42+
}
4143
}
4244

4345
this._cookies = getCookiesFromHeader(this._headers.cookie, app.get(configuration.COOKIE_SECRET));

src/lib/types/IApp.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ import IRouter from "./IRouter";
1515
*/
1616
export default interface IApp {
1717

18-
/**
19-
* Initialize the framework with the configuration of `settings`. If
20-
* no `settings`are given, the framework is initialized with the
21-
* default configuration.
22-
*
23-
* @param {object} settings
24-
*/
25-
init(settings?: object): void;
26-
2718
/**
2819
* Set to _true_ the configuration param `key`.
2920
*

0 commit comments

Comments
 (0)