-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (36 loc) · 849 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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"strings"
)
func main(){
go GetExchangeRatesFromPrivatBankAsJSON()
http.HandleFunc("/rate/", handle)
fmt.Println("Listening on port 8080...")
http.ListenAndServe(":8000", nil)
}
func handle(w http.ResponseWriter, r *http.Request){
format := strings.TrimPrefix(r.URL.String(), "/rate/")
fmt.Println(format)
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
body, _ := json.Marshal(value.Load())
json.NewEncoder(w).Encode(body)
break
case "xml":
w.Header().Set("Content-Type", "application/xml")
body, _ := xml.Marshal(value.Load())
xml.NewEncoder(w).Encode(body)
break
case "text":
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, value.Load())
break
default:
fmt.Fprint(w, "wrong format")
}
}