-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (103 loc) · 3.39 KB
/
server.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
/**
Validation server for json files used by the new EMU speech database management system
@author Raphael Winkelmann
*/
var myHttp = require('http'),
https = require('https'),
path = require('path'),
fs = require('fs'),
jsonlint = require('jsonlint');
var JSONLint = require('json-lint');
var url = require("url");
var tv4 = require('tv4');
// global vars
var port = 9263;
var annotSchemaData;
// get annotSchema
var annotSchemaURL = 'https://raw.githubusercontent.com/IPS-LMU/EMU-webApp/master/app/schemaFiles/annotationFileSchema.json';
https.get(annotSchemaURL, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
annotSchemaData = JSON.parse(body);
console.log("Finished loading annotSchemaData");
});
}).on('error', function(e) {
console.log("Got error: ", e);
});
myHttp.createServer(function (request, response) {
if (request.method === 'GET') {
console.log('not processing GET');
}
if (request.method === 'POST') {
// the body of the POST is JSON payload.
var queryData = url.parse(request.url, true).query;
// console.log(request.url);
var data = '';
request.addListener('data', function (chunk) {
data += chunk;
});
request.addListener('end', function () {
var lint = JSONLint(data);
var mess = {};
if (lint.error) {
mess.type = 'ERROR';
mess.from = 'JSLINT (means badly formated json)';
mess.ERROR_MESSAGE = lint.error;
mess.ERROR_LINE = lint.line;
mess.ERROR_CHARACTER = lint.character;
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end(JSON.stringify(mess, null, 2));
} else {
if (request.url === '/_DBconfig') {
// console.log('Validating _DBconfig file')
// console.log(path2schemas + 'globalDBschema.json')
// fs.readFile(path2schemas + 'globalDBschema.json', 'utf8', function (globErr, globData) {
// var validationErrs = v.validate(JSON.parse(data), JSON.parse(globData)).errors;
// if (validationErrs.length === 0) {
// mess.type = 'SUCCESS';
// response.writeHead(200, {
// 'content-type': 'text/plain'
// });
// response.end(JSON.stringify(mess, null, 2));
// } else {
// // console.log('VALIDATION ERRORS:');
// // console.log(validationErrs);
// mess.type = 'ERROR';
// mess.from = 'JSONSCHEMA (means does not comply to schema)';
// mess.ERRORS = validationErrs;
// response.writeHead(200, {
// 'content-type': 'text/plain'
// });
// response.end(JSON.stringify(mess, null, 2));
// }
// });
} else if (request.url === '/_annot') {
// console.log('Validating _annot file');
// console.log(path2schemas + 'annotationFileSchema.json');
var validRes = tv4.validate(JSON.parse(data), annotSchemaData);
if (validRes) {
mess.type = 'SUCCESS';
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end(JSON.stringify(mess, null, 2));
} else {
mess.type = 'ERROR';
mess.from = 'JSONSCHEMA (means does not comply to schema)';
mess.ERRORS = tv4.error;
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end(JSON.stringify(mess, null, 2));
}
}
}
});
}
}).listen(port);
console.log("Server Running @ http://localhost:" + port);