-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
executable file
·223 lines (179 loc) · 6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env node
var util = require('util');
var path = require('path');
var fs = require('fs');
var express = require('express');
var passport = require('passport');
var Pouch = require('pouchdb');
//var fabric = require('fabric').fabric; // requires 'canvas'
//var child_process = require('child_process');
var zero = require('./zero.js');
var LocalStrategy = require('passport-local').Strategy;
// TODO move to config file
var AllUsers = {
'admin': {
id: 'admin',
group: 'admin'
}
};
passport.use(new LocalStrategy(
function(username, password, done) {
// TODO actual login stuff
if((username == 'admin') && (password == 'admin')) {
return done(null, AllUsers[username]);
} else {
return done(null, false);
}
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
done(null, AllUsers[id]);
});
var app = module.exports = express();
app.configure(function() {
app.use(express.static(path.resolve(__dirname)));
app.use(express.cookieParser('mycelia zero secret'));
app.use(express.session({secret: 'mycelia session secret'}));
app.use(passport.initialize());
app.use(passport.session());
});
app.post('/login', express.bodyParser(), function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if(err) {
res.send({error: 'login_fail', msg: err});
return;
}
if(!user) {
res.send({error: 'login_fail', msg: "Login failed"});
return;
}
req.logIn(user, function(err) {
if(err) {
res.send({error: 'login_fail', msg: err});
return;
}
res.send({msg: "Login succeeded", user: user});
return;
});
})(req, res, next);
}, function(err, req, res, next) {
// called on unexpected errors
res.send({error: "Login failed: " + err});
});
// get a label image
app.get('/label/:filename', function(req, res) {
res.setHeader('Content-Type', 'image/png');
res.sendfile(path.join(zero.config.label_in_dir, req.params.filename));
});
// create a label
app.post('/create', express.bodyParser(), function(req, res) {
var doc = req.body;
var user_id = (req.user) ? req.user.id : 'anonymous';
doc.created_by = user_id;
doc.last_changed_by = user_id;
// create a document
zero.project.create(req.body, function(err, dbresp) {
if(err) {
res.send({
error: 'create_fail',
msg: "Could not create object: " + err
});
return;
}
var id = dbresp.id;
res.send(dbresp);
});
});
app.get('/objects', express.bodyParser(), function(req, res) {
// TODO insecure letting req.query decide view
zero.project.objects(req.query, function(err, rows) {
if(err) {
res.send({error: 'objects_fail', msg: "Could not get objects: " + err});
return;
}
res.send({rows: rows});
})
});
app.get('/view', express.bodyParser(), function(req, res) {
zero.project.object(req.query.id, function(err, doc) {
if(err) {
res.send({error: 'object_fail', msg: "Could not get object '"+req.query.id+"': " + err});
return;
}
res.send(doc);
});
});
app.post('/save_and_print_label', express.bodyParser(), function(req, res) {
var png_header = "data:image/png;base64,";
// base64-encoded png
var png_encoded = req.body.image.substring(png_header.length);
var doc = req.body.doc;
var user_id = (req.user) ? req.user.id : 'anonymous';
doc.created_by = user_id;
doc.last_changed_by = user_id;
// create a document
zero.project.create(doc, function(err, dbresp) {
if(err) {
res.send({
error: 'create_fail',
msg: "Could not create object: " + err
});
return;
}
var id = dbresp.id;
var filename = id+'.png';
zero.save_base64_png(png_encoded, filename, function(err) {
if(err) {
res.json(500, {
error: 'png_save_or_print_fail',
msg: err
});
return;
}
res.json(200, {
msg: 'Label saved as: ' + filename
});
});
});
});
/*
app.post('/save_label_fabric', express.bodyParser(), function(req, res) {
var canvas = fabric.createCanvasForNode(req.body.width, req.body.height);
console.log('> Loading JSON ...');
console.log('image: ' + util.inspect(req.body.image));
canvas.loadFromJSON(req.body.image, function() {
canvas.renderAll();
console.log('> Getting PNG data ... (this can take a while)');
var dataUrl = canvas.toDataURLWithMultiplier('png', req.body.multiplier),
data = dataUrl.replace(/^data:image\/png;base64,/, '');
console.log('> Saving PNG to file ...');
var filePath = __dirname + '/labels/test.png';
fs.writeFile(filePath, data, 'base64', function(err) {
if(err) {
console.log('! Error saving PNG: ' + err);
res.json(200, { error: 'Error saving PNG: ' + err });
} else {
console.log('> PNG file saved to: ' + filePath);
res.json(200, { success: 'PNG file saved to: ' + filePath });
}
});
});
});
*/
app.get('/', function(req, res) {
res.render("index.html");
});
app.get('/fooauth', express.bodyParser(), function(req, res) {
zero.requireAuth(req, res, function() {
res.send({msg: "this is a test"});
// var data = "Coconuts are delicious.";
// res.setHeader('Content-Type', 'text/plain');
// res.setHeader('Content-Length', data.length);
});
});
zero.initialize();
app.listen(3000);
console.log("Mycelia Zero started on port 3000");