-
Notifications
You must be signed in to change notification settings - Fork 23
/
app.js
113 lines (95 loc) · 3.98 KB
/
app.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
var app = require('./lib/server');
var server = require('http').createServer(app);
var io = require('socket.io').listen(server,{ 'destroy buffer size': Infinity });
io.set("log level", 1);
var index_socket = require('./lib/index_socket');
index_socket.setup(io);
var program = require('commander');
program
.version('0.0.3')
.option('-p, --port <n>', 'port no. default is 3000.')
.option('-d, --db_name [name]', 'db name. default is "devhub_db".')
.option('-t, --title_name [name]', 'title name. default is "".')
.option('--force-ssl', 'enforce access via https. default is false.')
.addHelpText('after', `Environment variables
BASIC_AUTH_USER: user name of basic authentication.
BASIC_AUTH_PASS: password of basic authentication.
GRIDFS: Set "true" when useing gridfs.`)
.parse(process.argv);
var menu_links = [];
try{
// メニューにリンクを表示したい場合は以下のファイルに json で記載
// [{"name": "Google", "url": "https://www.google.co.jp/"}]
menu_links = require('./lib/menu_links.json');
}catch(e){
// ファイルが無ければメニューリンクなし
}
const options = program.opts();
app.set('port', options.port || process.env.PORT || 3000);
app.set('db_name', options.db_name || 'devhub_db');
app.set('title_name', options.title_name ? "for " + options.title_name : "");
app.set('force_ssl', options.forceSsl || process.env.FORCE_SSL === 'true');
app.set('basic_user', process.env.BASIC_AUTH_USER ? process.env.BASIC_AUTH_USER : "");
app.set('basic_pass', process.env.BASIC_AUTH_PASS ? process.env.BASIC_AUTH_PASS : "");
app.set('gridfs', process.env.GRIDFS == "true" ? true : false);
app.set('menu_links', menu_links);
console.log(' port : ' + app.get('port'));
console.log(' db_name : ' + app.get('db_name'));
console.log(' title_name : ' + app.get('title_name'));
console.log(' BASIC_AUTH_USER : ' + app.get('basic_user'));
console.log(' BASIC_AUTH_PASS : ' + app.get('basic_pass'));
console.log(' GRIDFS: ' + app.get('gridfs'));
if (app.get('force_ssl')) {
app.get('*', function(req, res, next) {
if (req.secure || req.headers['x-forwarded-proto'] === 'https') {
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
}
var client_info = require('./lib/client_info');
client_info.options({
title: app.get('title_name')
});
// set routing
var routes = {
index: require('./routes/index'),
notify: require('./routes/notify'),
memo: require('./routes/memo'),
upload : app.get('gridfs') ? require('./routes/upload_db') : require('./routes/upload'),
blog: require('./routes/blog'),
calendar: require('./routes/calendar'),
};
app.get('/', function(req,res){ routes.index.get(req,res,app); });
app.get('/notify', function(req, res) { routes.notify.get(req,res,io); });
app.get('/memo', function(req, res) { routes.memo.insert(req,res,io); });
app.get('/memo/body', routes.memo.get);
app.post('/upload', routes.upload.post);
app.get('/upload', routes.upload.get);
app.delete('/upload', routes.upload.delete);
app.get('/uploads/:file', routes.upload.serve);
app.get('/blog', routes.blog.get);
app.get('/blog/body', routes.blog.body);
app.get('/blog/body_search', routes.blog.body_search);
app.get('/blog/body_older', routes.blog.body_older);
app.post('/blog', function(req,res){ routes.blog.post(req,res,io); });
app.delete('/blog', routes.blog.delete);
app.get('/blog/reset_tags', routes.blog.reset_tags);
app.get('/calendar', routes.calendar.get);
// set db and listen app
var chat_log = require('./lib/chat_log');
var text_log = require('./lib/text_log');
var blog = require('./lib/blog');
var tag = require('./lib/tag');
var mongo_builder = require('./lib/mongo_builder');
mongo_builder.ready(app.get('db_name'), function(db){
chat_log.set_db(db);
text_log.set_db(db);
blog.set_db(db);
tag.set_db(db);
routes.upload.set_db(db);
server.listen(app.get('port'));
console.log("listen!!!");
});
console.log('Server running at ' + (app.get('force_ssl') ? 'https' : 'http') + '://127.0.0.1:' + app.get('port') + '/');