This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
119 lines (102 loc) · 2.65 KB
/
server.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
// Create a basic Hapi.js server
require('babel-register')({
presets: ['es2015', 'react']
})
const Hapi = require('hapi')
const AuthCookie = require('hapi-auth-cookie')
const Hoek = require('hoek')
const HapiShelf = require('hapi-shelf')
const dateFormat = require('dateformat')
const format = 'dd mmm HH:MM:ss'
const routes = require('./api/routes')
const auth = require('./api/auth/Authenticate.js')
const newuser = require('./api/auth/CreateUser.js')
const port = process.env['PORT'] || '8000'
// Basic Hapi.js connection stuff
const server = new Hapi.Server({ debug: { request: ['error'] }})
server.connection({
host: '0.0.0.0',
port: port
})
const host = process.env['OKC_DB_HOST'] || '127.0.0.1'
const database = process.env['OKC_DB_NAME']
const user = process.env['OKC_DB_USER']
const password = process.env['OKC_DB_PASSWORD']
module.exports = server
server.register(
{
register: HapiShelf,
options: {
knex: {
client: 'pg',
connection: {
host,
user,
password,
database
}
},
plugins: ['registry'],
models: [
'./api/models/Category',
'./api/models/DataType',
'./api/models/Survey',
'./api/models/Question',
'./api/models/Answer',
'./api/models/SurveyResponse',
'./api/models/SurveyAnswer',
'./api/models/Geography',
'./api/models/Candidate',
'./api/models/CandidateAnswer',
'./api/models/CandidateGeography',
'./api/models/CandidateType',
'./api/models/Users'
]
}
},
function (err) {
if (err) {
throw err
}
}
)
// Register the inert and vision Hapi plugins
// As of Hapi 9.x, these two plugins are no longer
// included in Hapi automatically
// https://github.com/hapijs/hapi/issues/2682
server.register([{
register: require('inert')
}, {
register: require('vision')
}, {
register: auth
},{
register: newuser
}], function(err) {
if (err) return console.error(err)
// Add the React-rendering view engine
server.views({
engines: {
jsx: require('hapi-react-views')
},
relativeTo: __dirname,
path: 'views'
})
// Add a route to serve static assets (CSS, JS, IMG)
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'assets',
index: ['index.html']
}
}
})
// Should this say 'api' instead of app?
// Add main app route
server.route(routes(server))
server.start(function() {
console.log(dateFormat(new Date(), format) + ' - Server started at: ' + server.info.uri)
})
});