Skip to content

Commit 8fa2eae

Browse files
committed
Fixed bug when .env file was not set.
1 parent 5774d2c commit 8fa2eae

File tree

8 files changed

+110
-80
lines changed

8 files changed

+110
-80
lines changed

example/activity-tracking/server.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import http from 'http'
22
import https from 'https'
3-
import dotenv from 'dotenv'
43
import { Application } from 'express'
54
import { Identifier } from './src/di/identifiers'
65
import { DI } from './src/di/di'
@@ -9,12 +8,27 @@ import { BackgroundService } from './src/background/background.service'
98
import { Default } from './src/utils/default'
109
import { App } from './src/app'
1110

12-
// Define values in .env in environment variables
13-
dotenv.config()
11+
/**
12+
* Used only in development or test to load environment variables.
13+
* NOTE:
14+
* For the development and testing environment, create the .env file
15+
* in the root directory of your project and add your environment variables
16+
* to new lines in the format NAME=VALUE. For example:
17+
* DB_HOST=localhost
18+
* DB_USER=root
19+
* DB_PASS=mypass
20+
* The fastest way is to create a copy of the .env.example file.
21+
*
22+
* NOTE: For the production environment it is highly recommended not to use .env.
23+
* Manually enter the environment variables that are required for your application.
24+
*/
25+
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'production') {
26+
require(`dotenv`).config()
27+
}
1428

29+
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
1530
const app: Application = (DI.getInstance().getContainer().get<App>(Identifier.APP)).getExpress()
1631
const backgroundServices: BackgroundService = DI.getInstance().getContainer().get(Identifier.BACKGROUND_SERVICE)
17-
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
1832
const port_http = process.env.PORT_HTTP || Default.PORT_HTTP
1933
const port_https = process.env.PORT_HTTPS || Default.PORT_HTTPS
2034

@@ -34,7 +48,7 @@ if ((process.env.NODE_ENV || Default.NODE_ENV) === 'production') {
3448
// set to https://acme-staging-v02.api.letsencrypt.org/directory in dev
3549
server: 'https://acme-v02.api.letsencrypt.org/directory',
3650
version: 'draft-12',
37-
email: 'douglasrafaelcg@gmail.com',
51+
email: 'myemail@mydomain.com',
3852
// When set to true, this always accepts the LetsEncrypt TOS. When a string it checks the agreement url first.
3953
agreeTos: true,
4054
// Can be either of:

example/activity-tracking/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class App {
8585
))
8686

