1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | const app = require('express')();
|
20 | const bodyParser = require('body-parser');
|
21 | const Template = require('@accordproject/cicero-core').Template;
|
22 | const Clause = require('@accordproject/cicero-core').Clause;
|
23 | const Engine = require('@accordproject/cicero-engine').Engine;
|
24 |
|
25 | if(!process.env.CICERO_DIR) {
|
26 | throw new Error('You must set the CICERO_DIR environment variable.');
|
27 | }
|
28 |
|
29 | const PORT = process.env.CICERO_PORT | 6001;
|
30 |
|
31 |
|
32 | app.use(bodyParser.json());
|
33 |
|
34 |
|
35 | app.set('port', PORT);
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | app.post('/trigger/:template', async function (req, httpResponse, next) {
|
59 | try {
|
60 | const clause = await initTemplateInstance(req);
|
61 |
|
62 | const engine = new Engine();
|
63 | let result;
|
64 | if(Object.keys(req.body).length === 3 &&
|
65 | Object.prototype.hasOwnProperty.call(req.body,'request') &&
|
66 | Object.prototype.hasOwnProperty.call(req.body,'state') &&
|
67 | Object.prototype.hasOwnProperty.call(req.body,'data')) {
|
68 | clause.setData(req.body.data);
|
69 | result = await engine.trigger(clause, req.body.request, req.body.state);
|
70 | } else if(Object.keys(req.body).length === 2 &&
|
71 | Object.prototype.hasOwnProperty.call(req.body,'request') &&
|
72 | Object.prototype.hasOwnProperty.call(req.body,'data')) {
|
73 | const state = { '$class' : 'org.accordproject.cicero.contract.AccordContractState', 'stateId' : 'ehlo' };
|
74 | clause.setData(req.body.data);
|
75 | result = await engine.trigger(clause, req.body.request, state);
|
76 | delete result.state;
|
77 | } else {
|
78 | throw new Error('Missing request, state or data in /trigger body');
|
79 | }
|
80 | httpResponse.send(result);
|
81 | }
|
82 | catch(err) {
|
83 | return next(err);
|
84 | }
|
85 | });
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | app.post('/parse/:template', async function (req, httpResponse, next) {
|
108 | try {
|
109 | const clause = await initTemplateInstance(req);
|
110 | if(Object.keys(req.body).length === 1 &&
|
111 | Object.prototype.hasOwnProperty.call(req.body,'sample')) {
|
112 | clause.parse(req.body.sample.toString());
|
113 | httpResponse.send(clause.getData());
|
114 | } else {
|
115 | throw new Error('Missing sample in /parse body');
|
116 | }
|
117 | } catch(err) {
|
118 | return next(err);
|
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 | app.post('/draft/:template', async function (req, httpResponse, next) {
|
145 | try {
|
146 | const clause = await initTemplateInstance(req);
|
147 | if(Object.keys(req.body).length === 1 &&
|
148 | Object.prototype.hasOwnProperty.call(req.body,'data')) {
|
149 | clause.setData(req.body.data);
|
150 | httpResponse.send(clause.draft());
|
151 | } else if(Object.keys(req.body).length === 2 &&
|
152 | Object.prototype.hasOwnProperty.call(req.body,'data') &&
|
153 | Object.prototype.hasOwnProperty.call(req.body,'options')) {
|
154 | clause.setData(req.body.data);
|
155 | httpResponse.send(clause.draft(req.body.options));
|
156 | } else {
|
157 | throw new Error('Missing data or options in /draft body');
|
158 | }
|
159 | }
|
160 | catch(err) {
|
161 | return next(err);
|
162 | }
|
163 | });
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | async function initTemplateInstance(req) {
|
171 | const template = await Template.fromDirectory(`${process.env.CICERO_DIR}/${req.params.template}`);
|
172 | return new Clause(template);
|
173 | }
|
174 |
|
175 | const server = app.listen(app.get('port'), function () {
|
176 | console.log('Server listening on port: ', app.get('port'));
|
177 | });
|
178 |
|
179 | module.exports = server; |
\ | No newline at end of file |