mirror of https://github.com/anoshenko/rui.git
				
				
				
			Added AutoCertDomain param
This commit is contained in:
		
							parent
							
								
									658142d3f1
								
							
						
					
					
						commit
						a471df2471
					
				
							
								
								
									
										24
									
								
								appServer.go
								
								
								
								
							
							
						
						
									
										24
									
								
								appServer.go
								
								
								
								
							| 
						 | 
					@ -15,6 +15,8 @@ import (
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/acme/autocert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:embed app_socket.js
 | 
					//go:embed app_socket.js
 | 
				
			||||||
| 
						 | 
					@ -363,20 +365,19 @@ func StartApp(addr string, createContentFunc func(Session) SessionContent, param
 | 
				
			||||||
	apps = append(apps, app)
 | 
						apps = append(apps, app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	redirectAddr := ""
 | 
						redirectAddr := ""
 | 
				
			||||||
 | 
						https := params.AutoCertDomain != "" || (params.CertFile != "" && params.KeyFile != "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if index := strings.IndexRune(addr, ':'); index >= 0 {
 | 
						if index := strings.IndexRune(addr, ':'); index >= 0 {
 | 
				
			||||||
		redirectAddr = addr[:index] + ":80"
 | 
							redirectAddr = addr[:index] + ":80"
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		redirectAddr = addr + ":80"
 | 
							redirectAddr = addr + ":80"
 | 
				
			||||||
		if params.CertFile != "" && params.KeyFile != "" {
 | 
							if https {
 | 
				
			||||||
			addr += ":443"
 | 
								addr += ":443"
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			addr += ":80"
 | 
								addr += ":80"
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	app.server = &http.Server{Addr: addr}
 | 
					 | 
				
			||||||
	http.Handle("/", app)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	serverRun := func(err error) {
 | 
						serverRun := func(err error) {
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			if err == http.ErrServerClosed {
 | 
								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 {
 | 
							if params.Redirect80 {
 | 
				
			||||||
			redirectTLS := func(w http.ResponseWriter, r *http.Request) {
 | 
								redirectTLS := func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
				http.Redirect(w, r, "https://"+addr+r.RequestURI, http.StatusMovedPermanently)
 | 
									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(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 {
 | 
							} 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())
 | 
							serverRun(app.server.ListenAndServe())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,6 +38,8 @@ type AppParams struct {
 | 
				
			||||||
	// of the server's certificate, any intermediates, and the CA's certificate.
 | 
						// of the server's certificate, any intermediates, and the CA's certificate.
 | 
				
			||||||
	CertFile string
 | 
						CertFile string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						AutoCertDomain string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// KeyFile - path of a private key for the server must be provided
 | 
						// KeyFile - path of a private key for the server must be provided
 | 
				
			||||||
	// if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
 | 
						// if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
 | 
				
			||||||
	KeyFile string
 | 
						KeyFile string
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue