-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
99 lines (77 loc) · 2.12 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
const fs = require("fs")
const express = require("express");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config.js");
const webpackMiddleware = require("webpack-dev-middleware");
const locales = ["en", "de"];
const angular = [
"user",
"messages",
"circles",
"main",
"friends",
"settings",
"start",
"notificationCenter",
"setup",
"invite",
"backup",
"post",
"fund",
"search",
]
const staticPath = "static/postprocess"
const ownIndexFile = fs.readdirSync(`${staticPath}/de`)
const log = (line) => {
console.log(line)
}
const getPossibleLocale = (acceptLanguageHeader) => {
const languages = acceptLanguageHeader.split(",")
.map((lang) => lang.split(";")[0].split("-")[0])
.filter((lang) => locales.indexOf(lang) !== -1)
return languages[0] || locales[0];
}
const app = express();
const router = express.Router();
var WHISPEER_PORT = process.env.WHISPEER_PORT || 8080;
process.argv.forEach(function(val, index, array) {
// let's make nils happy
if (val === "--port") {
WHISPEER_PORT = array[index + 1];
} else {
// or just.. don't
val = val.split("=");
if (val[0] === "--port") {
WHISPEER_PORT = val[1];
}
}
});
log("Starting webserver...");
const mid = webpackMiddleware(webpack(webpackConfig), {
publicPath: "/assets",
})
router.use("/", express.static("static"))
router.use("/assets", express.static("assets"))
router.all("*", function (req, res, next) {
const paths = req.originalUrl.split(/\/|\?/).filter((path) => path !== "")
const redirectLocale = getPossibleLocale(req.get("accept-language"));
const possibleLocale = paths[0];
const hasLocale = locales.indexOf(possibleLocale) !== -1;
if (!hasLocale) {
return res.redirect(`/${redirectLocale}${req.originalUrl}`)
}
paths.shift()
if (ownIndexFile.indexOf(paths[0]) > -1) {
req.url = `/assets/../${possibleLocale}/${paths[0]}/index.html`
return mid(req, res, next)
}
if (angular.indexOf(paths[0]) > -1) {
req.url = "/assets/../index.html"
return mid(req, res, next)
}
next();
})
app.use(mid)
app.use(router)
app.listen(WHISPEER_PORT);
log("Whispeer web server started on port " + WHISPEER_PORT);