-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (100 loc) · 3.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// % Import Important Modules
const express = require('express')
const dotenv = require('dotenv')
const {
failedMessage,
syncFailedMessage,
attemptingToConnect,
attemptingToSync,
noSyncMessage,
listeningMessage,
} = require('./db_message')
const jwt = require('jsonwebtoken')
const path = require('path')
const cors = require('cors')
const bodyParser = require('body-parser')
const { userLogger } = require('./helpers/logger')
// % Reference Models
const db = require('./models')
// % Initialize Express
var app = express()
// % Express Shenanigans
// ? parse requests of content-type application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }))
// ? parse requrests of content-type application/json
app.use(express.json())
// % .env config
dotenv.config()
// % PORT value
const PORT = process.env.PORT || 3700
// % Middleware
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(function (req, res, next) {
const { method, socket, url, hostname } = req
const { remoteAddress, remoteFamily } = socket
// ? you can check session here.
const log = {
method,
remoteAddress,
remoteFamily,
hostname,
url,
}
userLogger.info(JSON.stringify(log, null, 2))
next()
})
/**
* ================================================================
* * ROUTES
* ================================================================
*/
const MAIN_API_ROUTE = '/multi_db/v1/'
// % Home Route
// >> localhost:3700/multi_db/v1/
/**
* ================================================================
* * DATABASE
* ================================================================
*/
const databases = Object.keys(require(__dirname + '/config/config.json')['development'])
const db_log = process.env.ENABLE_DB_LOG === 'true' || false
const sync_log = process.env.ENABLE_SYNC_MODEL_LOG === 'true' || false
const connectToDB = async db => {
db_log ? userLogger.info(attemptingToConnect()) : {}
databases.forEach(database => {
db.sequelize[database].authenticate().catch(err => {
console.error(failedMessage(err))
})
// Print connection logs
if (db_log) console.log(`✅ ${database} database is connected!`)
})
}
const syncToDB = async db => {
if (process.env.SEQUELIZE_ALTER_SYNC === 'true') {
sync_log ? userLogger.info(attemptingToSync()) : {}
databases.forEach(database => {
db.sequelize[database]
.sync({ alter: true })
.catch(err => console.error(syncFailedMessage(err)))
console.log(`✅ ${database} models have been synced to the database!`)
})
} else {
userLogger.info(noSyncMessage())
}
}
const connectAndSync = async () => {
// Connect to all databases
connectToDB(db)
syncToDB(db)
app.listen(PORT, () => {
userLogger.info(listeningMessage(PORT, MAIN_API_ROUTE))
})
}
/**
* ================================================================
* * APP LISTENING...
* ================================================================
*/
// Start the connection and syncing process
connectAndSync()