diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..656f5f1f --- /dev/null +++ b/.env.example @@ -0,0 +1,33 @@ +APP_URL=http://localhost:3333 +NODE_ENV=development + +# Auth + +APP_SECRET=bootcampgobarbernode + +# Database + +DB_HOST= +DB_USER= +DB_PASS= +DB_NAME= + +# Mongo + +MONGO_URL= + +# Redis + +REDIS_HOST=127.0.0.1 +REDIS_POST=6379 + +# Mail + +MAIL_HOST= +MAIL_PORT= +MAIL_USER= +MAIL_PASS= + +# Sentry + +SENTRY_DSN= diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..37d7e734 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.env diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity index 353964c1..dccbfab1 100644 --- a/node_modules/.yarn-integrity +++ b/node_modules/.yarn-integrity @@ -10,6 +10,7 @@ "bcryptjs@^2.4.3", "bee-queue@^1.2.2", "date-fns@^2.0.0-beta.3", + "dotenv@^8.0.0", "eslint-config-airbnb-base@^13.2.0", "eslint-config-prettier@^6.0.0", "eslint-plugin-import@^2.18.2", @@ -176,6 +177,7 @@ "doctrine@1.5.0": "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa", "doctrine@^3.0.0": "https://registry.yarnpkg.com/doctrine/-/doctrine-3.0.0.tgz#addebead72a6574db783639dc87a121773973961", "dot-prop@^4.1.0": "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.2.0.tgz#1f19e0c2e1aa0e32797c49799f2837ac6af69c57", + "dotenv@^8.0.0": "https://registry.yarnpkg.com/dotenv/-/dotenv-8.0.0.tgz#ed310c165b4e8a97bb745b0a9d99c31bda566440", "dottie@^2.0.0": "https://registry.yarnpkg.com/dottie/-/dottie-2.0.1.tgz#697ad9d72004db7574d21f892466a3c285893659", "double-ended-queue@^2.1.0-0": "https://registry.yarnpkg.com/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c", "duplexer3@^0.1.4": "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2", diff --git a/package.json b/package.json index cd4912fc..256b55e8 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "bcryptjs": "^2.4.3", "bee-queue": "^1.2.2", "date-fns": "^2.0.0-beta.3", + "dotenv": "^8.0.0", "express": "^4.17.1", "express-async-errors": "^3.1.1", "express-handlebars": "^3.1.0", diff --git a/src/app.js b/src/app.js index 3c3d9dc5..f3fcf374 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,4 @@ +import 'dotenv/config'; import express from 'express'; import path from 'path'; import Youch from 'youch'; @@ -34,9 +35,13 @@ class App { exceptionHandler() { this.server.use(async (err, req, res, next) => { - const errors = await new Youch(err, req).toJSON(); + if (process.env.NODE_ENV === 'development') { + const errors = await new Youch(err, req).toJSON(); - return res.status(500).json(errors); + return res.status(500).json(errors); + } + + return res.status(500).json({ error: 'Internal server error' }); }); } } diff --git a/src/app/models/File.js b/src/app/models/File.js index 2d898b2a..1bcc2855 100644 --- a/src/app/models/File.js +++ b/src/app/models/File.js @@ -9,7 +9,7 @@ class File extends Model { url: { type: Sequelize.VIRTUAL, get() { - return `http://localhost:3333/files/${this.path}`; + return `${process.env.APP_URL}/files/${this.path}`; }, }, }, diff --git a/src/config/auth.js b/src/config/auth.js index 080db4a1..1da951c4 100644 --- a/src/config/auth.js +++ b/src/config/auth.js @@ -1,4 +1,4 @@ export default { - secret: '519b5358c603267e6a1702a68a3b749b', + secret: process.env.APP_SECRET, expiresIn: '7d', }; diff --git a/src/config/database.js b/src/config/database.js index 14821447..3ae3abd8 100644 --- a/src/config/database.js +++ b/src/config/database.js @@ -1,9 +1,11 @@ +require('dotenv/config'); + module.exports = { dialect: 'postgres', - host: 'localhost', - username: 'postgres', - password: 'docker', - database: 'gobarber', + host: process.env.DB_HOST, + username: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, port: 5434, define: { timestamps: true, diff --git a/src/config/mail.js b/src/config/mail.js index 02ddfdbc..eeb1dfd3 100644 --- a/src/config/mail.js +++ b/src/config/mail.js @@ -1,10 +1,10 @@ export default { - host: 'smtp.mailtrap.io', - port: 2525, + host: process.env.MAIL_HOST, + port: process.env.MAIL_PORT, secure: false, auth: { - user: 'bf68c0c53c2c39', - pass: '05233a5d03725b', + user: process.env.MAIL_USER, + pass: process.env.MAIL_PASS, }, default: { from: 'Equipe GoBarber ', diff --git a/src/config/redis.js b/src/config/redis.js index ca98e3ae..e4670b44 100644 --- a/src/config/redis.js +++ b/src/config/redis.js @@ -1,4 +1,4 @@ export default { - host: '127.0.0.1', - port: 6379, + host: process.env.REDIS_HOST, + port: process.env.REDIS_PORT, }; diff --git a/src/config/sentry.js b/src/config/sentry.js index 0a072d25..c2850c66 100644 --- a/src/config/sentry.js +++ b/src/config/sentry.js @@ -1,3 +1,3 @@ export default { - dsn: 'https://6ca2425d1cfe494095b49ae1bc3941fc@sentry.io/1514707', + dsn: process.env.SENTRY_DSN, }; diff --git a/src/database/index.js b/src/database/index.js index c3ebbf59..4c445dab 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -22,10 +22,10 @@ class Database { } mongo() { - this.mongoConnection = mongoose.connect( - 'mongodb://localhost:27017/gobarber', - { useNewUrlParser: true, useFindAndModify: true } - ); + this.mongoConnection = mongoose.connect(process.env.MONGO_URL, { + useNewUrlParser: true, + useFindAndModify: true, + }); } } diff --git a/src/queue.js b/src/queue.js index 08824e59..490c101e 100644 --- a/src/queue.js +++ b/src/queue.js @@ -1,3 +1,4 @@ +import 'dotenv/config'; import Queue from './lib/Queue'; Queue.processQueue(); diff --git a/yarn.lock b/yarn.lock index 213ca228..95033c4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -836,6 +836,11 @@ dot-prop@^4.1.0: dependencies: is-obj "^1.0.0" +dotenv@^8.0.0: + version "8.0.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.0.0.tgz#ed310c165b4e8a97bb745b0a9d99c31bda566440" + integrity sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg== + dottie@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/dottie/-/dottie-2.0.1.tgz#697ad9d72004db7574d21f892466a3c285893659"