-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (145 loc) · 4.66 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var express = require("express");
var cors = require("cors");
var lessMiddleware = require("less-middleware");
var path = require("path");
var ejs = require("ejs");
var fs = require("fs");
var http = require("http");
var sprintf = require("./src/lib/sprintf").s;
var DataSync = require("./src/DataSync");
var RouteUtils = require("./src/utils/RouteUtils");
var JavascriptUtils = require("./src/utils/JavascriptUtils");
var DataController = require("./src/controllers/DataController");
var GenerationController = require("./src/controllers/GenerationController");
var UserController = require("./src/controllers/UserController");
var PreviewController = require("./src/controllers/PreviewController");
var pathify = function (aDir) { return path.join(__dirname, aDir); };
// Absolute file system paths
var assetPath = pathify("public");
var viewPath = pathify("views");
var app = express();
var server = http.createServer(app);
var fbAppId = "1390085397942073";
var clientJs = JavascriptUtils.createJsManager(assetPath);
// Javascript includes for each page
var jsIncludeList = {
"index": [
"lib/jquery-2.1.0.min.js",
"lib/underscore.min.js",
"lib/backbone.min.js",
"lib/sprintf.min.js",
"lib/jquery.throttle-debounce.min.js",
"lib/jquery.print-preview.js",
"js/common/Dropdown.js",
"js/common/TimeUtils.js",
"js/common/CalendarUtils.js",
"js/scheduler.js",
"js/models/RestModels.js",
"js/models/ThemeModels.js",
"js/models/ClassData.js",
"js/models/UserData.js",
"js/models/ThemeData.js",
"js/models/CalendarSettings.js",
"js/views/Sidebar.js",
"js/views/SidebarGroup.js",
"js/views/AddSectionSidebar.js",
"js/views/CustomizeSidebar.js",
"js/views/PrintSidebar.js",
"js/views/SearchResultEntry.js",
"js/views/TermDropdownEntry.js",
"js/views/SearchResultList.js",
"js/views/GroupedSectionDropdownEntry.js",
"js/views/GroupedSectionDropdownList.js",
"js/views/CalendarEntry.js",
"js/views/CalendarEntryGroup.js",
"js/views/Calendar.js",
"js/views/Palette.js",
"js/entrypoint.js"
]
};
// Prevents conflicts with underscore.js templates since both use <% ... %>
ejs.open = "{{";
ejs.close = "}}";
app.engine(".html", ejs.__express);
app.set("view engine", "ejs");
app.set("views", viewPath);
// Add the Javascript includes for each page, server will minify and concat for release
for (var key in jsIncludeList) {
jsIncludeList[key].forEach(function (aScript) {
clientJs.addFile(key, aScript);
});
}
var commonConfigure = function () {
// Put other global configurations here:
app.use(express.bodyParser());
app.use(express.static(assetPath));
app.use(cors());
};
app.configure("development", function () {
console.log("Server starting in development mode...");
app.use(lessMiddleware("../less", {
"dest": "css",
"pathRoot": assetPath,
"force": true,
"compress": false,
"debug": true
}));
commonConfigure();
DataSync.rebuildSearchIndex();
});
app.configure("production", function () {
console.log("Server starting in production mode...");
clientJs.concatJs();
app.use(lessMiddleware("../less", {
"dest": "css",
"pathRoot": assetPath,
"once": true,
"compress": true
}));
// Log errors on exceptions and exit
process.on("uncaughtException", function (err) {
console.error("UncaughtException: ", err.message);
console.error(err.stack);
process.exit(1);
});
commonConfigure();
DataSync.startPeriodicDataSync();
});
/**
* HTML routes
*/
app.get("/", function (aReq, aRes) {
aRes.render("index.ejs", {
"hash": undefined,
"appId": fbAppId,
"js": clientJs.getJsPaths("index")
});
});
app.get("/:hash", function (aReq, aRes) {
aRes.render("index.ejs", {
"hash": aReq.params.hash,
"appId": fbAppId,
"js": clientJs.getJsPaths("index")
});
});
app.get("/loading/img", function (aReq, aRes) {
aRes.render("loading.ejs", { "msg": "Generating schedule preview..." });
});
app.get("/loading/pdf", function (aReq, aRes) {
aRes.render("loading.ejs", { "msg": "Generating schedule PDF..." });
});
app.get("/preview/:hash", PreviewController.htmlPreview);
app.get("/preview/:hash/img", PreviewController.imagePreview);
/**
* Rest API routes
*/
app.get("/api/class", DataController.getClasses);
app.get("/api/:term/class", DataController.getClassesByTerm);
app.get("/api/term", DataController.getTerms);
app.post("/api/user/schedule", UserController.insertUserSchedule);
app.put("/api/user/schedule/:hash", UserController.updateUserSchedule);
app.get("/api/user/schedule/:hash", UserController.getUserSchedule);
app.post("/api/pdfify/:hash", GenerationController.genPdf);
app.post("/api/imgify/:hash", GenerationController.genImg);
app.listen(RouteUtils.port);
console.log("Starting server on port " + RouteUtils.port);