This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (74 loc) · 2.58 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
const express = require('express');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const MamUtils = require('./utils/mamUtils');
let mam;
const asyncInit = () => new Promise((res, rej) => { // eslint-disable-line no-unused-vars
mam = new MamUtils(res);
});
app.prepare()
.then(() => asyncInit())
.then(() => {
const server = express();
server.use(express.json());
server.get('/', (req, res) => {
const actualPage = '/users';
return app.render(req, res, actualPage);
});
server.post('/api/updateLocalAccount', (req, res) => {
console.log('storageUpdate');
mam.accountUpdate(req.body);
return res.send('Success');
});
server.post('/api/mamSend', async (req, res) => {
const { packet, params } = req.body;
console.log(req.body);
await mam.publishMessage(packet, params);
return res.send('Success');
});
server.post('/api/mamFetch', async (req, res) => {
const { id } = req.body;
console.log(`Fetching mam: ${id}`);
const messages = await mam.fetchMessages(id);
return res.send(messages);
});
/* for filtering propose */
server.post('/api/addContact', async (req, res) => {
mam.addContact(req.body.self_id, req.body.new_id);
return res.send('Success');
});
server.get('/api/user/:id', (req, res) => {
const user = mam.accountStore.findOnly({ id: req.params.id });
return res.send(user);
});
server.get('/api/fetchUserList', (req, res) => {
console.log('fetching user');
const list = mam.accountStore.findAll();
return res.send(list);
});
server.get('/users/new', (req, res) => {
const actualPage = '/users/new';
return app.render(req, res, actualPage);
});
server.get('/users/:id', (req, res) => {
const actualPage = '/user';
const queryParams = { id: req.params.id };
return app.render(req, res, actualPage, queryParams);
});
server.get('/claims/info/:transactionHash', (req, res) => {
const actualPage = '/claims/info';
const queryParams = { transactionHash: req.params.transactionHash };
return app.render(req, res, actualPage, queryParams);
});
server.get('*', (req, res) => {
req.url = req.url.replace(/\/$/, '');
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});