Skip to content

Commit

Permalink
Fixed bug when .env file was not set.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Nov 8, 2018
1 parent 5774d2c commit 8fa2eae
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 80 deletions.
24 changes: 19 additions & 5 deletions example/activity-tracking/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import http from 'http'
import https from 'https'
import dotenv from 'dotenv'
import { Application } from 'express'
import { Identifier } from './src/di/identifiers'
import { DI } from './src/di/di'
Expand All @@ -9,12 +8,27 @@ import { BackgroundService } from './src/background/background.service'
import { Default } from './src/utils/default'
import { App } from './src/app'

// Define values in .env in environment variables
dotenv.config()
/**
* Used only in development or test to load environment variables.
* NOTE:
* For the development and testing environment, create the .env file
* in the root directory of your project and add your environment variables
* to new lines in the format NAME=VALUE. For example:
* DB_HOST=localhost
* DB_USER=root
* DB_PASS=mypass
* The fastest way is to create a copy of the .env.example file.
*
* NOTE: For the production environment it is highly recommended not to use .env.
* Manually enter the environment variables that are required for your application.
*/
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'production') {
require(`dotenv`).config()
}

const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
const app: Application = (DI.getInstance().getContainer().get<App>(Identifier.APP)).getExpress()
const backgroundServices: BackgroundService = DI.getInstance().getContainer().get(Identifier.BACKGROUND_SERVICE)
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
const port_http = process.env.PORT_HTTP || Default.PORT_HTTP
const port_https = process.env.PORT_HTTPS || Default.PORT_HTTPS

Expand All @@ -34,7 +48,7 @@ if ((process.env.NODE_ENV || Default.NODE_ENV) === 'production') {
// set to https://acme-staging-v02.api.letsencrypt.org/directory in dev
server: 'https://acme-v02.api.letsencrypt.org/directory',
version: 'draft-12',
email: 'douglasrafaelcg@gmail.com',
email: 'myemail@mydomain.com',
// When set to true, this always accepts the LetsEncrypt TOS. When a string it checks the agreement url first.
agreeTos: true,
// Can be either of:
Expand Down
2 changes: 1 addition & 1 deletion example/activity-tracking/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class App {
))

// Middleware swagger. It should not run in the test environment.
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'test') {
const options = {
customCss: '.swagger-ui .topbar { display: none }',
customfavIcon: 'http://nutes.uepb.edu.br/wp-content/uploads/2014/01/icon.fw_.png',
Expand Down
2 changes: 1 addition & 1 deletion example/activity-tracking/src/utils/custom.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('winston-daily-rotate-file')
@injectable()
export class CustomLogger implements ILogger {
private readonly _logger: Logger
private readonly _env: string = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
private readonly _env: string = (process.env.NODE_ENV || Default.NODE_ENV) === 'development' ? 'debug' : 'info'
private readonly _logDir = process.env.LOG_DIR || Default.LOG_DIR

constructor() {
Expand Down
131 changes: 66 additions & 65 deletions example/activity-tracking/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["dom", "es2017"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
"removeComments": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
// "types": ["reflect-metadata"], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
},
"include": [
"./**/*.ts"
],
"exclude": [
"dist",
"node_modules",
"**/*.spec.ts"
]
}
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["dom", "es2017"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
"removeComments": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
// "types": ["reflect-metadata"], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
},
"include": [
"./**/*.ts"
],
"exclude": [
"dist",
"node_modules",
"**/*.spec.ts",
"exemple"
]
}
24 changes: 19 additions & 5 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import http from 'http'
import https from 'https'
import dotenv from 'dotenv'
import { Application } from 'express'
import { Identifier } from './src/di/identifiers'
import { DI } from './src/di/di'
Expand All @@ -9,12 +8,27 @@ import { BackgroundService } from './src/background/background.service'
import { Default } from './src/utils/default'
import { App } from './src/app'

// Define values in .env in environment variables. Development and testing environment only.
dotenv.config()
/**
* Used only in development or test to load environment variables.
* NOTE:
* For the development and testing environment, create the .env file
* in the root directory of your project and add your environment variables
* to new lines in the format NAME=VALUE. For example:
* DB_HOST=localhost
* DB_USER=root
* DB_PASS=mypass
* The fastest way is to create a copy of the .env.example file.
*
* NOTE: For the production environment it is highly recommended not to use .env.
* Manually enter the environment variables that are required for your application.
*/
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'production') {
require(`dotenv`).config()
}

const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
const app: Application = (DI.getInstance().getContainer().get<App>(Identifier.APP)).getExpress()
const backgroundServices: BackgroundService = DI.getInstance().getContainer().get(Identifier.BACKGROUND_SERVICE)
const logger: ILogger = DI.getInstance().getContainer().get<ILogger>(Identifier.LOGGER)
const port_http = process.env.PORT_HTTP || Default.PORT_HTTP
const port_https = process.env.PORT_HTTPS || Default.PORT_HTTPS

Expand All @@ -34,7 +48,7 @@ if ((process.env.NODE_ENV || Default.NODE_ENV) === 'production') {
// set to https://acme-staging-v02.api.letsencrypt.org/directory in dev
server: 'https://acme-v02.api.letsencrypt.org/directory',
version: 'draft-12',
email: 'douglasrafaelcg@gmail.com',
email: 'myemail@mydomain.com',
// When set to true, this always accepts the LetsEncrypt TOS. When a string it checks the agreement url first.
agreeTos: true,
// Can be either of:
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class App {
))

// Middleware swagger. It should not run in the test environment.
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'test') {
const options = {
customCss: '.swagger-ui .topbar { display: none }',
customfavIcon: 'http://nutes.uepb.edu.br/wp-content/uploads/2014/01/icon.fw_.png',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/custom.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('winston-daily-rotate-file')
@injectable()
export class CustomLogger implements ILogger {
private readonly _logger: Logger
private readonly _env: string = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
private readonly _env: string = (process.env.NODE_ENV || Default.NODE_ENV) === 'development' ? 'debug' : 'info'
private readonly _logDir = process.env.LOG_DIR || Default.LOG_DIR

constructor() {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"exclude": [
"dist",
"node_modules",
"**/*.spec.ts"
"**/*.spec.ts",
"exemple"
]
}

0 comments on commit 8fa2eae

Please sign in to comment.