-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (56 loc) · 1.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
'use strict';
require('dotenv').load();
var path = require('path');
var Joi = require('joi');
var Boom = require('boom');
var Promise = require('bluebird');
var generateHtml = require(path.join(__dirname, 'generate-html'));
var createDoc = Promise.method(require('apidoc').createDoc);
const Hapi = require('hapi');
const server = new Hapi.Server();
var epochtalkPath = process.env.EPOCHTALK_PATH;
var apiKey = process.env.API_KEY;
var port = process.env.PORT || 3000;
var host = process.env.HOST || 'localhost';
if (!epochtalkPath) {
console.error('No EPOCHTALK_PATH in ENV!');
process.exit(1);
}
else {
epochtalkPath = path.resolve(epochtalkPath);
}
if (!apiKey) {
console.error('No API_KEY in ENV!');
process.exit(1);
}
server.connection({ port, host });
server.route({
method: 'POST',
path: '/generate',
config: {
validate: {
payload: Joi.object().keys({
api_key: Joi.string().required()
})
},
handler: function (request, reply) {
if (request.payload.api_key === apiKey) {
var opts = { src: epochtalkPath, dest: 'doc', excludeFilters: ['node_modules', 'public'] };
return createDoc(opts)
.then(function() {
return generateHtml.generate()
.then(function(results) { reply(results); })
.catch(function(e){ reply(Boom.badImplementation(e.message)); });
})
.catch(function() {
reply(Boom.badImplementation('Error: api doc generation failed'));
});
}
else { reply(Boom.unauthorized('Invalid API Key.')); }
}
}
});
server.start((err) => {
if (err) { throw err; }
console.log(`Server running at: ${server.info.uri}`);
});