-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (141 loc) · 3.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
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
'use strict';
var Hapi = require('hapi'),
Joi = require('joi'),
app = new Hapi.Server(),
port = (process.env.PORT || 3000),
pack = require('./package'),
mongoose = require('mongoose'),
chartersRoutes = require('./lib/routers/charters'),
swaggerOptions = {
apiVersion: pack.version,
documentationPath: '/documentation'
},
dbUrl =
'MONGOLAB_URL OR YOUR OWN DB URL',
dbOptions = {
db: {
'native_parser': true
},
server: {
poolSize: 5
}
};
app.connection({
port: port
});
app.route([{
method: 'GET',
path: '/',
config: {
handler: function(req, res) {
res('Welcome').redirect('/charters');
}
},
}, {
method: 'GET',
path: '/charters',
config: {
handler: chartersRoutes.index,
description: 'Show all registered charters',
tags: ['api', 'charters']
}
}, {
method: 'GET',
path: '/charters/{id}',
config: {
handler: chartersRoutes.getCharter,
validate: {
params: {
id: Joi.string()
.required()
.description('This is the ID to identify the charter.')
}
},
tags: ['api', 'charters'],
description: 'Get one user\'s charter by his/her ID'
}
}, {
method: 'POST',
path: '/charters',
config: {
handler: chartersRoutes.registerCharter,
validate: {
payload: {
charter: Joi.string()
.regex(/^(\d{3})-(\d{7})-(\d{1})$/i)
.required()
.description('The charter to be saved.')
}
},
tags: ['api', 'charters'],
description: 'Register a charter to the docs collection'
}
}, {
method: 'PUT',
path: '/charters/{charterId}/update',
config: {
handler: chartersRoutes.updateCharter,
validate: {
payload: {
newCharter: Joi.string()
.regex(/^(\d{3})-(\d{7})-(\d{1})$/i)
.required()
.description('New charter\'s value.')
},
params: {
charterId: Joi.string()
.required()
.description('The charter\'s ID that is going to be update'),
}
},
tags: ['api', 'charters'],
description: 'Updates a charter based on the ID given.'
}
}, {
method: 'DELETE',
path: '/charters/{id}/delete',
config: {
handler: chartersRoutes.deleteCharter,
validate: {
params: {
id: Joi.string()
.required()
.description(
'The id to search to delete a charter with this id')
}
},
tags: ['api', 'charters'],
description: 'Deletes a charter from the collection'
}
}]);
app.register([{
//Register Swagger plugin for hapi.js api's documentation
register: require('hapi-swagger'),
options: swaggerOptions
}],
function(err) {
if (err) {
app.log(['error'], 'plugins failed to load: ' + err);
return;
}
app.log(['error'], 'plugins loaded');
mongoose.connect(dbUrl, dbOptions, function(err) {
if (err) {
app.log('error', err);
}
app.start(function() {
console.log('App running on port', port);
});
});
});
module.exports = {
app: function() {
return app;
},
mongoose: function() {
return mongoose;
},
db: function() {
return mongoose.connection;
}
};