-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
118 lines (101 loc) · 3.49 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import bodyParser from "body-parser";
import compression from "compression";
import express from "express";
import helmet from "helmet";
import morgan from "morgan";
import cors from "cors";
import _ from "underscore";
import fs from "fs";
import tmp from "tmp";
import { name, version } from "../package.json";
import config from "./config";
import * as parser from "../../dependencies/modelica-json/lib/parser";
import {
writeControlSequenceDocument,
SequenceData,
} from "./sequence";
const app = express();
// Apply global middleware
app.use(helmet());
app.use(compression());
app.use(bodyParser.json());
app.use(express.static("public"));
app.use(cors());
const logMode = config.NODE_ENV == "development" ? "dev" : "combined";
const allowedOrigins = config.FE_ORIGIN_URL.split(',');
app.use(morgan(logMode));
// TODO: Investigate if 'tmp.dirSync' can be used to create the directory
// I haven't found the correct way to clean up on app exit/restart
const tempDirPath = "/tmp/tmp-linkage-widget";
if (!fs.existsSync(tempDirPath)) {
fs.mkdirSync(tempDirPath);
}
// accept json in body, hand off to service
app.get("/", (req, res) => {
const appSignature = `${name}@${version}`;
res.send(appSignature);
});
app.post("/api/jsontomodelica", async (req, res) => {
const jsonToConvert = req.body;
const jsonFile = tmp.fileSync();
fs.writeSync(jsonFile.fd, JSON.stringify(jsonToConvert));
const modelica = parser.convertToModelica(jsonFile.name, tempDirPath);
// remove temp file
jsonFile.removeCallback();
res.send(modelica);
});
app.post("/api/modelicatojson", async (req, res) => {
const request = req.body;
const modelicaFile = tmp.fileSync();
const { format, modelica, parseMode } = request;
const prettyPrint = false; // format json
fs.writeSync(modelicaFile.fd, modelica);
let response: any;
try {
// getJsons will aways return an empty array (but it looks like it should?).
// To get around this read from the file that gets output during parsing
parser.getJsons(
[modelicaFile.name],
parseMode,
format,
tempDirPath,
prettyPrint,
);
// NOTE: 'modelicaFile.name' is a full path name (e.g. '/tmp/<tmp-file-name>)!
// For now I'm using a kludge to re-use this full path to get to the output path
// full path looks something like: /<tmpDirPath>/json/tmp/<tmp-file-name>
// TODO: figure out a better way to coordinate tempfile generation and teardown
response = fs.readFileSync(`${tempDirPath}/json/${modelicaFile.name}`, {
encoding: "utf8",
});
} catch (error) {
// TODO: put in a proper error response
response = error;
}
// remove temp file
modelicaFile.removeCallback();
res.send(response);
});
app.post("/api/sequence", async (req, res) => {
// The Control Sequence Input consists of mock data at the moment.
// Please note that this is a very naive data format.
// The shape of this object will most likely need to be modified and massaged when we work with real data.
const sequenceData: SequenceData = req.body;
const origin: string = req.headers.origin || '';
if (allowedOrigins.includes(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
try {
const { file, filePath } = await writeControlSequenceDocument(sequenceData);
res.send(file);
res.on('finish', () => {
fs.unlinkSync(filePath);
});
} catch (error) {
console.error(error);
res.send(error);
}
});
app.listen(config.PORT, () => {
console.log(`Listening on port ${config.PORT}`);
});