-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
149 lines (136 loc) · 4.94 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
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
var express = require("express"),
mysql = require("db-mysql"),
generic_pool = require("generic-pool"),
uuid = require("node-uuid");
require("express-resource");
/* MySQL pool */
var config = require("./config.json");
var pool = generic_pool.Pool({
name: 'mysql',
max: 10,
create: function(callback) {
new mysql.Database({
hostname: config.database.host,
user: config.database.user,
password: config.database.password,
database: config.database.database
}).connect(function(err,server) {
callback(err, this);
});
},
destroy: function(db) {
db.disconnect();
}
});
var app = express();
app.configure(function() {
app.use(express.bodyParser());
});
var info = require("./package.json");
app.get('/', function(req, res) {
res.send(info.name + " - " + info.version);
});
var User = {
create: function(req, res) {
if (req.body.user && req.body.pass) {
if (!req.body.email) req.body.email = null;
pool.acquire(function(err, db) {
if (err) {
res.statusCode = 500;
console.log(err);
return res.end("Internal Server Error: Database error\n");
}
db.query().select("*").from("users").where("user = ?", [req.body.user]).execute(function(err, rows, columns) {
if (err) {
pool.release(db);
console.log(err);
res.statusCode = 500;
return res.end("Internal Server Error: Database error\n");
}
if (rows.length) {
pool.release(db);
res.statusCode = 409;
return res.end("Conflict: Username already exists.\n");
}
console.log(db.query().insert("users",
["user", "pass", "email"], [req.body.user, req.body.pass, req.body.email]).sql());
db.query().insert("users",
["user", "pass", "email"], [req.body.user, req.body.pass, req.body.email]).execute(function(err, result) {
pool.release(db);
if (err) {
res.statusCode = 500;
console.log(err);
return res.end("Internal Server Error: Database error\n");
}
res.statusCode = 201;
return res.end("/users/" + req.body.user + "\n");
});
});
});
} else {
res.statusCode = 400;
return res.end("Bad Request: provide user and pass in POST request.\n");
}
},
update: function(req, res) {
res.statusCode = 501;
res.end("Not yet implemented.\n");
},
delete: function(req, res) {
res.statusCode = 501;
res.end("Not yet implemented.\n");
}
};
var tokens = Object.create(null);
var Token = {
create: function(req, res) {
// TODO Real validation
if (req.body.user && req.body.pass) {
pool.acquire(function(err, db) {
if (err) {
res.statusCode = 500;
return res.end("Internal Server Error: Database error.\n");
}
db.query().select("*").from("users").where("user = ? and pass = ?", [req.body.user, req.body.pass]).execute(function(error, rows, column) {
pool.release(db);
if (error) {
res.statusCode = 500;
return res.end("Internal Server Error: Database error.\n");
}
if (rows.length == 0) {
res.statusCode = 403;
return res.end("Forbidden: Please supply valid credentials when requesting token.\n");
}
var token = uuid.v4();
tokens[token] = {expires: Date.now() + 1000 * 60 * 30, user: req.body.user};
res.statusCode = 201;
return res.end("/tokens/" + token + "\n");
});
});
} else {
res.statusCode = 400;
return res.end("Bad Request: provide existing user and pass in POST request\n");
}
},
load: function(req, id, next) {
next(null, tokens[id]);
}
};
var Team = {
index: function(req, res) {
console.log("Team index");
console.log("Token: " + req.token);
res.end("\n");
},
create: function(req, res) {
},
show: function(req, res) {
},
update: function(req, res) {
},
destroy: function(req, res) {
}
};
app.resource('users', User);
app.resource('tokens', Token, {load: Token.load}).add(app.resource('teams', Team));
app.listen(3000)