This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (146 loc) · 3.56 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/umsatz/currency-exchange/ecb"
rateHTTP "github.com/umsatz/currency-exchange/http"
)
var (
// EUR is not present because all exchange rates are a reference to the EUR
desiredCurrencies = map[string]struct{}{
"USD": struct{}{},
"GBP": struct{}{},
// "DKK": struct{}{},
// "JPY": struct{}{},
// "BGN": struct{}{},
// "CZK": struct{}{},
// "HUF": struct{}{},
// "LTL": struct{}{},
// "PLN": struct{}{},
// "RON": struct{}{},
// "SEK": struct{}{},
// "CHF": struct{}{},
// "NOK": struct{}{},
// "HRK": struct{}{},
// "RUB": struct{}{},
// "TRY": struct{}{},
// "AUD": struct{}{},
// "BRL": struct{}{},
// "CAD": struct{}{},
// "CNY": struct{}{},
// "HKD": struct{}{},
// "IDR": struct{}{},
// "ILS": struct{}{},
// "INR": struct{}{},
// "KRW": struct{}{},
// "MXN": struct{}{},
// "MYR": struct{}{},
// "NZD": struct{}{},
// "PHP": struct{}{},
// "SGD": struct{}{},
// "THB": struct{}{},
// "ZAR": struct{}{},
}
// last 90 days are available at https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml
eurHistURL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml"
)
func filterExchangeRates(c ecb.Cube, currencies map[string]struct{}) []ecb.Exchange {
var rates []ecb.Exchange
for _, ex := range c.Exchanges {
if _, ok := currencies[ex.Currency]; ok {
rates = append(rates, ex)
}
}
return rates
}
func updateExchangeRates(data io.Reader) ([]ecb.Cube, error) {
cubes, err := ecb.Parse(data)
if err != nil {
return nil, err
}
for i, c := range cubes {
cubes[i].Exchanges = filterExchangeRates(c, desiredCurrencies)
}
return cubes, nil
}
func updateExchangeRatesCache(cache *rateCache) {
resp, err := http.Get(eurHistURL)
if err != nil {
log.Printf("Unable to download exchange rates: %q", err)
return
}
if resp.StatusCode != http.StatusOK {
log.Printf("HTTP request returned %q", resp.Status)
return
}
defer resp.Body.Close()
cubes, err := updateExchangeRates(resp.Body)
if err != nil {
log.Printf("Failed to update exchange rates: %v", err)
}
cache.update(cubes)
}
func populateExchangeRateCache(file string) (*rateCache, error) {
cache := rateCache{
exchangeRates: map[string][]ecb.Exchange{},
}
f, err := os.Open(file)
if err != nil {
return &cache, err
}
cs, err := updateExchangeRates(f)
if err != nil {
return &cache, err
}
cache.update(cs)
return &cache, nil
}
func logHandler(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
log.Printf(
"%s\t%s\t%s\t%s\t%s",
r.RemoteAddr,
time.Now().Format("2006-01-02T15:04:05 -0700"),
r.Method,
r.RequestURI,
time.Since(start),
)
}
}
// Set by make file on build
var (
Version string
Commit string
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
var (
httpAddress = flag.String("http.addr", ":8080", "HTTP listen address")
historicData = flag.String("historic.data", "", "path to data directory")
printVersion = flag.Bool("version", false, "print version and exit")
)
flag.Parse()
if *printVersion {
fmt.Printf("%s", Version)
os.Exit(0)
}
cache, err := populateExchangeRateCache(*historicData)
if err != nil {
log.Fatalf("Unable to populate cache: %v", err)
}
go func() {
for {
time.Sleep(6 * time.Hour)
updateExchangeRatesCache(cache)
}
}()
log.Printf("listening on %v", *httpAddress)
log.Fatal(http.ListenAndServe(*httpAddress, logHandler(http.Handler(rateHTTP.Handler(cache)))))
}