Skip to content

Commit e816b94

Browse files
committed
feat: .env instead of config.js
1 parent 03170aa commit e816b94

File tree

12 files changed

+61
-28
lines changed

12 files changed

+61
-28
lines changed

.env.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
MONGO_USERNAME=''
2+
MONGO_PASSWORD=''
3+
MONGO_CLUSTER=''
4+
MONGO_DATABASE=''
5+
6+
JWT_ACCESS_SECRET='super secret'
7+
JWT_REFRESH_SECRET='super secret'
8+
9+
API_PORT=5000
10+
API_URL='http://localhost:5000'
11+
CLIENT_URL='http://localhost:3000'
12+
APP_NAME='NotesAPP'
13+
14+
MAIL_USER='[email protected]'
15+
MAIL_FROM='[email protected]'
16+
MAIL_PASSWORD=''
17+
MAIL_PORT=
18+
MAIL_HOST=''

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ profile*
5757
*clinic*
5858
*flamegraph*
5959

60-
config.js
60+
.env

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
# NotesAPP API
22

33
## Installation
4+
45
1. Firstly, clone the repository
6+
57
```
68
git clone https://github.com/VadyChel/NotesAppAPI.git
79
```
810

911
...and change directory to cloned
12+
1013
```
1114
cd NotesAppAPI
1215
```
1316

1417
2. Install dependencies
18+
1519
```
1620
npm i
1721
```
1822

1923
...or if you want dev dependencies
24+
2025
```
2126
npm i -D
2227
```
2328

2429
3. Run server
30+
2531
```
2632
npm run start
2733
```
2834

2935
...or in dev mode
36+
3037
```
3138
npm run dev
3239
```
3340

3441
**For test executing**
42+
3543
```
3644
npm run test
3745
```
3846

3947
## Application settings
40-
1. Create file `config.js` and move to them `config.template.js` content
41-
2. Put your data into `config.js` variables
48+
49+
1. Create file `.env` and move to them `.env.template` content
50+
2. Put your data into `.env` variables

app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import fastifyFormBody from '@fastify/formbody'
55
import fastifyCORS from '@fastify/cors'
66
import ajvCompiler from '@fastify/ajv-compiler'
77
import Fastify from 'fastify'
8-
import { API_PORT } from './config.js'
98
import fastifyCookie from '@fastify/cookie'
9+
import { config } from 'dotenv'
10+
11+
config()
1012

1113
const __filename = fileURLToPath(import.meta.url)
1214
const __dirname = path.dirname(__filename)
@@ -41,7 +43,7 @@ app.register(fastifyCORS)
4143
app.register(fastifyFormBody)
4244

4345
try {
44-
await app.listen(API_PORT)
46+
await app.listen(process.env.API_PORT)
4547
} catch(e) {
4648
console.log(e)
4749
process.exit()

config.template.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

controllers/users.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import UsersService from '../services/users.js'
2-
import { CLIENT_URL } from '../config.js'
32

43
class UsersController {
54
async getCurrentUser(req, rep) {
@@ -10,7 +9,7 @@ class UsersController {
109
try {
1110
await UsersService.activateUser(req.params.code)
1211
} finally {
13-
rep.redirect(CLIENT_URL)
12+
rep.redirect(process.env.CLIENT_URL)
1413
}
1514
}
1615
}

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@fastify/cors": "^7.0.0",
3131
"@fastify/formbody": "^6.0.0",
3232
"bcrypt": "^5.0.1",
33+
"dotenv": "^16.0.1",
3334
"fastify": "^3.0.0",
3435
"fastify-autoload": "^3.10.0",
3536
"fastify-plugin": "^3.0.0",

plugins/mongoConnector.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import fastifyPlugin from 'fastify-plugin'
2-
import { MONGO_CLUSTER, MONGO_DATABASE, MONGO_PASSWORD, MONGO_USERNAME } from '../config.js'
32
import mongoose from 'mongoose'
43

54
export default fastifyPlugin(async (fastify, options) => {
65
mongoose.connection.on('connect', () => {
7-
fastify.log.info(`MongoDB connected with user ${MONGO_USERNAME} on ${MONGO_DATABASE} database`)
6+
fastify.log.info(`MongoDB connected with user ${process.env.MONGO_USERNAME} on ${process.env.MONGO_DATABASE} database`)
87
})
98
mongoose.connection.on('disconnect', () => {
109
fastify.log.info('MongoDB disconnected')
1110
})
1211
try {
13-
await mongoose.connect(`mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_CLUSTER}/${MONGO_DATABASE}?retryWrites=true&w=majority`)
12+
await mongoose.connect(`mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}/${process.env.MONGO_DATABASE}?retryWrites=true&w=majority`)
1413
} catch(e) {
1514
fastify.log.error(`Mongo error: ${e}`)
1615
}

services/auth.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import bcrypt from 'bcrypt'
33
import MailService from '../services/mails.js'
44
import UsersService from '../services/users.js'
55
import TokensService from '../services/tokens.js'
6-
import { API_URL } from '../config.js'
76
import { v4 as uuid4 } from 'uuid'
87
import ActivationMails from '../database/models/activationMails.js'
98

@@ -35,7 +34,7 @@ class AuthService {
3534
const passwordHash = await bcrypt.hash(password, 8)
3635
const activationCode = uuid4()
3736
const userId = uuid4()
38-
const activationLink = `${API_URL}/api/users/activate/${activationCode}`
37+
const activationLink = `${process.env.API_URL}/api/users/activate/${activationCode}`
3938

4039
await ActivationMails.create({ userId, activationCode })
4140
await Auth.create({ passwordHash, email, userId, username })

0 commit comments

Comments
 (0)