server_tls.go 784 B

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