-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
123 lines (106 loc) · 3.28 KB
/
Copy pathserver.ts
File metadata and controls
123 lines (106 loc) · 3.28 KB
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
import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as mongoose from 'mongoose';
import * as morgan from 'morgan';
import * as session from 'express-session';
import * as socket from 'socket.io';
import * as path from 'path';
import * as http from 'http';
import * as colors from 'colors/safe';
import ServerCommunicator from './routes/ServerCommunicator';
import { EXPRESS_SECRET, MAX_COOKIE_AGE } from './constants';
export class Server {
app: any;
port: string;
server: any;
router: any;
db: any;
io: any;
db_name: string;
communicator: ServerCommunicator;
session: any;
private static instance = new Server();
private constructor() {
this.port = '3000';
this.db_name = 'tickettoride';
this.config();
this.communicator = new ServerCommunicator(this.io);
this.routes();
this.sockets();
this.init_db();
}
static instanceOf() {
if (!this.instance) {
this.instance = new Server();
}
return this.instance;
}
config() {
this.app = express();
this.app.use(bodyParser.json());
this.app.use(
bodyParser.urlencoded({
extended: false,
})
);
this.app.use(morgan('dev'));
this.app.use(express.static(path.join(__dirname, 'public')));
this.app.use(function(req: any, res: any, next: any) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
this.session = session({ secret: EXPRESS_SECRET, cookie: { maxAge: MAX_COOKIE_AGE, secure: false, httpOnly: false } });
this.app.use(this.session);
this.app.set('port', this.port);
this.server = http.createServer(this.app);
this.io = socket(this.server);
this.server.listen(this.port);
// debug hosts
// this.server.listen(this.port, "10.37.71.246");
this.server.on('listening', () => {
var addr = this.server.address();
var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
this.msg('Listening on ' + bind);
});
}
init_db() {
mongoose.connect('mongodb://localhost/' + this.db_name);
this.db = mongoose.connection;
this.db.on('error', console.error.bind(console, 'connection error:'));
this.db.once('open', () => {
this.msg('MongoDB Connected');
});
}
sockets() {
// this.communicator.setupSockets(this.io);
this.io.on('connection', (socket: any) => {
socket.on('command', (data: any) => {
this.communicator.handleSocketCommand(data, socket);
});
socket.on('join', (data: any) => {
if (data.room) socket.join(data.room);
});
socket.on('leave', (data: any) => {
if (data.room) socket.leave(data.room);
});
});
}
routes(root = '/') {
this.router = express.Router();
this.router.get('/*', function(req: any, res: any) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
this.router.post('/execute', this.communicator.handleCommand);
this.app.use(root, this.router);
}
post(root = '/', route: any) {
this.router.post(root, route);
}
get(root = '/', route: any) {
this.router.get(root, route);
}
msg(msg: any, color = 'green') {
console.log((colors as any)[color](msg));
}
}