forked from usgin/dynamic-json-editor-schema-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeServer-d3-editor.js
184 lines (146 loc) · 5.31 KB
/
nodeServer-d3-editor.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
115
116
117
118
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* JSON Editor V1
G. Hudman
April 1, 2016
*/
var http = require('http'),
fs = require("fs"),
express = require('express'),
app = express(),
request = require('request'),
bodyParser = require('body-parser'),
Path = '/home/user/yourpath',
md_api = require(Path + "/js/mdpackage.js");
var htmObj = "",
default_json = "f2-min3.json";
app.use('/img', express.static(__dirname + '/public/img'));
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/jsonSchemas', express.static(__dirname + '/public/jsonSchemas'));
app.get('/' , function(req,res) {
res.sendFile(Path+'/public/treeEditor.htm');
} );
app.get('/jsonDictionary.json' , function(req,res) {
res.sendFile(Path+'/public/jsonDictionary.json');
} );
app.get('/md_data' , function(req,res) {
res.sendFile(Path+'/md_example.json');
});
app.get('/publish/faq' , function(req,res) {
res.sendFile(Path+'/public/faq.htm');
});
app.get('/md_get_package', function(req,res) {
var pid = req.query.pid;
var hurl = 'http://test.geothermaldata.org/api/3/action/package_show?id='+pid;
request(hurl, function (error, response, body) {
if (!error && response.statusCode == 200) {
mdbody = JSON.parse(body);
obj = mdbody.result;
res.send(obj);
} else {
res.send('error: ' + error);
}
});
});
app.get('/md_extract', function(req,res) {
/// this route receives a request from the editor page
/// Three step process - get the full package, strip the md_package, then translate to an editor page
// the d3 json wants a url to pull in the formated json from
var rootObj = 'empty',
rootType,
rootKey;
var pid = req.query.pid;
var schemafile = req.query.schema;
(typeof(schemafile) == "undefined") ? schemafile = "f2-min3.json" : schemafile = schemafile;
var d3file = JSON.parse(fs.readFileSync(Path+'/public/jsonSchemas/' + schemafile, 'utf8'));
for (var key in d3file) {
console.log('Schema key ' + key + ' value ' + d3file[key] );
if ( key == 'root' ) {
rootObj = d3file['root'];
rootType = d3file['rootType'];
rootKey = d3file['rootKey'];
break;
}
}
var hurl = 'http://test.geothermaldata.org/api/3/action/package_show?id='+pid;
console.log("here " + hurl);
request(hurl, function (error, response, body) {
if (!error && response.statusCode == 200) {
mdbody = JSON.parse(body);
obj = mdbody.result;
var doMDP = false;
for (key in obj) {
if ( key == rootObj ) {
var findRoot = obj[key];
if ( rootType == "array") {
for (i=0; i < findRoot.length; i ++ ) {
if ( findRoot[i][rootKey.keyname] == rootKey.keyvalue ) {
var luckyGuess = findRoot[i][rootKey.valuename];
console.log('lucky guess ' + rootKey.valuename + ' ' + JSON.stringify(luckyGuess));
}
}
}
// console.log(" Im a genius " + key);
break;
}
}
if ( typeof(obj.extras) !== "undefined" ) {
for (i = 0; i < obj.extras.length; i++) {
if (obj.extras[i].key === 'md_package') {
var md_pkg = JSON.parse(obj.extras[i].value);
doMDP = true;
var d3proc = md_api.any(d3file,md_pkg);
res.send(d3proc);
}
}
}
if (!doMDP) {
var noPack = { "status" : "No md_package!" };
res.send(noPack);
}
} else {
res.send('error: ' + error);
}
});
});
app.use(bodyParser.json({"strict":"false"}))
app.use(function (req, res, next) {
next()
})
app.post('/save_fullpackage' , function(req,res) {
// sample jsons for testing
var stuff = {"result": "Node start save .. "};
var sData = JSON.stringify(req.body);
console.log(' saving: ' + JSON.stringify(req.body) );
var hurl = 'http://test.geothermaldata.org/api/3/action/package_update';
// Requires authorization key - contact usgin admin
var options = {url: hurl,
method: "POST",
json: true,
body : req.body,
headers: {
'Authorization' :'*',
'sendImmediately': true,
dataType: "json",
contentType: "application/json"
}
};
var rtnRes = function(err, httpResponse, body) {
if (err) {
stuff = {"result": "Save error : " + err};
console.error('upload failed:', err);
} else {
stuff = {"result": "Upload response: " + body};
console.log('Upload response ' + httpResponse + ' body:', body);
// res.send(stuff);
}
}
request.post(options, rtnRes);
res.send(stuff);
});
app.get('/data.json' , function(req,res) {
res.sendFile(Path+'/data.json');
} );
app.set('port', process.env.PORT || 8001);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});