forked from jcoppieters/cody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartWebApp.js
93 lines (70 loc) · 2.83 KB
/
startWebApp.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
console.log("loading " + module.id);
var cody = require("./index.js");
var express = require("express");
var vhost = require("vhost");
function startWebApp(mainServer, config, done) {
if (typeof config.name === "undefined") {
console.log("startWebApp - missing name from config options");
return false;
}
console.log("\n======= starting " + config.name + " =======");
config.db = config.db || config.name;
config.version = config.version || "v1.0";
config.controllers = config.controllers || [];
config.datapath = config.datapath || ".";
var app = new cody.Application(config);
for (var iC in config.controllers) {
if (config.controllers.hasOwnProperty(iC)) {
var C = config.controllers[iC];
console.log("Adding controller: " + iC + " - " + C.constructor.name);
app.addController(iC, C);
}
}
app.init(function () {
if ((config.hostnames !== undefined) && (config.hostnames !== "")) {
var siteServer = express();
for (var iL in app.languages) {
// mysite.com/en/page
siteServer.all("/" + app.languages[iL].id + "/*", function (req, res) {
app.servePage(req, res);
});
// mysite.com/nl
siteServer.all("/" + app.languages[iL].id, function (req, res) {
console.log("------------------------------------------------------------------- " + new Date() + "--");
console.log("-- redirecting to " + "/" + app.languages[iL].id + "/");
res.redirect("/" + app.languages[iL].id + "/");
// app.servePage(req, res);
});
}
// no language -> mysite.com
siteServer.all("/", function (req, res) {
console.log("------------------------------------------------------------------- " + new Date() + "--");
console.log("-- redirecting to " + "/" + app.defaultlanguage + "/");
res.redirect("/" + app.defaultlanguage + "/");
//app.servePage(req, res);
});
// mysite.com/static/file-path
siteServer.get("/static/*", function (req, res) {
var fileserver = new cody.Static(req, res, config.name);
fileserver.serve();
});
// mysite.com/data/[category]file-id.extension (standard "files" and "images")
siteServer.get("/data/*", function (req, res) {
var fileserver = new cody.Dynamic(req, res, app.getDataPath());
fileserver.serve();
});
// mysite.com,www.mysite.com,mysite.be,...
var hosts = config.hostnames.split(",");
for (var iH in hosts) {
mainServer.use(vhost(hosts[iH], siteServer));
console.log("======= started " + config.name + " on " + hosts[iH] + " =======\n");
}
} else {
console.log("Could not start app " + config.name + ": no vhost data");
}
if (typeof done === "function") {
done();
}
});
}
module.exports = startWebApp;