-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_client.go
81 lines (66 loc) · 2 KB
/
http_client.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
package main
import (
"crypto/tls"
"encoding/json"
"log"
"net/http"
)
var (
httpClient *http.Client
)
const (
MaxIdleConnections int = 2
ApiEndpoint string = "https://api.example.com/v2/info"
)
type cloudInfo struct {
Name string `json:"name"`
Support string `json:"support"`
Build string `json:"build"`
Version float32 `json:"version"`
Description string `json:"description"`
AuthEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
ApiVersion string `json:"api_version"`
LoggregatorEndpoint string `json:"loggregator_endpoint"`
RoutingEndpoint string `json:"routing_endpoint"`
LoggingEndpoint string `json:"logging_endpoint"`
DopplerEndpoint string `json:"doppler_logging_endpoint"`
MinCliVersion string `json:"min_cli_version"`
MinRecommendedCliVersion string `json:"min_recommended_cli_version"`
}
func init() {
httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: MaxIdleConnections,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
func main() {
http.HandleFunc("/", handleInfo)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleInfo(w http.ResponseWriter, r *http.Request) {
data, err := getInfo()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(data)
}
}
func getInfo() (cloudInfo, error) {
log.Println("Sending request")
response, err := httpClient.Get(ApiEndpoint)
if err != nil && response == nil {
log.Printf("Error sending request: %+v", err)
return cloudInfo{}, err
}
log.Println("Got response")
defer response.Body.Close()
var info cloudInfo
if err := json.NewDecoder(response.Body).Decode(&info); err != nil {
return cloudInfo{}, err
}
return info, nil
}