-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathapi.js
122 lines (108 loc) · 3.24 KB
/
api.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
import feathers from 'feathers';
import morgan from 'morgan';
import session from 'express-session';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import hooks from 'feathers-hooks';
import rest from 'feathers-rest';
import socketio from 'feathers-socketio';
import isPromise from 'is-promise';
import PrettyError from 'pretty-error';
import config from './config';
import middleware from './middleware';
import services from './services';
import * as actions from './actions';
import mapUrl from './utils/url.js';
import auth, { socketAuth } from './services/authentication';
process.on('unhandledRejection', error => console.error(error));
const pretty = new PrettyError();
const app = feathers();
app
.set('config', config)
.use(morgan('dev'))
.use(cookieParser())
.use(
session({
secret: 'react and redux rule!!!!',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60000 }
})
)
.use(bodyParser.urlencoded({ extended: true }))
.use(bodyParser.json());
const actionsHandler = (req, res, next) => {
const splittedUrlPath = req.url.split('?')[0].split('/').slice(1);
const { action, params } = mapUrl(actions, splittedUrlPath);
req.app = app;
const catchError = error => {
console.error('API ERROR:', pretty.render(error));
res.status(error.status || 500).json(error);
};
if (action) {
try {
const handle = action(req, params);
(isPromise(handle) ? handle : Promise.resolve(handle))
.then(result => {
if (result instanceof Function) {
result(res);
} else {
res.json(result);
}
})
.catch(reason => {
if (reason && reason.redirect) {
res.redirect(reason.redirect);
} else {
catchError(reason);
}
});
} catch (error) {
catchError(error);
}
} else {
next();
}
};
app
.configure(hooks())
.configure(rest())
.configure(socketio({ path: '/ws' }))
.configure(auth)
.use(actionsHandler)
.configure(services)
.configure(middleware);
if (process.env.APIPORT) {
app.listen(process.env.APIPORT, err => {
if (err) {
console.error(err);
}
console.info('----\n==> 🌎 API is running on port %s', process.env.APIPORT);
console.info('==> 💻 Send requests to http://localhost:%s', process.env.APIPORT);
});
} else {
console.error('==> ERROR: No APIPORT environment variable has been specified');
}
const bufferSize = 100;
const messageBuffer = new Array(bufferSize);
let messageIndex = 0;
app.io.use(socketAuth(app));
app.io.on('connection', socket => {
const user = socket.feathers.user ? { ...socket.feathers.user, password: undefined } : undefined;
socket.emit('news', { msg: "'Hello World!' from server", user });
socket.on('history', () => {
for (let index = 0; index < bufferSize; index += 1) {
const msgNo = (messageIndex + index) % bufferSize;
const msg = messageBuffer[msgNo];
if (msg) {
socket.emit('msg', msg);
}
}
});
socket.on('msg', data => {
const message = { ...data, id: messageIndex };
messageBuffer[messageIndex % bufferSize] = message;
messageIndex += 1;
app.io.emit('msg', message);
});
});