forked from ObjectIsAdvantag/webex-api-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
118 lines (98 loc) · 3.38 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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
// eslint-disable-next-line no-unused-vars
const debug = require("debug")("emulator");
const express = require("express");
const uuid = require('uuid/v4');
//
// Setting up common services
//
const app = express();
// Inject EventBus
const EventEmitter = require('events').EventEmitter;
const bus = new EventEmitter();
// Inject Datastore
const datastore = require("./storage/memory");
// [TODO] replace with new MemoryDatastore(bus)
datastore.bus = bus;
app.locals.datastore = datastore;
// Inject Controller
const Controller = require("./controller");
// This is set to create routes but never "used"
// eslint-disable-next-line no-unused-vars
const controller = new Controller(bus, datastore);
app.set("x-powered-by", false); // to mimic Cisco Spark headers
app.set("etag", false); // to mimic Cisco Spark headers
// Middleware to mimic Cisco Spark HTTP headers
app.use(function (req, res, next) {
res.setHeader("Cache-Control", "no-cache"); // to mimic Cisco Spark headers
res.setHeader("Content-Type", "application/json;charset=UTF-8"); // to mimic Cisco Spark headers
// New Trackingid
res.locals.trackingId = "EM_" + uuid();
res.setHeader("Trackingid", res.locals.trackingId);
next();
});
// Middleware to enforce authentication
const authentication = require("./auth");
app.use(authentication.middleware);
// for parsing application/json in middleware
const bodyParser = require("body-parser");
app.use(bodyParser.json());
// Allow user to set the BOT_UNDER_TEST environmnet var in a .env file
require('dotenv').load();
if (process.env.BOT_UNDER_TEST) {
app.locals.botUnderTestEmail = process.env.BOT_UNDER_TEST;
// Middleware to catch requests with the X-Bot-Responses header
const botTest = require("./bot-test/bot-test");
app.use(botTest.middleware);
// Middleware to delay responses until we get the expected bot response
var botInterceptor = require('./bot-test/bot-interceptor');
app.use(botInterceptor);
}
// Load initial list of accounts
// eslint-disable-next-line no-unused-vars
const accounts = Object.keys(authentication.tokens).map(function (item, index) {
return authentication.tokens[item];
});
datastore.people.init(accounts);
//
// Loading services
//
const peopleAPI = require("./resources/people");
app.use("/people", peopleAPI);
const roomsAPI = require("./resources/rooms");
app.use("/rooms", roomsAPI);
const membershipsAPI = require("./resources/memberships");
app.use("/memberships", membershipsAPI);
const messagesAPI = require("./resources/messages");
app.use("/messages", messagesAPI);
const webhooksAPI = require("./resources/webhooks");
app.use("/webhooks", webhooksAPI);
// Public resources
app.locals.started = new Date(Date.now()).toISOString();
var props = require('./package.json');
app.get("/", function(req, res) {
res.status(200).send({
"service" : "Mini-Spark",
"description" : props.description,
"version" : props.version,
"up-since" : app.locals.started,
"creator" : props.author,
"code": "https://github.com/ObjectIsAdvantag/mini-spark",
"tokens" : "/tokens",
"resources": [
"/people", "/rooms", "/memberships", "/messages", "/webhooks"
]
});
});
app.get("/tokens", function(req, res) {
res.status(200).send(authentication.tokens);
});
//
// Starting server
//
const port = process.env.PORT || 3210;
app.listen(port, function () {
});