-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
123 lines (100 loc) · 2.71 KB
/
index.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
var fs=require('fs');
var data=fs.readFileSync('cms_release_info.json');
var years=JSON.parse(data);
var data2=fs.readFileSync('run_ranges.json');
var run_eras=JSON.parse(data2);
const express = require("express");
var favicon = require('serve-favicon');
var path = require('path');
const app = express();
const cors=require('cors');
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App started on PORT ${PORT}`);
});
app.use(express.static('public'));
app.use(cors());
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.get('/',welcome);
function welcome(request,response)
{
let hostname=request.get('Host')
console.log(request.get('Host'));
var reply={
usage: "Add a path, available paths: http://"+hostname+"/years and http://"+hostname+"/runeras",
years: "use: /years/ to get all year info or: /years/<key> for a specific value",
runeras: "use: /runeras/ to get all run era info or: /runeras/<key> for a specific value",
queries: "Pass a query with: ?<akey>=<avalue>",
example: "http://"+hostname+"/runeras/run_era?year=2015",
output: "For year info, output=plain returns single values without square brackets"
}
response.send(reply);
}
app.get('/years/:mykey?',searchYear);
function searchYear(request,response)
{
var key=request.params.mykey;
var filters=request.query;
var filtered_years = years;
var output_value;
for (var reqkey in filters) {
if (reqkey == 'output')
{
output_value = filters[reqkey];
}
else
{
filtered_years = filtered_years.filter(a_year => a_year[reqkey] == filters[reqkey]);
}
}
var reply;
if (key)
{
// note that year.key won't work
let keyvalues = filtered_years.map(year => year[key]);
reply = keyvalues;
}
else
{
reply=filtered_years;
}
console.log(reply);
// There are reasons to do this (i.e. to get CMSSW_5_3_32 instead of [ "CMSSW_5_3_32" ])
// but this is probably not a good idea in general
// adding a query parameter for output format: output=plain returns reply without square brackets
if (reply.length == 1 && output_value == 'plain')
{
if (typeof reply[0] == 'string')
{
reply = String(reply[0]);
}
else
{
reply = reply[0];
}
}
response.send(reply);
}
app.get('/runeras/:mykey?',searchEra);
function searchEra(request,response)
{
var key=request.params.mykey;
var filters=request.query;
var filtered_eras = run_eras;
for (var reqkey in filters) {
filtered_eras = filtered_eras.filter(an_era => an_era[reqkey] == filters[reqkey]);
}
var reply;
if (key)
{
// note that era.key won't work
let keyvalues = filtered_eras.map(era => era[key]);
reply = keyvalues;
}
else
{
reply=filtered_eras;
}
console.log(reply);
response.send(reply);
}