Added AutoCertDomain param

This commit is contained in:
Alexei Anoshenko 2024-06-11 15:03:13 +03:00
parent 658142d3f1
commit a471df2471
2 changed files with 20 additions and 6 deletions

View File

@ -15,6 +15,8 @@ import (
"strconv"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
)
//go:embed app_socket.js
@ -363,20 +365,19 @@ func StartApp(addr string, createContentFunc func(Session) SessionContent, param
apps = append(apps, app)
redirectAddr := ""
https := params.AutoCertDomain != "" || (params.CertFile != "" && params.KeyFile != "")
if index := strings.IndexRune(addr, ':'); index >= 0 {
redirectAddr = addr[:index] + ":80"
} else {
redirectAddr = addr + ":80"
if params.CertFile != "" && params.KeyFile != "" {
if https {
addr += ":443"
} else {
addr += ":80"
}
}
app.server = &http.Server{Addr: addr}
http.Handle("/", app)
serverRun := func(err error) {
if err != nil {
if err == http.ErrServerClosed {
@ -387,7 +388,7 @@ func StartApp(addr string, createContentFunc func(Session) SessionContent, param
}
}
if params.CertFile != "" && params.KeyFile != "" {
if https {
if params.Redirect80 {
redirectTLS := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+addr+r.RequestURI, http.StatusMovedPermanently)
@ -397,8 +398,19 @@ func StartApp(addr string, createContentFunc func(Session) SessionContent, param
serverRun(http.ListenAndServe(redirectAddr, http.HandlerFunc(redirectTLS)))
}()
}
serverRun(app.server.ListenAndServeTLS(params.CertFile, params.KeyFile))
if params.AutoCertDomain != "" {
mux := http.NewServeMux()
mux.Handle("/", app)
serverRun(http.Serve(autocert.NewListener(params.AutoCertDomain), mux))
} else {
app.server = &http.Server{Addr: addr}
http.Handle("/", app)
serverRun(app.server.ListenAndServeTLS(params.CertFile, params.KeyFile))
}
} else {
app.server = &http.Server{Addr: addr}
http.Handle("/", app)
serverRun(app.server.ListenAndServe())
}
}

View File

@ -38,6 +38,8 @@ type AppParams struct {
// of the server's certificate, any intermediates, and the CA's certificate.
CertFile string
AutoCertDomain string
// KeyFile - path of a private key for the server must be provided
// if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
KeyFile string