-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
114 lines (95 loc) · 3.04 KB
/
routes.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
const express = require("express");
const axios = require("axios");
const xmlparser = require("express-xml-bodyparser");
const { keycloakForSiebel,keycloakForFormRepo} = require("./keycloak.js");
const generateTemplate = require("./generateHandler");
const getFormsFromFormTemplate = require("./formRepoHandler");
const router = express.Router();
const FORM_SERVER_URL = process.env.FORMSERVERURL;
const ENDPOINT_URL = process.env.ENDPOINTURL;
// Form Map
const formMap = new Map();
formMap.set("id1", "form1");
formMap.set("id2", "form2");
function findForm(input_path) {
return formMap.get(input_path);
}
// Status route
router.get("/status", (req, res) => {
res.send({ status: "running" });
});
// API forwarding route
router.get("/api*", async (req, res) => {
try {
const grant =
await keycloakForSiebel.grantManager.obtainFromClientCredentials();
const destinationPath = RegExp("api/(.*)").exec(req.path)[1];
let endpointUrl = `${ENDPOINT_URL}${destinationPath}`;
endpointUrl += "?ViewMode=Catalog&workspace=dev_sadmin_bz";
const newResp = await axios.get(endpointUrl, {
headers: {
Authorization: `Bearer ${grant.access_token.token}`,
},
});
res.json(newResp.data);
} catch (err) {
console.error("API error: " + err.message);
res.status(500).send({ error: err.message });
}
});
// XML form post route
router.post("/form1", xmlparser(), async (req, res) => {
console.log(req.body);
res.status(200).json("{success!}");
});
// XML push route
router.post("/xmlPush/:formId", xmlparser(), async (req, res) => {
try {
const response_path = findForm(req.params.formId);
if (response_path === undefined) {
return res.status(404).send({ error: "Form not found" });
}
const forwardAddress = `${FORM_SERVER_URL}/${response_path}`;
console.log(forwardAddress);
res.redirect(307, forwardAddress);
} catch (err) {
res.status(500).send({ error: err.message });
}
});
// Send XML file route
router.get("/xml", async (req, res) => {
const options = {
root: "./common",
};
res.sendFile("data.xml", options, (err) => {
if (err) {
console.error(`XML Error: ${err.message}`);
res.status(500).send({ error: "Internal server error." });
}
});
});
// Save data route
router.post("/saveData", async (request, response) => {
console.log(request.body);
response.status(200).json("{success!}");
});
// Generate route
router.post("/generate", generateTemplate);
router.get("/getAllForms", async (request,response) => {
try {
const grant =
await keycloakForFormRepo.grantManager.obtainFromClientCredentials();
let endpointUrl = `http://localhost:3030/api/forms-list`;
const forms = await axios.get(endpointUrl, {
headers: {
Authorization: `Bearer ${grant.access_token.token}`,
},
});
console.log("forms",forms);
response.json(forms.data);
} catch (err) {
console.error("API error: " + err.message);
response.status(500).send({ error: err.message });
}
});
module.exports = router;