Skip to content

Commit

Permalink
Merge 9fa2826 into c6db508
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Feb 8, 2018
2 parents c6db508 + 9fa2826 commit 9c635ba
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-framework",
"version": "1.1.0",
"version": "1.2.0",
"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.",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
74 changes: 66 additions & 8 deletions src/lib/App.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from "fs";
import configuration from "./configuration/configuration";
import defaultConfiguration from "./configuration/defaultConfiguration";
import eventFinalHandler from "./event/eventFinalHandler";
Expand Down Expand Up @@ -29,13 +30,17 @@ export default class App implements IApp {
private _settings: object;
private _router: IRouter;

constructor() {
constructor(settings?: object) {
this._settings = {};
this._router = new Router();
}

public init(settings?: object): void {
this.initDefaultConfiguration(settings);
this.initEnvConfiguration();
this.initParamsConfiguration(settings);
this.initEnvFileConfiguration();
this.initDefaultFileConfiguration();
this.initDefaultConfiguration();

this._router = new Router({
subpath: this.get(configuration.PATH_CONTEXT)
});
}

public enable(key: string): void {
Expand Down Expand Up @@ -107,8 +112,61 @@ export default class App implements IApp {
return this;
}

private initDefaultConfiguration(settings: object): void {
this._settings = settings ? settings : defaultConfiguration;
private initEnvConfiguration(): void {
const settings = {};

for (const key of Object.keys(process.env)) {
settings[key] = process.env[key];
}

this.initConfiguration(settings);
}

private initParamsConfiguration(settings: {[name: string]: any}): void {
this.initConfiguration(settings);
}

private initEnvFileConfiguration(): void {
const env = this.get(configuration.ENVIRONMENT) || defaultConfiguration[configuration.ENVIRONMENT];

this.initFileConfiguration(env);
}

private initDefaultFileConfiguration(): void {
this.initFileConfiguration("application");
}

private initFileConfiguration(fileName: string): void {
const path = this.getProjectBasePath() + "/conf/" + fileName + ".json";

if (fs.existsSync(path)) {
const rawSetting = fs.readFileSync(path, "utf8");
this.initConfiguration(JSON.parse(rawSetting));
}
}

private getProjectBasePath(): string {
console.log(process.env.PWD);
if (!process.env.PWD) {
console.log(process.cwd());
return process.cwd();
} else {
return process.env.PWD;
}
}

private initDefaultConfiguration(): void {
this.initConfiguration(defaultConfiguration);
}

private initConfiguration(settings: {[name: string]: any}): void {
if (settings) {
for (const key of Object.keys(settings)) {
if (!this._settings[key]) {
this._settings[key] = settings[key];
}
}
}
}

private logError(req: IHttpRequest|IEventRequest, err: Error): void {
Expand Down
1 change: 1 addition & 0 deletions src/lib/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class Router implements IRouter {
this._eventStack = [];
this._caseSensitive = options.caseSensitive || false;
this._strict = options.strict || false;
this._subpath = options.subpath;
}

get subrouters(): IRouter[] {
Expand Down
17 changes: 9 additions & 8 deletions src/lib/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/

const configuration = {
DEFAULT_MYME_TYPE: "default_myme_type",
ENVIRONMENT: "environment",
TRUST_PROXY: "trus_proxy",
ETAG_FN: "etag_fn",
COOKIE_SECRET: "cookie_secret",
JSON_REPLACER: "json_replacer",
JSON_SPACES: "json_spaces",
JSON_ESCAPE: "json_escape"
DEFAULT_MYME_TYPE: "DEFAULT_MYME_TYPE",
ENVIRONMENT: "ENVIRONMENT",
TRUST_PROXY: "TRUST_PROXY",
ETAG_FN: "ETAG_FN",
COOKIE_SECRET: "COOKIE_SECRET",
JSON_REPLACER: "JSON_REPLACER",
JSON_SPACES: "JSON_SPACES",
JSON_ESCAPE: "JSON_ESCAPE",
PATH_CONTEXT: "PATH_CONTEXT"
};

export default configuration;
6 changes: 4 additions & 2 deletions src/lib/http/HttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export default class HttpRequest implements IHttpRequest {
this.params = mergeParams(event);

this._headers = {};
for (const key of Object.keys(this._event.headers)) {
this._headers[key.toLowerCase()] = this._event.headers[key];
if (this._event.headers) {
for (const key of Object.keys(this._event.headers)) {
this._headers[key.toLowerCase()] = this._event.headers[key];
}
}

this._cookies = getCookiesFromHeader(this._headers.cookie, app.get(configuration.COOKIE_SECRET));
Expand Down
9 changes: 0 additions & 9 deletions src/lib/types/IApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ import IRouter from "./IRouter";
*/
export default interface IApp {

/**
* Initialize the framework with the configuration of `settings`. If
* no `settings`are given, the framework is initialized with the
* default configuration.
*
* @param {object} settings
*/
init(settings?: object): void;

/**
* Set to _true_ the configuration param `key`.
*
Expand Down
Loading

0 comments on commit 9c635ba

Please sign in to comment.