-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
51 lines (45 loc) · 1.23 KB
/
server.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
package main
import (
"context"
"encoding/json"
"github.com/gorilla/mux"
repositories "github.com/gvillela7/price/Repositories"
"github.com/gvillela7/price/config"
"log"
"net/http"
"time"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/cotacao", GetPrice).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", router))
}
func GetPrice(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
priceHost := config.ViperEnvVariable("PRICE_HOST")
req, err := http.NewRequest("GET", priceHost+"/json/last/USD-BRL", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req = req.WithContext(ctx)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error: maximum time exceeded -> %v\n", err)
}
defer resp.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, bid, err := repositories.Save(resp)
if err != nil {
log.Printf("Error: Unable to save to database -> %v\n", err)
}
w.Header().Add("content-type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
"bid": bid, "statusCode": "ok",
})
}