1 |
|
2 | 'use strict';
|
3 |
|
4 | const debug = require('debug')('motif:context');
|
5 |
|
6 | class Context {
|
7 |
|
8 | get port() {
|
9 | return this._port;
|
10 | }
|
11 |
|
12 | get config() {
|
13 | return this._config;
|
14 | }
|
15 |
|
16 | get appPath() {
|
17 | return this._appPath;
|
18 | }
|
19 |
|
20 | get path() {
|
21 | return this._path;
|
22 | }
|
23 |
|
24 | get createDoc() {
|
25 | return this._createDoc;
|
26 | }
|
27 |
|
28 | get useCookie() {
|
29 | return this._useCookie;
|
30 | }
|
31 |
|
32 | get useProxy() {
|
33 | return this._useProxy;
|
34 | }
|
35 |
|
36 | get session() {
|
37 | return this._session;
|
38 | }
|
39 |
|
40 | _normalizePath( val ) {
|
41 |
|
42 | if ( val && val.indexOf("/") === 0 ) {
|
43 | return val.substr(1);
|
44 | }
|
45 |
|
46 | return val;
|
47 | }
|
48 |
|
49 | _normalizePort( val ) {
|
50 | let port = parseInt( val, 10 );
|
51 | if ( isNaN( port ) ) {
|
52 | return val;
|
53 | }
|
54 |
|
55 | if ( port >= 0 ) {
|
56 | return port;
|
57 | }
|
58 |
|
59 | return false;
|
60 | }
|
61 |
|
62 | constructor( options ) {
|
63 | let { appPath, config, path, port, createDoc, useFavicon, useProxy, useCookie, session } = options;
|
64 |
|
65 | debug( "context", options );
|
66 |
|
67 | this._appPath = appPath;
|
68 | this._config = config;
|
69 | this._path = this._normalizePath(path || '');
|
70 | this._port = this._normalizePort(port || 3000);
|
71 | this._createDoc = createDoc === true ? true : false;
|
72 | this._useFavicon = useFavicon === true ? true : false;
|
73 | this._useCookie = useCookie === true ? true : false;
|
74 | this._useProxy = useProxy === true ? true : false;
|
75 | this._session = session || null;
|
76 |
|
77 | this._awsConfig = this._config && this._config.aws || {};
|
78 |
|
79 | if (this._awsConfig.accessKeyId) {
|
80 | process.env.AWS_ACCESS_KEY_ID = this._awsConfig.accessKeyId;
|
81 | }
|
82 | if (this._awsConfig.secretAccessKey) {
|
83 | process.env.AWS_SECRET_ACCESS_KEY = this._awsConfig.secretAccessKey;
|
84 | }
|
85 | }
|
86 | }
|
87 |
|
88 | module.exports = Context;
|