From 59c18fdaff2ae8e875be7b2ddfd558e0f4ec4b9d Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Sun, 29 Mar 2020 22:23:27 -0500 Subject: [PATCH] Add UseTLS config param for non-encrypted debugging --- factorio-site.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/factorio-site.go b/factorio-site.go index d2e49b0..e5436ac 100644 --- a/factorio-site.go +++ b/factorio-site.go @@ -14,6 +14,7 @@ import ( ) type config struct { + UseTLS bool `json:"useTLS"` Servers []server `json:"servers"` } @@ -111,22 +112,29 @@ func main() { t.Execute(w, data) }) - certManager := autocert.Manager{ - Prompt: autocert.AcceptTOS, - HostPolicy: autocert.HostWhitelist("factorio.blackolivepineapple.pizza"), // TODO: add config - Cache: autocert.DirCache("certs"), + if config.UseTLS { + + certManager := autocert.Manager{ + Prompt: autocert.AcceptTOS, + HostPolicy: autocert.HostWhitelist("factorio.blackolivepineapple.pizza"), // TODO: add config + Cache: autocert.DirCache("certs"), + } + + srv := &http.Server{ + Addr: ":https", + TLSConfig: &tls.Config{ + GetCertificate: certManager.GetCertificate, + }, + } + + go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt + + fmt.Println("Serving...") + srv.ListenAndServeTLS("", "") // Key/cert come from srv.TLSConfig + + } else { // Debug + fmt.Println("Serving...") + http.ListenAndServe(":8080", nil) // TODO: pass as config value } - srv := &http.Server{ - Addr: ":https", - TLSConfig: &tls.Config{ - GetCertificate: certManager.GetCertificate, - }, - } - - go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt - - fmt.Println("Serving...") - srv.ListenAndServeTLS("", "") // Key/cert come from srv.TLSConfig - }