-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
104 lines (98 loc) · 2.58 KB
/
app.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
const express = require('express')
const graphqlHttp = require('express-graphql')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const Show = require('./models/show')
const helmet = require('helmet')
const dotenv = require('dotenv')
const chalk = require('chalk')
const winston = require('winston')
const expressWinston = require('express-winston')
const graphQlSchema = require('./graphql/schema/index')
const graphQlResolvers = require('./graphql/resolvers/index')
console.log(chalk.yellow('Starting Frisky Server...'))
/**
* Express App
*/
const app = express()
let port = process.env.PORT || 3000
/**
* Environment Variables
*/
dotenv.config()
/**
* Middlewares
*/
console.log(chalk.blue('Initializing Middlewares...'))
app.use(helmet())
app.set('x-powered-by', 'Frisky Server')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(expressWinston.logger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'logs/'+new Date().toISOString().substring(0, 10)+'.log' })
],
format: winston.format.combine(
winston.format.json()
)
}))
/**
* MongoDB Connection
*/
console.log(chalk.blue('Initializing Database Connection...'))
let options = {
autoIndex: false,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
useNewUrlParser: true,
}
let dbUri = process.env.NODE_ENV === 'production' ? process.env.MONGO_URI : process.env.MONGO_URI_DEV
mongoose
.connect(dbUri, options)
.then(() => {
/**
* GraphQL Server
*/
app.use(
'/graphql',
graphqlHttp({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true
})
).listen(port, () => {
console.log(chalk.yellow(`✔︎ Frisky GraphQL Server started on port ${port}`))
})
})
.catch((err) => {
console.log(err)
console.log(chalk.red('Shutting down Frisky Server'))
})
/**
* Database Connection Events
*/
const connection = mongoose.connection
connection.on('connected', () => {
console.log(chalk.green(`✔︎ Connected to Database: ${dbUri}`))
/**
* Sync Indexes for Search
*/
Show.syncIndexes()
.then(() => {
console.log(chalk.green('✔︎ Indexes in place for Search'))
})
.catch((err) => {
console.log(err)
console.log(chalk.red('✘ Problem with indexes'))
})
})
connection.on('error', (err) => {
console.log(chalk.red(`✘ Database Error: ${err}`))
})
connection.on('disconnected', () => {
console.log(chalk.red('✘ Disconnected from Database'))
})
connection.on('reconnected', () => {
console.log(chalk.green(`✔︎ Reconnected to Database: ${dbUri}`))
})