1
0

server_tls.go 807 B

123456789101112131415161718192021222324252627282930313233343536
  1. package cert
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "sync/atomic"
  6. cSettings "github.com/uozi-tech/cosy/settings"
  7. )
  8. var tlsCert atomic.Value
  9. // LoadServerTLSCertificate loads the TLS certificate
  10. func LoadServerTLSCertificate() error {
  11. return ReloadServerTLSCertificate()
  12. }
  13. // ReloadServerTLSCertificate reloads the TLS certificate
  14. func ReloadServerTLSCertificate() error {
  15. newCert, err := tls.LoadX509KeyPair(cSettings.ServerSettings.SSLCert, cSettings.ServerSettings.SSLKey)
  16. if err != nil {
  17. return err
  18. }
  19. tlsCert.Store(&newCert)
  20. return nil
  21. }
  22. // GetServerTLSCertificate returns the current TLS certificate
  23. func GetServerTLSCertificate() (*tls.Certificate, error) {
  24. cert, ok := tlsCert.Load().(*tls.Certificate)
  25. if !ok {
  26. return nil, errors.New("no certificate available")
  27. }
  28. return cert, nil
  29. }