github.com/influx6/npkg@v0.8.8/nhttp/tls.go (about) 1 package nhttp 2 3 import ( 4 "crypto/tls" 5 "os" 6 "os/signal" 7 "syscall" 8 9 "golang.org/x/crypto/acme/autocert" 10 ) 11 12 // LetsEncryptTLS returns a tls.Config instance which retrieves its 13 // its tls certificate from LetsEncrypt service. 14 func LetsEncryptTLS(http2 bool) (*autocert.Manager, *tls.Config) { 15 manager := &autocert.Manager{ 16 Prompt: autocert.AcceptTOS, 17 } 18 19 var tlsConfig tls.Config 20 tlsConfig.GetCertificate = manager.GetCertificate 21 22 if http2 { 23 tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2") 24 } 25 26 return manager, &tlsConfig 27 } 28 29 //LoadTLS loads a tls.Config from a key and cert file path 30 func LoadTLS(cert, key string) (*tls.Config, error) { 31 var config = &tls.Config{} 32 config.Certificates = make([]tls.Certificate, 1) 33 34 c, err := tls.LoadX509KeyPair(cert, key) 35 36 if err != nil { 37 return nil, err 38 } 39 40 config.Certificates[0] = c 41 return config, nil 42 } 43 44 // WaitOnInterrupt will register the needed signals to wait until it recieves 45 // a os interrupt singnal and calls any provided functions later. 46 func WaitOnInterrupt(cbs ...func()) { 47 ch := make(chan os.Signal, 1) 48 signal.Notify(ch, syscall.SIGTERM, os.Interrupt, syscall.SIGSTOP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) 49 50 <-ch 51 52 for _, cb := range cbs { 53 cb() 54 } 55 }