forked from compdemocracy/polis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
81 lines (67 loc) · 2.68 KB
/
dev-server.js
File metadata and controls
81 lines (67 loc) · 2.68 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
79
80
81
// Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
var { boolean: isTrue } = require("boolean");
var path = require("path");
var express = require("express");
var fs = require("fs");
var webpack = require("webpack");
var config = require("./webpack.config.dev");
var request = require("request");
var app = express();
var compiler = webpack(config);
let POLIS_ROOT = process.env.POLIS_ROOT
var config = require(POLIS_ROOT + 'config/config.js');
app.use(
require("webpack-dev-middleware")(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
})
);
const serviceUrl = config.get('service_url') ? config.get('service_url') : "https://pol.is";
console.log("SERVICE_URL:", serviceUrl);
function proxy(req, res) {
const hostHeader = serviceUrl.replace(/.*\/\//, "");
const headers = Object.assign(req.headers, {
origin: serviceUrl,
Origin: serviceUrl,
host: hostHeader,
Host: hostHeader,
});
var x = request({
url: serviceUrl + req.path,
qs: req.query,
headers: headers,
rejectUnauthorized: false,
});
req.pipe(x);
x.pipe(res);
x.on("error", function (err) {
console.log("error", err);
res.status(500).send("proxy error");
});
}
// proxy everything else
app.get(/^\/api\//, proxy);
app.put(/^\/api\//, proxy);
app.post(/^\/api\//, proxy);
app.get(/^\/embed\/?$/, function (req, res) {
res.sendFile(path.join(__dirname, "embed.html"));
});
app.get("*", function (req, res) {
var html = fs.readFileSync(path.join(__dirname, "index.html"), { encoding: "utf8" });
html = html.replace("<%= fbAppId %>", config.get('fb_app_id'));
html = html.replace("<%= useIntercom %>", !isTrue(config.get('disable_intercom')));
html = html.replace("<%= usePlans %>", !isTrue(config.get('disable_plans')));
var domainWhitelist = '["' + config.get('domainWhitelist').join('","') + '"]';
html = html.replace("<%= domainWhitelist %>", domainWhitelist);
res.set({
"Content-Type": "text/html",
});
res.status(200).send(html);
});
app.listen(5002, "0.0.0.0", function (err) {
if (err) {
console.log("error", err);
return;
}
console.log("Listening at http://localhost:5002");
});