github.com/btccom/go-micro/v2@v2.9.3/api/server/acme/autocert/autocert.go (about)

     1  // Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert
     2  // This provider does not take any config.
     3  package autocert
     4  
     5  import (
     6  	"crypto/tls"
     7  	"net"
     8  	"os"
     9  
    10  	"github.com/btccom/go-micro/v2/api/server/acme"
    11  	"github.com/btccom/go-micro/v2/logger"
    12  	"golang.org/x/crypto/acme/autocert"
    13  )
    14  
    15  // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
    16  type autocertProvider struct{}
    17  
    18  // Listen implements acme.Provider
    19  func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
    20  	return autocert.NewListener(hosts...), nil
    21  }
    22  
    23  // TLSConfig returns a new tls config
    24  func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
    25  	// create a new manager
    26  	m := &autocert.Manager{
    27  		Prompt: autocert.AcceptTOS,
    28  	}
    29  	if len(hosts) > 0 {
    30  		m.HostPolicy = autocert.HostWhitelist(hosts...)
    31  	}
    32  	dir := cacheDir()
    33  	if err := os.MkdirAll(dir, 0700); err != nil {
    34  		if logger.V(logger.InfoLevel, logger.DefaultLogger) {
    35  			logger.Infof("warning: autocert not using a cache: %v", err)
    36  		}
    37  	} else {
    38  		m.Cache = autocert.DirCache(dir)
    39  	}
    40  	return m.TLSConfig(), nil
    41  }
    42  
    43  // New returns an autocert acme.Provider
    44  func NewProvider() acme.Provider {
    45  	return &autocertProvider{}
    46  }