-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_sabae_cc.js
241 lines (226 loc) · 8.95 KB
/
app_sabae_cc.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const PORT = 8003
const http = require('http')
const urllib = require('url')
const fetch = require('node-fetch')
const fs = require('fs')
const util = require('./util.js')
const PROXY_CONTENT_TYPE = {
".txt": "text/plain; charset=utf-8",
".js": "application/json; charset=utf-8",
".json": "application/json; charset=utf-8",
".csv": "text/csv; charset=utf-8",
}
const globalsafe = require('./globalsafe.js')
const taiwanmask = require('./taiwanmask.js')
const covid19tokyo = require('./covid19tokyo.js')
const covid19fukui = require('./covid19fukui.js')
const covid19tokushima = require('./covid19tokushima.js')
const covid19ishikawa = require('./covid19ishikawa.js')
const covid19japan = require('./covid19japan.js')
//const covid19japan_summary = require('./covid19japan_summary.js')
const covid19kyoto = require('./covid19kyoto.js')
const covid19cio = require('./covid19cio.js')
const bedforinfection = require('./bedforinfection.js')
const googlespreadsheet = require('./googlespreadsheet.js')
const countLog = function(name) {
name = name.replace(/[\/|\¥|\?]/g, '-')
console.log(name)
const path = "countlog/" + name + "/"
const fn = path + util.getYMDH() + ".txt"
let n = 0
try {
n = parseInt(fs.readFileSync(fn, 'utf-8'))
} catch (e) {
}
n++
try {
fs.writeFileSync(fn, "" + n, 'utf-8')
} catch (e) {
fs.mkdirSync(path, 0744)
fs.writeFileSync(fn, "" + n, 'utf-8')
}
}
const server = http.createServer()
server.on('request', async function(req, res) {
if (req.url == "/favicon.ico") {
res.end()
return
}
if (req.url.startsWith("/img/") || req.url.endsWith(".css")) {
serveStatic(res, req.url)
return
}
countLog(req.url)
if (req.url == '/api/taiwan_masks.json') {
const data = await taiwanmask.getMasksDataTaiwan()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/globalsafe.json')) {
const params = urllib.parse(req.url, true)
//console.log(params.query)
const key = params.query.key || ""
let limit = parseInt(params.query.limit) || 30
if (limit > 1000)
limit = 1000
//console.log(key, limit)
const data = globalsafe.getGlobalSafeDataByKeyword(key, limit)
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19tokyo.json')) {
const data = await covid19tokyo.getCovid19DataSummary()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19tokyo.txt')) {
const data = await covid19tokyo.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19fukui.json')) {
const data = await covid19fukui.getCovid19DataJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19fukui.txt')) {
const data = await covid19fukui.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19tokushima.json')) {
const data = await covid19tokushima.getCovid19DataJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19tokushima.txt')) {
const data = await covid19tokushima.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19ishikawa.json')) {
const data = await covid19ishikawa.getCovid19DataJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19ishikawa.txt')) {
const data = await covid19ishikawa.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19kyoto.json')) {
const data = await covid19kyoto.getCovid19DataJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19kyoto.txt')) {
const data = await covid19kyoto.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19japan.json')) {
const type = urllib.parse(req.url, true).query.type
const data = await covid19japan.getCovid19DataJSON(type)
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/covid19japan.txt')) {
const data = await covid19japan.getCovid19DataSummaryForIchigoJam()
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
} else if (req.url.startsWith('/api/covid19japan_summary.')) {
//const data = await covid19japan_summary.getCovid19DataJSON()
const data = await covid19japan.getCovid19DataJSON()
const nbed = await bedforinfection.getSummaryJSON()
data.nbedforinfection = nbed.total.sumi
delete data.area
delete data.description
if (req.url.endsWith(".json")) {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
} else if (req.url.endsWith(".txt")) {
res.end(util.simplejson2txt(data))
}
return
} else if (req.url.startsWith('/api/covid19cio.json')) {
const data = await covid19cio.getCovid19DataJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/bedforinfection.json')) {
const data = await bedforinfection.getJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/bedforinfection_summary.json')) {
const data = await bedforinfection.getSummaryJSON()
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/api/googlespreadsheet.json')) {
const params = urllib.parse(req.url, true)
const key = params.query.key
const data = await googlespreadsheet.fetchCSVfromGoogleSpreadSheet(key)
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*' })
res.end(JSON.stringify(data))
return
} else if (req.url.startsWith('/proxy/')) {
//countLog('proxy')
const params = urllib.parse(req.url, true)
const url = params.query.url
//console.log(url)
if (url) {
const n = url.lastIndexOf('.')
let ext = ".txt"
if (n >= 0) {
ext = url.substring(n)
}
const data = await (await fetch(url)).text()
let ctype = PROXY_CONTENT_TYPE[ext]
if (!ctype) {
ctype = PROXY_CONTENT_TYPE[".txt"]
}
res.writeHead(200, { 'Content-Type': ctype, 'Access-Control-Allow-Origin': '*' })
res.end(data)
return
}
}
console.log(req.url)
res.end()
})
server.listen(PORT)
const CONTENT_TYPE = {
'html' : 'text/html; charset=utf-8',
'png' : 'image/png',
'gif' : 'image/gif',
'jpg' : 'image/jpeg',
'txt' : 'text/plain',
'js' : 'text/javascript',
'json' : 'application/json',
'jsonld' : 'application/ld+json',
'csv' : 'text/csv',
'css' : 'text/css',
'pdf' : 'application/pdf',
'ico' : 'image/vnd.microsoft.icon',
}
function serveStatic(res, fn) {
fn = 'static' + fn
if (fn.indexOf('..') >= 0) {
return
}
if (fn.endsWith('/'))
fn += "index.html"
const ext = fn.substring(fn.lastIndexOf('.') + 1)
let type = CONTENT_TYPE[ext]
if (!type)
type = 'text/plain'
try {
const b = fs.readFileSync(fn)
res.writeHead(200, { 'Content-Type' : type })
res.write(b)
} catch (e) {
res.writeHead(404)
}
res.end()
}