| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
type CertStorage struct { |
| 9 |
certs sync.Map |
| 10 |
} |
| 11 |
|
| 12 |
func (tcs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) { |
| 13 |
var cert tls.Certificate |
| 14 |
icert, ok := tcs.certs.Load(hostname) |
| 15 |
if ok { |
| 16 |
cert = icert.(tls.Certificate) |
| 17 |
} else { |
| 18 |
certp, err := gen() |
| 19 |
if err != nil { |
| 20 |
return nil, err |
| 21 |
} |
| 22 |
// store as concrete implementation |
| 23 |
cert = *certp |
| 24 |
tcs.certs.Store(hostname, cert) |
| 25 |
} |
| 26 |
return &cert, nil |
| 27 |
} |
| 28 |
|
| 29 |
func NewCertStorage() *CertStorage { |
| 30 |
tcs := &CertStorage{} |
| 31 |
tcs.certs = sync.Map{} |
| 32 |
|
| 33 |
return tcs |
| 34 |
} |