-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
154 lines (146 loc) Β· 4.22 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
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
'use strict';
const express = require('express');
const request = require('superagent');
const path = require('path');
const { readFileSync } = require('fs');
const app = express();
const config = require('common-env/withLogger')(console).getOrElseAll({
port: 3000,
// where this server can be reached from outside (e.g. "https://mydomain.com:8083")
endpoint: 'http://localhost:3000',
});
const { toNumber } = require('lodash');
const BLOCK_BITCOIN_TEMPLATE = readFileSync(
path.resolve(__dirname, './static/template.html'),
'utf-8'
);
// expose BlockProviderConfig route
app.get('/configurations', (req, res) =>
res.json([
{
name: 'cms-block-provider-example-bitcoin',
type: 'Display',
configurations: [
{
version: '1.0.0',
endpoint: {
// we specify a relative url to the current blockprovider public endpoint
// we could also specify a FQDN
url: `/block/bitcoin`,
method: 'GET',
// this block depends on time, so it's not pure and won't be cached for a long time
pure: false,
required: ['threshold'],
parameters: [
{
name: 'threshold',
in: 'query',
type: 'integer',
format: 'int32',
minimum: 0,
maximum: 100000,
},
],
ui: {
threshold: {
'ui:autofocus': true,
'ui:emptyValue': '10200',
'ui:title': 'Bitcoin threshold',
'ui:description':
'At which point should the Bitcoin threshold should be defined',
},
},
},
templates: [
{
name: 'default-theme',
source: BLOCK_BITCOIN_TEMPLATE,
engine: 'mustache',
assets: {
js: [],
css: [`${config.endpoint}/static/style.css`],
fonts: [],
images: [`${config.endpoint}/static/bitcoin.png`],
},
},
{
name: 'troll-theme',
source: BLOCK_BITCOIN_TEMPLATE,
engine: 'mustache',
assets: {
js: [],
css: [`${config.endpoint}/static/style.css`],
fonts: [],
images: [`${config.endpoint}/static/bitcoin.png`],
},
},
],
external: {
parameters: [
{
name: 'bid',
// any value will work here
in: 'query',
schema: {
type: 'integer',
format: 'int32',
minimum: 0,
maximum: 100000,
},
},
{
name: 'ask',
// any value will work here
in: 'query',
schema: {
type: 'integer',
format: 'int32',
minimum: 0,
maximum: 100000,
},
},
],
ui: {
ask: {
'ui:title': 'Bitcoin ask price',
},
bid: {
'ui:title': 'Bitcoin bit price',
},
},
},
},
],
},
])
);
app.get('/block/bitcoin', (req, res) => {
request
.get('https://www.bitstamp.net/api/ticker/')
.set('accept', 'json')
.end((err, { body }) => {
console.log(err, body);
err
? res.status(500).json(err)
: res.json({
internal: {
data: {
last: toNumber(body.last),
},
},
external: {
data: {
bid: toNumber(body.bid),
ask: toNumber(body.ask),
},
},
});
});
});
// expose BlockProviderConfig route
require('./route.documentation')(app);
app.use('/static', express.static(path.resolve(__dirname, 'static')));
// start to listen to http
app.listen(config.port, () =>
console.log(`π Example BlockProvider listening on port ${config.port}!`)
);