8787
// Middleware swagger. It should not run in the test environment.
88-
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
88+
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'test') {
8989
const options = {
9090
customCss: '.swagger-ui .topbar { display: none }',
9191
customfavIcon: 'http://nutes.uepb.edu.br/wp-content/uploads/2014/01/icon.fw_.png',

example/activity-tracking/src/utils/custom.logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require('winston-daily-rotate-file')
99
@injectable()
1010
export class CustomLogger implements ILogger {
1111
private readonly _logger: Logger
12-
private readonly _env: string = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
12+
private readonly _env: string = (process.env.NODE_ENV || Default.NODE_ENV) === 'development' ? 'debug' : 'info'
1313
private readonly _logDir = process.env.LOG_DIR || Default.LOG_DIR
1414

1515
constructor() {
Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
11
{
2-
"compilerOptions": {
3-
/* Basic Options */
4-
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5-
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6-
"lib": ["dom", "es2017"], /* Specify library files to be included in the compilation. */
7-
"allowJs": true, /* Allow javascript files to be compiled. */
8-
// "checkJs": true, /* Report errors in .js files. */
9-
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10-
"declaration": false, /* Generates corresponding '.d.ts' file. */
11-
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
12-
"sourceMap": true, /* Generates corresponding '.map' file. */
13-
// "outFile": "./", /* Concatenate and emit output to single file. */
14-
"outDir": "./dist", /* Redirect output structure to the directory. */
15-
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16-
// "composite": true, /* Enable project compilation */
17-
"removeComments": true, /* Do not emit outputs. */
18-
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
19-
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
20-
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
21-
22-
/* Strict Type-Checking Options */
23-
"strict": true, /* Enable all strict type-checking options. */
24-
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
25-
// "strictNullChecks": true, /* Enable strict null checks. */
26-
"strictFunctionTypes": true, /* Enable strict checking of function types. */
27-
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
28-
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
29-
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
30-
31-
/* Additional Checks */
32-
"noUnusedLocals": true, /* Report errors on unused locals. */
33-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
34-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
35-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
36-
37-
/* Module Resolution Options */
38-
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
39-
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
40-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
41-
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
42-
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
43-
// "types": ["reflect-metadata"], /* Type declaration files to be included in compilation. */
44-
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
45-
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
46-
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
47-
48-
/* Source Map Options */
49-
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
50-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
51-
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
52-
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
53-
54-
/* Experimental Options */
55-
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
56-
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
57-
},
58-
"include": [
59-
"./**/*.ts"
60-
],
61-
"exclude": [
62-
"dist",
63-
"node_modules",
64-
"**/*.spec.ts"
65-
]
66-
}
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
"lib": ["dom", "es2017"], /* Specify library files to be included in the compilation. */
7+
"allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10+
"declaration": false, /* Generates corresponding '.d.ts' file. */
11+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
12+
"sourceMap": true, /* Generates corresponding '.map' file. */
13+
// "outFile": "./", /* Concatenate and emit output to single file. */
14+
"outDir": "./dist", /* Redirect output structure to the directory. */
15+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16+
// "composite": true, /* Enable project compilation */
17+
"removeComments": true, /* Do not emit outputs. */
18+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
19+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
20+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
21+
22+
/* Strict Type-Checking Options */
23+
"strict": true, /* Enable all strict type-checking options. */
24+
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
25+
// "strictNullChecks": true, /* Enable strict null checks. */
26+
"strictFunctionTypes": true, /* Enable strict checking of function types. */
27+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
28+
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
29+
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
30+
31+
/* Additional Checks */
32+
"noUnusedLocals": true, /* Report errors on unused locals. */
33+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
34+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
35+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
36+
37+
/* Module Resolution Options */
38+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
39+
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
40+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
41+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
42+
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
43+
// "types": ["reflect-metadata"], /* Type declaration files to be included in compilation. */
44+
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
45+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
46+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
47+
48+
/* Source Map Options */
49+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
50+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
51+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
52+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
53+
54+
/* Experimental Options */
55+
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
56+
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
57+
},
58+
"include": [
59+
"./**/*.ts"
60+
],
61+
"exclude": [
62+
"dist",
63+
"node_modules",
64+
"**/*.spec.ts",
65+
"exemple"
66+
]
67+
}

server.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import http from 'http'
22
import https from 'https'
3-
import dotenv from 'dotenv'
43
import { Application } from 'express'
54
import { Identifier } from './src/di/identifiers'
65
import { DI } from './src/di/di'
@@ -9,12 +8,27 @@ import { BackgroundService } from './src/background/background.service'
98
import { Default } from './src/utils/default'
109
import { App } from './src/app'
1110

12-
// Define values in .env in environment variables. Development and testing environment only.
13-
dotenv.config()
11+
/**
12+
* Used only in development or test to load environment variables.
13+
* NOTE:
14+
* For the development and testing environment, create the .env file
15+
* in the root directory of your project and add your environment variables
16+
* to new lines in the format NAME=VALUE. For example:
17+
* DB_HOST=localhost
18+
* DB_USER=root
19+
* DB_PASS=mypass
20+
* The fastest way is to create a copy of the .env.example file.
21+
*
22+
* NOTE: For the production environment it is highly recommended not to use .env.
23+
* Manually enter the environment variables that are required for your application.
24+
*/
25+
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'production') {
26+
require(`dotenv`).config()
27+
}
1428

29+
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
1530
const app: Application = (DI.getInstance().getContainer().get<App>(Identifier.APP)).getExpress()
1631
const backgroundServices: BackgroundService = DI.getInstance().getContainer().get(Identifier.BACKGROUND_SERVICE)
17-
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
1832
const port_http = process.env.PORT_HTTP || Default.PORT_HTTP
1933
const port_https = process.env.PORT_HTTPS || Default.PORT_HTTPS
2034

@@ -34,7 +48,7 @@ if ((process.env.NODE_ENV || Default.NODE_ENV) === 'production') {
3448
// set to https://acme-staging-v02.api.letsencrypt.org/directory in dev
3549
server: 'https://acme-v02.api.letsencrypt.org/directory',
3650
version: 'draft-12',
37-
email: 'douglasrafaelcg@gmail.com',
51+
email: 'myemail@mydomain.com',
3852
// When set to true, this always accepts the LetsEncrypt TOS. When a string it checks the agreement url first.
3953
agreeTos: true,
4054
// Can be either of:

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class App {
8585
))
8686

8787
// Middleware swagger. It should not run in the test environment.
88-
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
88+
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'test') {
8989
const options = {
9090
customCss: '.swagger-ui .topbar { display: none }',
9191
customfavIcon: 'http://nutes.uepb.edu.br/wp-content/uploads/2014/01/icon.fw_.png',

src/utils/custom.logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require('winston-daily-rotate-file')
99
@injectable()
1010
export class CustomLogger implements ILogger {
1111
private readonly _logger: Logger
12-
private readonly _env: string = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
12+
private readonly _env: string = (process.env.NODE_ENV || Default.NODE_ENV) === 'development' ? 'debug' : 'info'
1313
private readonly _logDir = process.env.LOG_DIR || Default.LOG_DIR
1414

1515
constructor() {

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"exclude": [
6262
"dist",
6363
"node_modules",
64-
"**/*.spec.ts"
64+
"**/*.spec.ts",
65+
"exemple"
6566
]
6667
}

0 commit comments

Comments
 (0)