This repository was archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (69 loc) · 2.18 KB
/
server.js
File metadata and controls
79 lines (69 loc) · 2.18 KB
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
const config = require('config');
const logger = require('./utils/logger');
const restify = require('restify');
const yeller = require('./utils/yeller');
const generateURLs = require('./utils/generate-urls');
const keygen = require('./utils/keygen');
const uploadUtils = require('./utils/upload');
const server = restify.createServer({
name: 'notificaption'
});
server.use(restify.bodyParser({ mapParams: true }));
server.use(restify.CORS());
/**
* @api POST /screenshot Generate a screenshot
* @apiName PostScreenshot
*
* @apiParam {Object} check - A JSON object detailing the check, associated
* assertions, and so on. Used to populate the screenshot page in Emissary.
*
* @apiSuccess {String} json_url - An URL to the JSON (stored in S3)
* @apiSuccess {Object} image_urls - Multiple sizes of the image, keyed by width
*
* @apiSucessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "json_url": "https://opsee-notificaption-images.s3.amazonaws.com/21G057gL7oNtKKDW64g9Dl_1454339476821.json",
* "image_urls": {
* "default": "https://opsee-notificaption-images.s3.amazonaws.com/21G057gL7oNtKKDW64g9Dl_1454339476821"
* }
* }
*/
server.post('/screenshot', (req, res, next) => {
const check = req.params;
logger.info(check);
const checkID = check.check_id;
if (!checkID) {
return next(new restify.InvalidArgumentError('Must provide check_id'));
}
// Generate a base key for S3 URLs
const key = keygen(checkID);
uploadUtils.uploadJSON(key, check)
.then(result => {
if (!result.url) {
throw new Error('Malformed S3 response: ' + result);
}
res.send({ json_url: result.url });
next();
})
.catch(err => {
logger.error(err);
yeller.report(err);
});
});
/**
* @api GET /health Health check
* @apiDescription Health check endpoint, mostly for AWS.
*/
server.get('/health', (req, res, next) => {
res.send(200, { status: 'ok' });
next();
});
module.exports = {
run() {
server.listen(config.server.port, () => {
logger.info('%s server %s listening at %s', config.util.getEnv('NODE_ENV'), server.name, server.url);
logger.info(config);
});
}
}