I´m using a https-Server in golang to serve some functions and files secured with basic auth. This all had worked wonderfully to the point I implemented a waitgroup for shutting down the server. This is needed cause i´m programming on gokrazy and have to shutdown the server one time a day at 0 o´clock to renew the certificate with let´s encrypt.
I have tried to implemet as in the following thread discribed, but it´s not working anymore (How to stop http.ListenAndServe()).
The HTTP-Server ist starting and working fine, the HTTPS-Server isn´t starting.
Hope you can help me with this problem Yours Christian
Code:
Main Thread (starting both web servers (HTTP and HTTPS fpr 30 secounds):
//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-Server
//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-Server (not working)
//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
}
Log if i start the server locally and connect to the 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/
Error if i coonect to the HTTPS-Site:
Timeout