-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
131 lines (108 loc) · 4.57 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
var express = require('express');
var path = require('path');
var passport = require('passport');
var bodyParser = require('body-parser');
var cors = require('cors');
var config = require('./libs/config');
var log = require('./libs/log')(module);
var oauth2 = require('./libs/oauth2');
var SudokuModel = require('./libs/mongoose').SudokuModel;
var UserModel = require('./libs/mongoose').UserModel;
var sudoku = require('./libs/sudoku.js');
var player = require('./libs/player.js');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients = {};
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.get('/api', passport.authenticate('bearer', { session: false }), function (req, res) {
res.send('API is running');
});
app.post('/oauth/token', oauth2.token);
app.get('/api/userInfo',
passport.authenticate('bearer', { session: false }),
function(req, res) {
// req.authInfo is set using the `info` argument supplied by
// `BearerStrategy`. It is typically used to indicate scope of the token,
// and used in access control checks. For illustrative purposes, this
// example simply returns the scope in the response.
res.json({ user_id: req.user.userId, name: req.user.username, scope: req.authInfo.scope })
}
);
app.get('/ErrorExample', function(req, res, next){
next(new Error('Random error!'));
});
app.post('/api/register', function(req, res) {
UserModel.findOne({ username: req.body.username }, function(err, user) {
if (user) {
log.info("Username already exists - %s:%s",user.username,user.password);
return res.send({ error: 'Username already exists' });
} else {
var newuser = new UserModel({username : req.body.username, password : req.body.password});
newuser.save(function(err, newuser) {
if (err) {
log.error(err);
return res.send({ error: error(err)});
} else {
log.info("New user - %s:%s",newuser.username,newuser.password);
return res.send({ status: 'Registration Successful' });
}
});
}
});
});
app.get('/sudoku/generate', function(req, res) {
return res.send({ sudoku: sudoku.generate("medium") });
});
app.get('/sudoku/generate-string', function(req, res) {
return res.send({ sudoku: sudoku.generate("medium").toString() });
});
app.get('/sudoku/generate/:id', function(req, res) {
if (req.params.id == 'easy' || req.params.id == 'medium' || req.params.id == 'hard') return res.send({ sudoku: sudoku.generate(req.params.id)});
return res.send({ error: 'Last argument has to be "easy", "medium" or "hard"' });
});
app.get('/sudoku/generate-string/:id', function(req, res) {
if (req.params.id == 'easy' || req.params.id == 'medium' || req.params.id == 'hard') return res.send({ sudoku: sudoku.generate(req.params.id).toString()});
return res.send({ error: 'Last argument has to be "easy", "medium" or "hard"' });
});
app.post('/sudoku/solve', function(req, res) {
var grid = req.body.grid.split(",").map(Number);
return res.send({ sudoku: sudoku.solve(grid) });
});
app.post('/sudoku/solve-string', function(req, res) {
var grid = req.body.grid.split(",").map(Number);
return res.send({ sudoku: sudoku.solve(grid).toString() });
});
app.use(express.static(path.join(__dirname, "public")));
require('./libs/auth');
app.use(function(req, res, next){
res.status(404);
log.debug('Not found URL: %s',req.url);
res.send({ error: 'Not found' });
return;
});
app.use(function(err, req, res, next){
res.status(err.status || 500);
log.error('Internal error(%d): %s',res.statusCode,err.message);
res.send({ error: err.message });
return;
});
http.listen(config.get('port'), function(){
log.info('Express server listening on port ' + config.get('port'));
});
// Socket.io
io.on('connection', function (socket) {
socket.emit('connect','hello');
console.log('Client connected ' + socket.id.toString());
socket.on('connect', function (data) {
console.log(data);
console.log("New player has connected: " + data.name);
// socket.emit('welcome', {welcome :'Hello, this is working'});
});
socket.on('move', function (data) {
console.log(data);
});
});