forked from virtadpt/ripple-gateway-appliance-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
88 lines (76 loc) · 2.24 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
var nconf = require('./config/nconf.js');
process.env.DATABASE_URL = nconf.get('DATABASE_URL');
var express = require('express');
var fs = require('fs');
var https = require('https');
var api = require(nconf.get('RIPPLE_DATAMODEL_ADAPTER'));
var passport = require('./config/passport')(api);
app = express();
app.use("/", express.static(__dirname + "/app"));
app.use(express.bodyParser());
api.users.register = function(opts, fn) {
var userOpts = {
name: opts.name,
password: opts.password
};
api.users.create(userOpts, function(err, user) {
if (err) { fn(err, null); return; }
var addressOpts = {
user_id: user.id,
address: opts.ripple_address,
managed: false,
type: "independent"
};
api.rippleAddresses.create(addressOpts, function(err, ripple_address) {
if (err) { fn(err, null); return; }
api.externalAccounts.create({ name: "default", user_id: user.id }, function(err, account){
if (err) { fn(err, null); return; }
console.log('account', account);
var response = user.toJSON();
response.ripple_address = ripple_address;
response.external_account = account;
fn(err, response);
});
});
});
};
app.post('/api/v1/register', function(req, res) {
api.users.register(req.body, function(err, user){
if (err) {
res.send(500, { error: err });
} else {
res.send({ user: user });
}
})
});
app.post('/api/v1/users', function(req, res){
var opts = {
name: req.body.name,
password: req.body.password
};
api.users.create(opts, function(err, user){
console.log(err, user);
if (err) {
res.send(500, { error: err });
} else {
res.send({ user: user.toJSON() });
}
});
});
app.get('/ripple.txt', function(req, res) {
res.set({ 'Content-Type': 'text/plain' });
res.send(
"[accounts]\n"+nconf.get('gateway_cold_wallet')+"\n\n"+
"[hotwallets]\n"+nconf.get('gateway_hot_wallet').address
);
});
var ssl = (nconf.get('SSL') && (nconf.get('SSL') != 'false'));
if (ssl) {
app = https.createServer({
key: fs.readFileSync('./certs/server.key'),
cert: fs.readFileSync('./certs/server.crt')
}, app);
}
var host = nconf.get('HOST');
var port = nconf.get('PORT');
app.listen(port, host);