diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-13 23:55:23 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-13 23:55:23 -0400 |
commit | c4130e200d1282c1637c6f79332593026c87993d (patch) | |
tree | 34a4eab5f17d81706c423940823942eb626495c9 /svc/svc.go | |
parent | d20ad73467c0636edea6133d7bd4b38bbfdefc55 (diff) | |
download | getwtxt-c4130e200d1282c1637c6f79332593026c87993d.tar.gz |
refactored tls listener
Diffstat (limited to 'svc/svc.go')
-rw-r--r-- | svc/svc.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/svc/svc.go b/svc/svc.go index a7786d9..d8b4128 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -1,6 +1,7 @@ package svc // import "github.com/getwtxt/getwtxt/svc" import ( + "crypto/tls" "fmt" "log" "net/http" @@ -34,12 +35,21 @@ func Start() { confObj.Mu.RUnlock() server := newServer(portnum, index) - log.Printf("*** Listening on %v\n", portnum) - log.Printf("*** getwtxt %v Startup finished at %v, took %v\n\n", Vers, time.Now().Format(time.RFC3339), time.Since(before)) if TLS { - errLog("", server.ListenAndServeTLS(TLSCert, TLSKey)) + cert, err := tls.LoadX509KeyPair(TLSCert, TLSKey) + errFatal("", err) + + cfg := &tls.Config{Certificates: []tls.Certificate{cert}} + lstnr, err := tls.Listen("tcp", portnum, cfg) + errFatal("", err) + + server.TLSConfig = cfg + startAnnounce(portnum, before) + errLog("", server.ServeTLS(lstnr, "", "")) + } else { + startAnnounce(portnum, before) errLog("", server.ListenAndServe()) } @@ -50,6 +60,11 @@ func Start() { close(closeLog) } +func startAnnounce(portnum string, before time.Time) { + log.Printf("*** Listening on %v\n", portnum) + log.Printf("*** getwtxt %v Startup finished at %v, took %v\n\n", Vers, time.Now().Format(time.RFC3339), time.Since(before)) +} + func newServer(port string, index *mux.Router) *http.Server { // handlers.CompressHandler gzips all responses. // ipMiddleware passes the request IP along. |