go - Golang:TLS Webserver 未在等待组中启动(用于正常关闭)
问题描述
我在 golang 中使用 https-Server 来提供一些使用基本身份验证保护的功能和文件。这一切都非常有效,以至于我实现了一个等待组来关闭服务器。这是必需的,因为我正在 gokrazy 上编程,并且必须每天 0 点关闭服务器一次以使用 let encrypt 更新证书。
我已尝试按照以下线程中描述的方式实现,但它不再起作用(如何停止 http.ListenAndServe())。
HTTP-Server 正在启动并且工作正常,HTTPS-Server 没有启动。
希望你能帮我解决这个问题 Yours Christian
代码:
主线程(启动两个 Web 服务器(HTTP 和 HTTPS fpr 30 秒):
//Start Web Server in Background
log.Printf("Starting Web Server Process")
httpServerExitDone := &sync.WaitGroup{}
httpServerExitDone.Add(1)
srvHTTP := startHttpServer(httpServerExitDone)
httpsServerExitDone := &sync.WaitGroup{}
httpsServerExitDone.Add(1)
srvHTTPS := startHttpsServer(httpsServerExitDone)
log.Printf("Webserver process started successfully")
time.Sleep(5 * time.Second)
if err := srvHTTP.Shutdown(context.TODO()); err != nil {
log.Fatalf("Error stopping the server: %s", err)
}
if err := srvHTTPS.Shutdown(context.TODO()); err != nil {
log.Fatalf("Error stopping the server: %s", err)
}
httpServerExitDone.Wait()
httpsServerExitDone.Wait()
HTTP服务器
//WEB-Server
func startHttpServer(wg *sync.WaitGroup) *http.Server {
srvHTTP := &http.Server{
Addr: ":" + config.PortHTTP,
Handler: http.HandlerFunc(redirect),
}
go func() {
defer wg.Done()
log.Printf("Starting HTTP Redictor Server on Port %s", config.PortHTTP)
// always returns error. ErrServerClosed on graceful close
if err := srvHTTP.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("Error with HTTP-Server: %s", err)
}
}()
// returning reference so caller can call Shutdown()
return srvHTTP
}
HTTPS 服务器(不工作)
//WEB-Server
func startHttpsServer(wg *sync.WaitGroup) *http.Server {
srvHTTPS := &http.Server{
Addr: ":" + config.PortHTTPS,
}
//BasicAuth
authentificator := auth.NewBasicAuthenticator("Dlock", secretForWebUser)
//Static sites
devFS := flag.Bool("devFS", false, "use local file system")
flag.Parse()
fs := http.FileServer(assets)
if *devFS {
fmt.Printf("Use local dev file sytem\n")
fs = http.FileServer(http.Dir("../web/files"))
}
http.HandleFunc("/", authentificator.Wrap(func(w http.ResponseWriter, req *auth.AuthenticatedRequest) {
fs.ServeHTTP(w, &req.Request)
}))
//Main-API
http.HandleFunc("/dlock/v1/management/add", authentificator.Wrap(func (w http.ResponseWriter, r *auth.AuthenticatedRequest) {
newHttpRequest("/dlock/v1/management/add", &r.Request, &w)
}))
http.HandleFunc("/dlock/v1/management/get", authentificator.Wrap(func (w http.ResponseWriter, r *auth.AuthenticatedRequest) {
newHttpRequest("/dlock/v1/management/get", &r.Request, &w)
}))
http.HandleFunc("/dlock/v1/websv", authentificator.Wrap(func (w http.ResponseWriter, r *auth.AuthenticatedRequest) {
newHttpRequest("/dlock/v1/websv", &r.Request, &w)
}))
go func() {
defer wg.Done()
log.Printf("Starting HTTPS Server on Port %s", config.PortHTTPS)
// always returns error. ErrServerClosed on graceful close
if err := srvHTTPS.ListenAndServeTLS(config.Crt, config.Key); err != http.ErrServerClosed {
log.Fatalf("Error with HTTP-Server: %s", err)
}
}()
// returning reference so caller can call Shutdown()
return srvHTTPS
}
如果我在本地启动服务器并连接到 HTTP-Server,则记录:
2021/10/16 16:12:11 Starting Web Server Process
2021/10/16 16:12:11 Webserver process started successfully
2021/10/16 16:12:11 Starting HTTPS Server on Port 8443
2021/10/16 16:12:11 Starting HTTP Redictor Server on Port 8080
2021/10/16 16:12:14 redirect to: https://localhost:8443/
如果我连接到 HTTPS 站点,则会出错:
暂停
解决方案
事实证明,只有我的 Firefox 的站点和端口有问题。
如果我更改端口,它可以工作 - 如果我用 chrome 尝试它,它可以工作。