-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.js
151 lines (149 loc) · 4.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
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
"use strict";
var restify = require("restify")
var request = require("request")
var CoinAPI = require("./lib/CoinAPI")
var self = null
module.exports = class Server {
constructor(address, port, trackingID) {
self = this
self.address = address
self.port = port
self.trackingID = trackingID
this.api = new CoinAPI()
self.metrics = {
"startTime": 0,
"lastSuccessfulRefresh": 0,
"coins": 0
}
self.app = restify.createServer({
"name": "CoinmarketCap API Server"
})
/* Register quit events so the server quits when it's supposed to */
var events = ["SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT",
"SIGBUS", "SIGFPE", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGTERM"
]
events.forEach(function (event) {
process.on(event, function() {
self.stop(event)
})
})
/* Rate limiting */
self.app.use(restify.throttle({
burst: 100,
rate: 50, // requests/second
ip: true
}))
/* Serve index file */
self.app.get("/", function (req, res, next) {
next()
},
restify.serveStatic({
directory: __dirname,
file: "index.html"
}))
/* Serve API */
this.app.get(/\/api\/*/, self.handleAPIRequest)
/* Metrics */
self.app.get("/metrics", self.handleMetricsRequest)
/* Schedule API refresh */
self.APIRefreshHandler = setInterval(self.handleAPIRefresh, 300000)
/* Initial refresh */
self.handleAPIRefresh()
}
start() {
self.app.listen(self.port, self.address, function() {
// Server started
self.metrics.startTime = Date(Date.now())
self.log("Server started.")
})
}
trackEvent(category, action, value) {
if (!self.trackingID) return
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
request.post("https://www.google-analytics.com/collect").form({
v: 1,
t: "event",
tid: self.trackingID,
uid: "server",
ec: category,
ea: action,
ev: value
})
}
log(msg, isSevere) {
console.log("%s: %s", Date(Date.now()), msg)
}
handleAPIRequest(req, res, next) {
var coin = null
var property = null
var result = null
res.setHeader("Content-Type", "application/json")
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "X-Requested-With")
var query = req.path().substring(5).toLowerCase()
self.trackEvent("Request", query)
if (query == "") {
res.send({"error": "This isn't an endpoint!"})
return
}
if (query == "all") {
res.send(self.api.coins)
return
}
var slashLocation = query.indexOf("/")
var queryContainsProperty = (slashLocation > -1) // i.e. /api/btc/market_cap
if (queryContainsProperty) {
property = query.substring(slashLocation + 1)
coin = query.substring(0, slashLocation)
}
else {
coin = query
}
coin = self.api.getCoin(coin)
if (coin) {
if (property) {
if (coin[property]) {
result = coin[property]
}
else {
result = {"error": "Invalid property requested"}
}
}
else {
result = coin
}
}
else {
result = {"error": "Requested coin does not exist or has not been updated yet."}
}
res.send(result)
}
handleAPIRefresh() {
self.api.refresh(function (numCoinsUpdated) {
var success = (numCoinsUpdated == 100)
var isSevere = !success
self.log("Updated " + numCoinsUpdated + " coins. Success: " + success, isSevere)
self.trackEvent("Update", success ? "Success" : "Failure", numCoinsUpdated)
if (success) {
self.metrics.lastSuccessfulRefresh = Date(Date.now())
self.metrics.coins = numCoinsUpdated
}
}, function(error, statusCode) {
console.log("Error while refreshing API", error, "with status code", statusCode)
self.trackEvent("Update", "Error", statusCode)
})
}
handleMetricsRequest(req, res, next) {
res.setHeader("Content-Type", "application/json")
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "X-Requested-With")
res.send(self.metrics)
next()
}
stop(event) {
// Server stopped
clearInterval(self.APIRefreshHandler)
self.log("Server stopped with event: " + event + ". Last metrics: " + JSON.stringify(this.metrics))
process.exit(1)
}
}