This repository has been archived by the owner on May 24, 2023. It is now read-only.
/
server.js
77 lines (68 loc) · 2.48 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
var express = require('express');
var Address = require('./models/Address.js');
var bodyParser = require("body-parser");
var Mailgun = require('mailgun-js');
var Swagger = require('swagger-ui-express');
var docs = require('./docs/swagger.json')
//TODO: hide the next two values before commit
var api_key = "";
var domain = "";
var mailgun = new Mailgun({apiKey: api_key, domain: domain});
var app = express();
app.use('/api', Swagger.serve, Swagger.setup(docs));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/validate', function(req, res) {
var addr = new Address(req.body.signature, req.body.msg, req.body.address);
var body = addr.isValid();
console.log("Sending:\t" + JSON.stringify(body));
return res.send(JSON.stringify(body));
});
app.post('/send', function(req, res){
console.log("/send endpoint called.");
var addr = new Address(req.body.signature, req.body.from);
if(addr.isValid(req.body.message)){
console.log("Address was valid");
var data = {
from: req.body.from + "@" + domain,
to: req.body.to,
subject: req.body.subject,
html: req.body.message
};
mailgun.messages().send(data, function (err, body) {
if (err) {
res.status(500).send(err);
console.log("Error: ", err);
}
else {
res.status(200).send(body);
console.log("Success: " + body);
}
});
}
});
//required fields: signature, address
app.post('/inbox', function(req, res){
console.log('/create_inbox called...');
var addr = new Address(req.body.signature, req.body.address);
if(addr.isValid(req.body.address)){
//TODO: Change the description to the txid of the payment
mailgun.post('/routes', {'priority': 0,
'description': addr.location,
'expression': 'match_recipient("' + addr.location + '")',
//TODO: make the following field dynamic. (Maybe there's a free version that just forwards to your standard inbox)
//This action could also be changed to notify another endpoint on mail reciept, that can store the emails in an s3 bucket or something
'action': 'forward("jamesvorder@gmail.com")'
}, function(error, body){
if(error){
res.status(500).send(error);
console.log("Error: " + error);
} else{
res.status(200).send(body);
console.log("Success: " + JSON.stringify(body));
}
});
}
});
app.listen(3000);
console.log('Listening on port 3000...');