forked from apiforfun/theforexapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
99 lines (96 loc) · 3.49 KB
/
api.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
const MongoClient = require('mongodb').MongoClient
const http = require('http')
const url = require('url')
const fs = require('fs')
const DB_URL = 'mongodb://localhost:27017/currency'
http.createServer(function (req, res) {
const q = url.parse(req.url, true)
pathname = url.parse(req.url).pathname
if (pathname.match(/^\/style.css\/$/) || pathname.match(/^\/style.css$/)) {
res.writeHead(200, { 'Content-type': 'text/css' })
const fileContents = fs.readFileSync('./style.css', { encoding: 'utf8' })
res.write(fileContents)
res.end()
} else if (req.url === '/') {
fs.readFile('./home.html', null, function (err, data) {
if (err) {
res.writeHead(404)
res.write('Contents you are looking are Not Found')
res.end()
} else {
res.write(data)
res.end()
}
})
} else if (pathname.match(/^\/api\/latest\/$/) || pathname.match(/^\/api\/latest$/)) {
MongoClient.connect(DB_URL, { useUnifiedTopology: true }, function (err, dbo) {
const db = dbo.db('currency')
const collection = db.collection('prices')
fields = { base: 1, date: 1, _id: 0, rates: 1 }
query = { base: 'EUR' }
if (q.query.base) {
query = { base: q.query.base.toUpperCase() }
}
if (q.query.symbols) {
fields = { base: 1, date: 1, _id: 0 }
symbols = q.query.symbols.split(',')
for (each in symbols) {
symbol = 'rates.' + symbols[each].toUpperCase()
fields[symbol] = 1
}
}
const cursor = collection.find(query, { fields: fields }).sort({ date: -1 }).limit(1).toArray(function (err, result) {
if (err) throw err
dbo.close()
if (result[0]) {
currency_keys = Object.keys(result[0]['rates'])
for (let key of currency_keys) {
result[0]['rates'][key] = parseFloat(result[0]['rates'][key])
}
res.writeHead(200, { 'Content-Type': 'text/json' })
res.write(JSON.stringify(result[0]))
} else {
res.writeHead(400, { 'Content-Type': 'text/json' })
res.write(JSON.stringify({ error: 'Invalid base or symbols' }))
}
res.end()
})
})
} else if (pathname.match(/^\/api\/\d{4}([./-])\d{2}\1\d{2}$/)) {
MongoClient.connect(DB_URL, function (err, dbo) {
const db = dbo.db('currency')
const collection = db.collection('prices')
fields = { base: 1, date: 1, _id: 0, rates: 1 }
query = { base: 'EUR', date: { $lte: req.url.split('/api/')[1].split('?')[0].toString() } }
if (q.query.base) {
query.base = q.query.base.toUpperCase()
}
if (q.query.symbols) {
fields = { base: 1, date: 1, _id: 0 }
symbols = q.query.symbols.split(',')
for (each in symbols) {
symbol = 'rates.' + symbols[each].toUpperCase()
fields[symbol] = 1
}
}
const cursor = collection.find(query, { fields: fields }).sort({ date: -1 }).toArray(function (err, result) {
if (err) {
throw err
}
dbo.close()
if (result[0]) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.write(JSON.stringify(result[0]))
} else {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.write(JSON.stringify({ error: 'Invalid base or symbols' }))
}
res.end()
})
})
} else {
res.writeHead(404)
res.write('Contents you are looking are Not Found')
res.end()
}
}).listen(8080)