Skip to content

Commit ff6fe9a

Browse files
Serve JSON from /settings/paymentPointer.json
1 parent 307874c commit ff6fe9a

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

lib/payment-pointer-discovery.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
* @module payment-pointer-discovery
44
*/
55
const express = require('express')
6+
const { promisify } = require('util')
7+
const fs = require('fs')
68

79
module.exports = paymentPointerDiscovery
810

11+
const SETTING_FILE_PATH = '/settings/paymentPointer.json'
12+
913
/**
1014
* Returns a set of routes to deal with server payment pointer discovery
1115
* @method paymentPointerDiscovery
@@ -28,9 +32,36 @@ function paymentPointerDiscovery () {
2832
* @param next
2933
*/
3034
function paymentPointerDocument () {
31-
return (req, res) => {
32-
res.json({
33-
hello: 'world'
34-
})
35+
return async (req, res) => {
36+
try {
37+
const ldp = req.app.locals.ldp
38+
const url = ldp.resourceMapper.resolveUrl(req.hostname, SETTING_FILE_PATH)
39+
const contentType = 'application/json'
40+
const createIfNotExists = true
41+
const { path } = await ldp.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists })
42+
let body
43+
try {
44+
// Read the file from disk
45+
body = await promisify(fs.readFile)(path, { encoding: 'utf8' })
46+
} catch (e) {
47+
if (e.message.startsWith('ENOENT: no such file or directory,')) {
48+
res.json({
49+
error: `Please create ${SETTING_FILE_PATH} on your pod`
50+
})
51+
}
52+
}
53+
let obj
54+
try {
55+
// Read the file from disk
56+
obj = JSON.parse(body)
57+
} catch (e) {
58+
res.json({
59+
error: `Please make sure ${SETTING_FILE_PATH} contains valid JSON`
60+
})
61+
}
62+
res.json(obj)
63+
} catch (e) {
64+
res.json({ fail: e.message })
65+
}
3566
}
3667
}

0 commit comments

Comments
 (0)