1
18
19
20 package faas_openfaas
21
22 import (
23 "bytes"
24 "encoding/base64"
25 "fmt"
26 "net"
27 "net/http"
28 "scheduler/config"
29 "scheduler/log"
30 "time"
31 )
32
33 var httpTransport *http.Transport
34
35 func init() {
36 httpTransport = &http.Transport{
37 MaxIdleConnsPerHost: 8,
38 MaxIdleConns: 24,
39 IdleConnTimeout: 64,
40 DisableKeepAlives: false,
41 DialContext: (&net.Dialer{
42 Timeout: 30 * time.Second,
43 KeepAlive: 120 * time.Second,
44 }).DialContext,
45 }
46 }
47
48
51
52 func GetApiUrl(host string) string {
53 return fmt.Sprintf("http://%s:%d", host, config.GetOpenFaasListeningPort())
54 }
55
56 func GetApiSystemFunctionsUrl(host string) string {
57 return fmt.Sprintf("%s/system/functions", GetApiUrl(host))
58 }
59
60 func GetApiFunctionUrl(host string, functionName string) string {
61 return fmt.Sprintf("%s/function/%s", GetApiUrl(host), functionName)
62 }
63
64 func GetApiSystemFunctionUrl(host string, functionName string) string {
65 return fmt.Sprintf("%s/system/function/%s", GetApiUrl(host), functionName)
66 }
67
68 func GetApiScaleFunction(host string, functionName string) string {
69 return fmt.Sprintf("%s/system/scale-function/%s", GetApiUrl(host), functionName)
70 }
71
72
75
76 func SetAuthHeader(req *http.Request) {
77 auth := config.OpenFaaSUsername + ":" + config.OpenFaaSPassword
78 req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
79 }
80
81
84
85 type ErrorHttpCannotCreateRequest struct{}
86
87 func (e ErrorHttpCannotCreateRequest) Error() string {
88 return "cannot create http request."
89 }
90
91 func HttpPostJSON(url string, json string) (*http.Response, error) {
92 req, err := http.NewRequest("POST", url, bytes.NewBufferString(json))
93 if err != nil {
94 return nil, ErrorHttpCannotCreateRequest{}
95 }
96
97 req.Header.Set("Content-Type", "application/json")
98 SetAuthHeader(req)
99
100 client := &http.Client{Transport: httpTransport}
101 res, err := client.Do(req)
102 if err != nil {
103 log.Log.Debugf("cannot POST to %s: %s", url, err.Error())
104 }
105
106 return res, err
107 }
108
109 func HttpGet(url string) (*http.Response, error) {
110 req, err := http.NewRequest("GET", url, nil)
111 if err != nil {
112 return nil, ErrorHttpCannotCreateRequest{}
113 }
114
115 SetAuthHeader(req)
116
117 client := &http.Client{Transport: httpTransport}
118 res, err := client.Do(req)
119 if err != nil {
120 log.Log.Debugf("cannot GET to %s: %s", url, err.Error())
121 }
122
123 return res, err
124 }
125
126 func HttpPost(url string, payload []byte, contentType string) (*http.Response, error) {
127 req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
128 if err != nil {
129 return nil, ErrorHttpCannotCreateRequest{}
130 }
131
132 if contentType != "" {
133 req.Header.Set("Content-Type", contentType)
134 }
135
136 SetAuthHeader(req)
137
138 client := &http.Client{Transport: httpTransport}
139 res, err := client.Do(req)
140 if err != nil {
141 log.Log.Debugf("cannot POST to %s: %s", url, err.Error())
142 }
143
144 return res, err
145 }
146
View as plain text