-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (40 loc) · 858 Bytes
/
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
package main
import (
"fmt"
"flag"
"net/http"
"encoding/json"
)
type ExchangeRate struct {
Currency string
Buying string
Selling string
}
const (
defautBank = "cbe"
)
func main() {
var port string
flag.StringVar(&port, "port", "8888", "http port to run the server")
flag.Parse()
var exchangeList []ExchangeRate
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
bank := req.URL.Query().Get("bank")
if bank == "" {
bank = defautBank
}
fmt.Println("Selected Bank", bank)
switch bank {
case "cbe":
exchangeList, _ = GetCBERate()
js, _ := json.Marshal(exchangeList)
fmt.Fprintf(w, string(js))
// TODO: Add more banks here
default:
js, _ := json.Marshal([]ExchangeRate{})
fmt.Fprintf(w, string(js))
}
})
fmt.Println("Running at", port)
http.ListenAndServe(":" + port, nil)
}