艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

golang post带证书请求

func getTLSConfig() (*tls.Config, error) {
    if _tlsConfig != nil {
        return _tlsConfig, nil
    }

    // load cert
    cert, err := tls.LoadX509KeyPair("apiclient_cert.pem", "apiclient_key.pem")
    if err != nil {
        log.Println("load wechat keys fail", err)
        return nil, err
    }

    // load root ca
    caData, err := ioutil.ReadFile("cacert.pem")
    if err != nil {
        log.Println("read wechat ca fail", err)
        return nil, err
    }
    pool := x509.NewCertPool()
    pool.AppendCertsFromPEM(caData)

    _tlsConfig = &tls.Config{
        Certificates: []tls.Certificate{cert},
        RootCAs:      pool,
    }
    return _tlsConfig, nil
}