forked from SirSeim/SPFY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
153 lines (134 loc) · 3.89 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var Hapi = require('hapi');
var Path = require('path');
var Inert = require('inert');
var Vision = require('vision');
var PostgreSQL = require('pg');
var url = require('url');
var setup = {
host: process.env.NODE_ENV === "production" ? "0.0.0.0" : "localhost",
port: process.env.PORT || "8080"
};
var Api = require(Path.join(__dirname, 'routes/api_routes.js'));
var viewRoutes = require(Path.join(__dirname, 'routes/view_routes.js'));
var postgresqlPool = {
register: function (server, options, next) {
var params = url.parse(process.env.DATABASE_URL);
var auth = params.auth.split(':');
var dbconfig = {
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: process.env.NODE_ENV === "production",
max: 20,
min: 4
};
var pool = new PostgreSQL.Pool(dbconfig);
server.decorate('server', 'postgres', pool);
server.decorate('request', 'postgres', pool);
pool.on('error', function (err, client) {
if ("" + err) {
server.log(['warning', 'PostgreSQL'], "" + err);
server.log(['warning', 'PostgreSQL'], client);
}
});
next();
}
};
postgresqlPool.register.attributes = {
name: "PostgreSQL",
version: "0.0.0"
};
var validate = function (decoded, request, callback) {
console.log(decoded);
// console.log(request);
// TODO: Look into what is in decoded & request
// query the database for the user
return callback(null, true);
};
var SPFY = new Hapi.Server({
connections: {
routes: {
files: {
relativeTo: Path.join(__dirname, 'static')
}
}
}
});
SPFY.connection({
host: setup.host,
port: setup.port
});
SPFY.register(require('hapi-auth-jwt2'), function (err) {
if (err) {
SPFY.log(['error', 'hapi-auth-jwt2'], err);
}
SPFY.auth.strategy('jwt', 'jwt', {
key: process.env.SPFY_KEY, // Never Share your secret key
validateFunc: validate, // validate function defined above
verifyOptions: { algorithms: [ 'HS256' ] } // pick a strong algorithm
});
SPFY.auth.default('jwt');
// EXAMPLE ROUTES
// SPFY.route([
// {
// method: "GET", path: "/", config: { auth: false },
// handler: function(request, reply) {
// reply({text: 'Token not required'});
// }
// },
// {
// method: 'GET', path: '/restricted', config: { auth: 'jwt' },
// handler: function(request, reply) {
// reply({text: 'You used a Token!'})
// .header("Authorization", request.headers.authorization);
// }
// }
// ]);
});
SPFY.register(postgresqlPool, function () {});
SPFY.register(Api, {
routes: {
prefix: '/api'
}
});
SPFY.register(Inert, function () {});
SPFY.register(Vision, function () {
SPFY.views({
engines: {
html: require('nunjucks-hapi')
},
path: Path.join(__dirname, 'templates')
});
SPFY.route(viewRoutes);
});
SPFY.register({
register: require('good'),
options: {
ops: {
interval: 1000
},
reporters: {
console: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{
log: '*',
response: '*'
}]
}, {
module: 'good-console'
}, 'stdout']
}
}
}, function (err) {
if (err) {
SPFY.log(['error', 'good'], err);
}
});
SPFY.start(function () {
SPFY.log(['info', 'SPFY'], "Server started on " + setup.host + ":" + setup.port);
SPFY.log(['info', 'SPFY'], process.env.DATABASE_URL);
});
module.exports = SPFY;