forked from larskarbo/napchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
192 lines (158 loc) · 4.44 KB
/
script.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
187
188
189
190
191
192
// Dependencies
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var logger = require('./logger.js');
var nconf = require('nconf');
var environment = process.env.NODE_ENV || 'development';
nconf.argv()
.file({ file: 'config.json' });
if(nconf.get('setup')){
setup();
}else if(nconf.get('create-tables')){
createTables();
}else if(nconf.get('exportJson')){
exportJson();
}else if(nconf.get('importJson')){
importJson();
}else if(!nconf.get('mysql')){
logger.info('No mysql credentials found');
logger.info('To set up a server, run node script --setup');
logger.info('running napchart without database capabilities');
start(false);
}else{
start(true);
}
function setup() {
var install = require('./install.js');
install.setup(function(){
process.exit();
});
}
function createTables() {
var install = require('./install.js');
install.createTables(function(){
process.exit();
})
}
function exportJson() {
var database = require('./database.js');
database.exportJson(function(horse,error){
if(error){
logger.error(error);
}else{
logger.verbose('finished')
process.exit();
}
})
}
function importJson() {
var database = require('./database.js');
var file = nconf.get('file') | 'export.json';
database.importJson(file, function(horse,error){
if(error){
logger.error(error);
}else{
logger.verbose('finished')
process.exit();
}
})
}
function start(db){
app.use(express.static('public'));
app.use(favicon(__dirname + '/public/img/favicon.ico')); //serve favicon
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('view engine', 'ejs');
//Routes:
//index
app.get('/', function (req, res) {
var host = req.headers.host;
res.render('pages/main',{chartid:null,chart:null, url:host, db:db});
});
if(db){
//chart
app.get('/:chartid', function (req, res) {
var chartid = req.params.chartid;
var host = req.headers.host;
var database = require('./database.js');
database.getChart(chartid, function(chartData,error){
if(error){
if(error == 404){
res.redirect('/');
//res.render('pages/main',{chartid:null,chart:null, url:host, db:db});
}else{
logger.error("There was a problem when creating a new chart:", error);
res.writeHead(503);
res.end("error");
}
}else{
res.render('pages/main',{chartid:chartid,chart:JSON.stringify(chartData), url:host, db:db});
}
});
});
//get schedule data (used for ajax requests ( not currently in use))
// app.get('/get/:chartid', function(req, res) {
// var chartid = req.params.chartid;
// getObject(chartid,function(object){
// res.writeHead(200, {"Content-Type": "application/json"});
// res.end(JSON.stringify(output));
// });
// });
//save schedule
app.post('/post', function (req, res) {
var database = require('./database.js');
var data = JSON.parse(req.body.data);
database.newChart(req,data, function(chartid,error){
if(error){
logger.error("There was a problem when creating a new chart:", error);
res.writeHead(503);
res.end("error");
}else{
res.writeHead(200);
res.end(chartid);
}
});
});
app.post('/post/feedback', function (req,res){
var text = req.body.message;
var database = require('./database.js');
database.postFeedback(text, function(result, error){
if(error){
logger.error("There was a problem when posting feedback:", error);
res.writeHead(503);
res.end("error");
}else{
res.writeHead(200);
res.end(JSON.stringify(result,null,2));
}
})
});
app.post('/post/linkEmailToFeedback', function (req,res){
var token = req.body.token;
var email = req.body.email;
var database = require('./database.js');
console.log("email:", email);
database.linkEmailToFeedback(token, email, function(result, error){
if(error){
logger.error("There was a problem when posting feedback:", error);
res.writeHead(503);
res.end("error");
}else{
res.writeHead(200);
res.end(JSON.stringify(result));
}
})
});
}
app.get('*', function (req, res) {
res.redirect('/');
});
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 3000
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0'
var server = app.listen(server_port,server_ip_address);
logger.info('Napchart started at %s:%s', server_ip_address, server_port)
}