forked from wafflestudio/snutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_snutt.js
107 lines (101 loc) · 3.88 KB
/
run_snutt.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
var url = require('url');
var path = require('path');
var fs = require('fs');
var deparam = require('node-jquery-deparam');
var Router = require('routes');
var mkdirp = require('mkdirp');
var _ = require("underscore");
var Cookies = require("cookies");
// config
var config = require('./config.js');
// make directories if not exist
mkdirp.sync(config.snutt.USER_IMAGE_PATH);
mkdirp.sync(config.snutt.USER_TIMETABLE_PATH);
mkdirp.sync(config.snutt.USER_ICS_PATH);
// load model
var NaiveLectureModel = require('./model/naive_snutt_data.js').NaiveLectureModel;
var lectureModel = new NaiveLectureModel();
lectureModel.init();
var cookies;
// load controllers
var controllers = require('./controllers/controllers.js').functor(config, "snutt", lectureModel);
// define application
var port = process.env.PORT || 3784;
var app = require('http').createServer(handler);
app.listen(port);
console.log("Listening on " + port);
// register router
var router = Router();
// api
router.addRoute("/api/init_client", controllers.api_controller.initClient);
router.addRoute("/api/search_query?", controllers.api_controller.searchQuery);
router.addRoute("/api/export_timetable?", controllers.api_controller.exportTimetable);
router.addRoute("/api/publish_to_facebook", controllers.api_controller.publishToFacebook)
// view
router.addRoute("/", controllers.home_controller.home);
router.addRoute("/member", controllers.home_controller.member);
router.addRoute("/user/:id", controllers.home_controller.show);
router.addRoute("/calendar/export", controllers.home_controller.export_cal);
// asset
router.addRoute("/asset/:name.:format", controllers.home_controller.asset);
router.addRoute("/asset/:path2/:name.:format", controllers.home_controller.asset);
router.addRoute("/user/asset/:name.:format", controllers.home_controller.asset);
router.addRoute("/user/asset/:path2/:name.:format", controllers.home_controller.asset);
// data for the mobile app - this is just a temporal fix
router.addRoute("/data/snutt/:name", controllers.home_controller.app_data);
router.addRoute("/api/:name", controllers.home_controller.json);
//http server handler
function handler (req, res) {
req.url = req.url.replace("//","/"); // app is requesting /api//sugang.json...
var uri = url.parse(req.url).pathname;
var params = deparam(url.parse(req.url).query);
cookies = new Cookies(req,res);
var renderer = {
json: function(hash, nostringfy) {
res.writeHead(200, {"Content-Type": "application/json"});
if (nostringfy) res.end(hash);
else res.end(JSON.stringify(hash)); //app doesn't work when JSON is stringfied...
},
zip: function(data, size) {
res.writeHead(200,
{'Content-Type' : "application/zip",
'Content-Length' : size }
);
res.end(data);
},
// TODO : deprecated, use generic instead
text: function(data, mime) {
if (mime) res.writeHead(200, {'Content-Type' : mime});
else res.writeHead(200, {'Content-Type' : "text/html"});
res.end(data);
},
generic: function(data, mime) {
if (mime) res.writeHead(200, {'Content-Type' : mime});
else res.writeHead(200, {'Content-Type' : "text/html"});
res.end(data);
},
image: function (img, extension) {
res.writeHead(200, {'Content-Type' : "image/"+extension});
res.end(img);
},
cal: function (ics) {
res.writeHead(200, {
'Content-Type' : "application/octet-stream",
"Content-Disposition": "attachment; filename=snutt-calendar.ics"
});
res.end(ics);
},
cookies: cookies,
err: function () {
res.writeHead(404, {'Content-Type' : "text/html"});
res.end("<h1>SNUTT</h1><br/><h5>페이지를 찾을 수 없습니다! 주소를 확인해주세요.</h5>");
}
};
// router
var route = router.match(uri);
if (route) {
_.extend(params, route.params);
route.fn.apply(null, [params, renderer, req]);
}
else renderer.err();
}