This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
101 lines (84 loc) · 3.06 KB
/
index.ts
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
import express, { Request, Response } from 'express'
import { urlencoded, json } from 'body-parser'
import cookieParser from 'cookie-parser'
import compile from 'bpmn-sol'
import SignavioParser from './packages/signavio-parser'
import bodyParser from 'body-parser'
import helmet from 'helmet'
import path from 'path'
import fetchCookie from './server/api/fetchCookie'
import cors from 'cors'
import getCookieDetails from './server/helpers/getCookieDetails'
import fetchDiagramXml from './server/api/fetchDiagramXml'
import handleFileUpload from './server/api/handleFileUpload'
const app: express.Application = express()
const PORT = process.env.PORT || 8080
const HOST = process.env.HOST || 'http://127.0.0.1:8080'
app.use(express.static(`${path.resolve("./")}/client/build`))
app.use(bodyParser.json())
app.use(helmet())
app.use(cookieParser())
app.use(cors({ origin: true }))
app.use(urlencoded({ extended: false }))
app.post('/submit', async (req: Request, res: Response, next) => {
const {
username,
password,
revisionId,
globalVariables,
contractName,
} = req.body
try {
const cookieData = await fetchCookie({ name: username, password: password, tokenonly: 'true' })
try {
const diagramXml = await fetchDiagramXml({ revisionId, ...getCookieDetails(cookieData) })
const parser = SignavioParser.parse(diagramXml,
globalVariables,
contractName)
const isSuccessfullyParsed = parser.getTestResult()
if (isSuccessfullyParsed) {
const contract: any = await compile(parser.getXmlWithGlobalVariables())
res.send({ status: 200, message: 'Success', data: contract })
} else {
res.send({ status: 500, message: 'Tests failed. Please check the diagram details.' })
}
} catch (error) {
res.send({ status: 404, message: 'Error while fetching diagram. Please provide the correct diagram details' })
}
}
catch (error) {
res.send({ status: 401, message: 'Error authenticating. Please check your credentials.' })
}
})
app.post('/compile', async (req: Request, res: Response, next) => {
const {
xmlString,
globalVariables,
contractName,
} = req.body
try {
const parser = SignavioParser.parse(xmlString,
globalVariables,
contractName)
const isSuccessfullyParsed = parser.getTestResult()
if (isSuccessfullyParsed) {
const contract: any = await compile(parser.getXmlWithGlobalVariables())
res.send({ status: 200, message: 'Success', data: contract })
} else {
res.send({ status: 500, message: 'Tests failed. Please check the diagram details.' })
}
} catch (error) {
res.send({ status: 404, message: 'Error while compiling to smart contract. Please provide the correct diagram details' })
}
})
interface CustomRequest extends Request {
file: any
rawBody: any
}
app.post('/file', async (req: CustomRequest, res) => {
await handleFileUpload(req, res)
})
app.get('*', (req, res) => {
res.sendFile(`${path.resolve('./')} /client/build/index.html`)
})
app.listen(PORT, () => console.log(`app listening on port ${PORT} `))