-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpSrv.js
160 lines (89 loc) · 2.73 KB
/
httpSrv.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
150
151
152
153
154
155
156
157
158
159
160
"use strict";
import express from 'express'
import config from './config.js'
let mod = {
start: function(db, domainName, port){
const app = express();
app.use(express.json());
app.use(express.static('./front/html/dist'));
app.get('/', (_req, res) => {
res.redirect('/index.html');
})
let refreshInterval = config.getConfig("MailRefreshInterval");
app.post('/addresses', (_req, res) => {
try {
let rows = db.prepare("SELECT addr FROM address").all();
res.json({addresses: rows, refreshInterval: refreshInterval});
} catch(err) {
console.log("DB get addresses fail")
console.log(err)
}
})
app.post('/domain', (req, res) => {
if (domainName){
return res.status(200).send(domainName);
}else{
return res.status(200).send(req.headers.host.split(':')[0]);
}
})
app.post('/addAddress', (req, res) => {
const json = req.body;
try {
let rows = db.prepare("SELECT addr FROM address WHERE addr = ?").all(json.address);
if(rows.length > 0){
return res.status(200).send("exist");
}
db.prepare("INSERT INTO address (addr) VALUES (?)").run(json.address);
return res.status(200).send("done");
} catch(err) {
console.log("DB add addresses fail")
console.log(err)
}
})
app.post('/deleteAddress', (req, res) => {
const json = req.body;
try {
db.prepare("DELETE FROM address WHERE addr = ?").run(json.address);
db.prepare("DELETE FROM mail WHERE recipient = ?").run(json.address);
return res.status(200).send("done");
} catch(err) {
console.log("DB delete address fail")
console.log(err)
}
})
app.post('/mails', (req, res) => {
const json = req.body;
try {
let rows = db.prepare("SELECT id, sender, subject FROM mail WHERE recipient = @recipient ORDER BY id DESC LIMIT @mailCount OFFSET (@page-1)*@mailCount").all({recipient: json.addr, page: json.page, mailCount: config.getConfig('MailCountPerPage')});
res.json(rows);
} catch(err) {
console.log("DB get mails fail")
console.log(err)
}
});
app.post('/mailData', (req, res) => {
const json = req.body;
try {
let rows = db.prepare("SELECT sender, subject, content FROM mail WHERE id = ?").all(json.id);
res.json(rows[0])
} catch(err) {
console.log("DB get mail data fail")
console.log(err)
}
});
app.post('/deleteMail', (req, res) => {
const json = req.body;
try {
db.prepare("DELETE FROM mail WHERE id = ?").run(json.id);
res.status(200).send();
} catch(err) {
console.log("DB delete mail fail")
console.log(err)
}
})
app.listen(port, () => {
console.log('http server listening at port: ' + port);
})
}
}
export default mod;