-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
109 lines (87 loc) · 2.37 KB
/
main.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
"use strict";
const express = require("express");
const compression = require("compression");
const fs = require("fs");
const bodyParser = require('body-parser');
const _port = 8080;
const _app_folder = 'dist/json-yaml-editor';
const _dataPath = 'db/data.json';
const app = express();
app.use(compression());
app.use(bodyParser.json());
// ---- SERVE STATIC FILES ---- //
app.get('*.*', express.static(_app_folder, {maxAge: '1y'}));
function getData() {
var data = [];
if (fs.existsSync(_dataPath)) {
data = JSON.parse(fs.readFileSync(_dataPath).toString());
}
return data;
}
function updateData(data) {
if (!fs.existsSync('db/')) {
fs.mkdirSync('db/');
}
fs.writeFileSync(_dataPath, JSON.stringify(data, null, '\t'));
}
/* "/api/status"
* GET: Get server status
* PS: it's just an example, not mandatory
*/
app.get("/api/deployment", function (req, res) {
var response = getData()
.map(d => {
return {
id: d.id,
name: d.name
};
});
res.send(response);
});
app.get('/api/deployment/:id', function(req, res) {
var data = getData();
var found = data.find(d => d.id === req.params.id);
res.send(found);
});
app.put('/api/deployment/:id', function(req, res) {
var data = getData();
var found = data.find(d => d.id === req.params.id);
var foundIndex = !!found ? data.indexOf(found) : -1;
if (!found) {
res.status(404).send();
} else {
data[foundIndex] = req.body;
}
updateData(data);
res.send();
});
app.post('/api/deployment', function (req, res) {
var data = getData();
var found = data.find(d => d.id === req.body.id);
var foundIndex = !!found ? data.indexOf(found) : -1;
if (foundIndex >= 0) {
data[foundIndex] = req.body;
} else {
data.push(req.body);
}
updateData(data);
res.send();
});
app.delete('/api/deployment/:id', function(req, res) {
var data = getData();
var found = data.find(d => d.id === req.params.id);
var foundIndex = !!found ? data.indexOf(found) : -1;
if (foundIndex >= 0) {
data.splice(foundIndex, 1);
}
updateData(data);
res.send();
});
// ---- SERVE APLICATION PATHS ---- //
app.all('*', function (req, res) {
res.status(200).sendFile(`/`, {root: _app_folder});
});
// ---- START UP THE NODE SERVER ----
app.listen(_port, function () {
console.log("Node Express server for " + app.name + " listening on http://localhost:" + _port);
});