Hi Gurus,
I am writing a plugin to invoke the API of another remote web server, the GET body is based on the incoming request.
Now I put the client generation part in a dedicated function and for each new incoming request, this function will be used to generate a new client, may I know how could I set it to a global one so that all the incoming request could re-use it?
Thanks.
func (ta *TLSAuth2) ServeHTTP(w http.ResponseWriter, r *http.Request) {
GetBodydata, _ := json.Marshal(&struct)
ta.someFunction(GetBodydata)
// Continue with next handler
ta.next.ServeHTTP(w, r)
}
func (ta *TLSAuth2) someFunction(data []byte) {
caPath := "./ca.crt"
certPath := "./tls.crt"
keyPath := "./tls.key"
// load CA
caCert, errReadCA := os.ReadFile(caPath)
if errReadCA != nil {
fmt.Fprintf(os.Stdout, "%s [%v] Loading %v with error: %v\n", time.Now().Format(time.RFC3339), ta.name, caPath, errReadCA)
return
}
// load cert and key pair
cert, errLoadCertKey := tls.LoadX509KeyPair(certPath, keyPath)
if errLoadCertKey != nil {
fmt.Fprintf(os.Stdout, "%s [%v] Loading %v and %v with error: %v\n", time.Now().Format(time.RFC3339), ta.name, certPath, keyPath, errLoadCertKey)
return
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
},
},
}
...
}