Skip to content

Commit 15dd9ee

Browse files
committed
remove custom mount, update examples, add script to build dashboard
1 parent a410c5f commit 15dd9ee

14 files changed

+78
-107
lines changed

Diff for: bin/lib/start.js

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function bin(argv, server) {
7676
const iotap = require('../../')
7777
let app
7878
const options = {
79-
value: 0,
8079
websockets: true,
8180
api: true,
8281
dashboard: true

Diff for: docs/README.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ Creates and returns a express server.
6363

6464
- `app` **[object][33]** an express application.
6565
- `options` **[object][33]** an options object. (optional, default `{}`)
66-
- `options.mount` **[String][34]** The payment route name.
67-
- `options.value` **[Number][32]** The default IOTA value.
66+
- `options.websockets` **[Boolean][34]** websockets.
67+
- `options.dashboard` **[Boolean][34]** dashboard at /iotapay.
68+
- `options.api` **[Boolean][34]** api at /iotapay/api.
6869

6970
### Examples
7071

@@ -73,9 +74,9 @@ Creates and returns a express server.
7374
var paymentModule = require('iota-payment')
7475
var app = require('express')()
7576

76-
let server = paymentModule.createServer(app, {mount: '/payments'})
77+
let server = paymentModule.createServer(app, {api: true})
7778

78-
// Start server with iota-payment module on '/payments'
79+
// Start server with iota-payment api on '/iotapay/api'
7980
server.listen(3000, function () {
8081
console.log(`Server started on http://localhost:3000 `)
8182
})
@@ -266,8 +267,8 @@ Creates and returns a payout.
266267
- `payoutData` **[object][33]** data for the payout
267268
- `payoutData.address` **address** 90 trytes iota address (with checksum)
268269
- `payoutData.amount` **[number][32]** amount of iotas
269-
- `payoutData.message` **[string][34]?** message which will be send with the transaction
270-
- `payoutData.tag` **[string][34]?** tryte tag
270+
- `payoutData.message` **[string][37]?** message which will be send with the transaction
271+
- `payoutData.tag` **[string][37]?** tryte tag
271272
- `payoutData.startIndex` **[number][32]?** custom start index to search for inputaddresses
272273
- `payoutData.endIndex` **[number][32]?** custom end index to search for inputaddresses
273274

@@ -438,10 +439,10 @@ Returns **[Object][33]** payment
438439
WebSockets with socket.io
439440

440441
backend:
441-
[https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websockets.js][37]
442+
[https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websockets.js][38]
442443

443444
frontend:
444-
[https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websocket.html][38]
445+
[https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websocket.html][39]
445446

446447
[1]: #getbalance
447448

@@ -509,12 +510,14 @@ frontend:
509510

510511
[33]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
511512

512-
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
513+
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
513514

514515
[35]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
515516

516517
[36]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
517518

518-
[37]: https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websockets.js
519+
[37]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
519520

520-
[38]: https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websocket.html
521+
[38]: https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websockets.js
522+
523+
[39]: https://github.com/machineeconomy/iota-payment/blob/master/examples/06_websocket.html

Diff for: examples/01_simple_server.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
var paymentModule = require('..')
2-
var app = require('express')()
1+
const paymentModule = require('..')
32

4-
let server = paymentModule.createServer(app)
5-
6-
// Start server with iota-payment module on '/'
7-
server.listen(3000, function () {
8-
console.log(`Server started on http://localhost:3000 `)
9-
})
3+
// Start iota-payment module
4+
paymentModule.createServer()

Diff for: examples/02_custom_server.js

-19
This file was deleted.

Diff for: examples/02_express_server_with_gui+api.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const paymentModule = require('..')
2+
const app = require('express')()
3+
4+
app.get("/", (req, res) => {
5+
res.send('hello world from 02_express_server_with_gui+api example!');
6+
});
7+
8+
let options = {
9+
api: true,
10+
//"npm run build:dashboard" is required for the dashboard
11+
dashboard: true
12+
// ...
13+
}
14+
15+
let server = paymentModule.createServer(app, options)
16+
17+
// Start server with iota-payment dashboard on '/iotapay' and api on '/iotapay/api'
18+
server.listen(3000, () => {
19+
console.log(`Server started on http://localhost:3000 `)
20+
})

Diff for: examples/03_events.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
var paymentModule = require('..')
2-
var app = require('express')()
32

4-
var options = {
5-
mount: '/',
6-
value: 1
7-
// ...
8-
}
9-
app.get("/", function (req, res) {
10-
res.send('hello world from 03_events example!');
11-
});
12-
13-
let server = paymentModule.createServer(app, options)
14-
15-
// Start server with iota-payment module on '/'
16-
server.listen(3000, function () {
17-
console.log(`Server started on http://localhost:3000 `)
18-
})
3+
// Start server with iota-payment module on
4+
paymentModule.createServer()
195

206
//Create an event handler which is called, when a payment was created
217
var onPaymentCreated = function (payment) {

Diff for: examples/04_payment.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@ var paymentModule = require('..')
33
async function run() {
44
// get all payments
55
console.log("All payments:");
6-
await paymentModule.payment.getPayments().then(payments => {
7-
console.log(payments)
8-
})
6+
let allPayments = await paymentModule.payment.getPayments()
7+
console.log(allPayments)
98

109
// get all open payments
1110
console.log("Open payments:");
12-
await paymentModule.payment.getOpenPayments().then(payments => {
13-
console.log(payments)
14-
})
11+
let openPayments = await paymentModule.payment.getOpenPayments()
12+
console.log(openPayments)
1513

1614
// create a payment
1715
console.log("Create payment:");
18-
let id;
19-
await paymentModule.payment.createPayment({ value: 1, data: { "test": "123" } }).then(payment => {
20-
console.log(payment)
21-
id = payment.id
22-
})
16+
let payment = await paymentModule.payment.createPayment({ value: 1, data: { "test": "123" } })
17+
console.log(payment)
2318

2419
// get a specific payment
2520
console.log("Get payment:");
26-
await paymentModule.payment.getPaymentByID(id).then(payment => {
27-
console.log(payment)
28-
})
21+
let specificPayment = await paymentModule.payment.getPaymentByID(payment.id)
22+
console.log(specificPayment)
23+
2924
}
3025
run()

Diff for: examples/05_payout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var paymentModule = require('..')
33
let payoutObject = {
44
//required
55
address: 'IKTYKKCZFZZECSFIJYWYSUUTXCIBNIFPFSPGUIUUAYONDYUSHEZVQBNPDYUTDMTNTHBLABCYYLZKLGIVCINGBALQVX',
6-
value: 1,
6+
value: 0,
77
//optional
88
message: 'Example message',
99
tag: 'TRYTETAG',

Diff for: examples/06_websocket.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<script>
5252
var socketClientId
5353
const socket = io('http://localhost:3000', {
54-
path: '/payments/socket'
54+
path: '/iotapay/socket'
5555
});
5656

5757
var app = new Vue({

Diff for: examples/06_websockets.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ app.post('/payout', async (req, res) => {
4444
})
4545

4646
var options = {
47-
mount: '/payments',
48-
value: 0,
4947
websockets: true
5048
// ...
5149
}
5250

5351
let server = paymentModule.createServer(app, options)
5452

55-
// Start server with iota-payment module on '/payments'
53+
// Start server
5654
server.listen(3000, function () {
5755
console.log(`Server started on http://localhost:3000 `)
5856
})

Diff for: lib/WebSockets.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ var allowedOrigins =
44
process.env.socket_origins || 'http://localhost:* http://127.0.0.1:*'
55

66
var socketServer = undefined
7-
var socket_path = '/socket'
7+
var socket_path = '/iotapay/socket'
88
var clients = []
99

10-
function start(server, mount = '') {
10+
function start(server) {
1111
if (socketServer) return
1212

13-
if (mount != '/') {
14-
socket_path = mount + '/socket'
15-
}
1613
console.log(`start socketServer on '${socket_path}'`)
1714

1815
socketServer = io(server, {

Diff for: lib/create-server.js

+15-27
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@ const dotenv = require('dotenv')
44
dotenv.config()
55

66
const express = require('express')
7-
const fs = require('fs')
8-
const https = require('https')
97
const http = require('http')
108
const bodyParser = require('body-parser')
11-
var cors = require('cors')
9+
const cors = require('cors')
10+
const passport = require('passport')
11+
const { ExtractJwt, Strategy } = require('passport-jwt')
1212

1313
const eventEmitter = require('./eventEmitter')
14-
1514
const paymentHandler = require('./paymentHandler')
1615
const payoutHandler = require('./payoutHandler')
1716
const WebSockets = require('./WebSockets.js')
1817
const { startZmq } = require('./zmq.js')
19-
const passport = require('passport')
20-
const { ExtractJwt, Strategy } = require('passport-jwt')
2118
const api = require('./api')
2219

2320
// Start Handler
@@ -28,17 +25,18 @@ payoutHandler.start()
2825
* Creates and returns a express server.
2926
* @param {object} app - an express application.
3027
* @param {object} options - an options object.
31-
* @param {String} options.mount - The payment route name.
32-
* @param {Number} options.value - The default IOTA value.
28+
* @param {Boolean} options.websockets - websockets.
29+
* @param {Boolean} options.dashboard - dashboard at /iotapay.
30+
* @param {Boolean} options.api - api at /iotapay/api.
3331
* @returns {object} an http server
3432
* @example
3533
* // creates a simple server
3634
* var paymentModule = require('iota-payment')
3735
* var app = require('express')()
3836
*
39-
* let server = paymentModule.createServer(app, {mount: '/payments'})
37+
* let server = paymentModule.createServer(app, {api: true})
4038
*
41-
* // Start server with iota-payment module on '/payments'
39+
* // Start server with iota-payment api on '/iotapay/api'
4240
* server.listen(3000, function () {
4341
* console.log(`Server started on http://localhost:3000 `)
4442
* })
@@ -75,32 +73,22 @@ function createServer(app, options = {}) {
7573
passport.serializeUser(function(user, done) {
7674
done(null, user)
7775
})
78-
let mount = options.mount || '/iotapay'
7976

8077
if (options.api) {
81-
app.use(mount + '/api', api({}))
78+
app.use('/iotapay/api', api({}))
79+
console.log('API on /iotapay/api')
8280
}
8381

8482
if (options.dashboard) {
85-
app.use(mount, express.static('dashboard/dist'))
83+
app.use('/iotapay', express.static('dashboard/dist'))
84+
console.log('Dasboard on /iotapay')
8685
}
8786

88-
// Removing ending '/'
89-
if (mount.length > 1 && mount[mount.length - 1] === '/') {
90-
mount = mount.slice(0, -1)
91-
}
92-
93-
let value = options.value || 0
94-
95-
console.log('Base URL (--mount): ' + mount)
96-
97-
let server
98-
99-
server = http.createServer(app)
87+
let server = http.createServer(app)
10088

10189
let websockets = options.websockets || false
10290
if (websockets) {
103-
WebSockets.start(server, mount)
91+
WebSockets.start(server)
10492
}
10593

10694
if (process.env.fast_but_risky == 'true' || process.env.zmq == 'true') {
@@ -112,7 +100,7 @@ function createServer(app, options = {}) {
112100
}
113101
}
114102

115-
if (process.env.debug == 'full') {
103+
if (process.env.debug == 'basic' || process.env.debug == 'full') {
116104
console.log('debug true')
117105
}
118106

Diff for: lib/zmq.js

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ async function check_transfer(txdata) {
9797
//check if there is no other outgoing transfer from the input addresses
9898
await check_for_outgoing_transfers(txs)
9999
}
100+
//return if already confirmed
101+
let payment = db
102+
.get('payments')
103+
.find({ address: checksum.addChecksum(txdata[2]) })
104+
.value()
105+
if (payment.payed == true) {
106+
return
107+
}
100108
//update payment
101109
let new_payment = db
102110
.get('payments')

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"lint": "eslint lib --color",
1010
"p": "prettier --write \"lib/**/*.js\"",
1111
"p:w": "onchange \"lib/**/*.js\" -- prettier --write {{changed}}",
12-
"docs:build": "documentation build lib/** -f md -o docs/README.md"
12+
"docs:build": "documentation build lib/** -f md -o docs/README.md",
13+
"build:dashboard": "cd ./dashboard && npm i && npm run build:prod"
1314
},
1415
"author": "huhn511",
1516
"license": "MIT",

0 commit comments

Comments
 (0)