* app: move code for http server loop to a separate function

This commit is contained in:
Simon Zolin 2019-04-23 11:36:23 +03:00
parent 5f88abb322
commit 1ac9419c94
1 changed files with 53 additions and 48 deletions

49
app.go
View File

@ -168,11 +168,34 @@ func run(args options) {
httpsServer.cond = sync.NewCond(&httpsServer.Mutex) httpsServer.cond = sync.NewCond(&httpsServer.Mutex)
// for https, we have a separate goroutine loop // for https, we have a separate goroutine loop
go func() { go httpServerLoop()
for { // this is an endless loop
// this loop is used as an ability to change listening host and/or port
for {
printHTTPAddresses("http")
// we need to have new instance, because after Shutdown() the Server is not usable
address := net.JoinHostPort(config.BindHost, strconv.Itoa(config.BindPort))
httpServer = &http.Server{
Addr: address,
}
err := httpServer.ListenAndServe()
if err != http.ErrServerClosed {
cleanupAlways()
log.Fatal(err)
}
// We use ErrServerClosed as a sign that we need to rebind on new address, so go back to the start of the loop
}
}
func httpServerLoop() {
for {
httpsServer.cond.L.Lock() httpsServer.cond.L.Lock()
// this mechanism doesn't let us through until all conditions are ment // this mechanism doesn't let us through until all conditions are met
for config.TLS.Enabled == false || config.TLS.PortHTTPS == 0 || config.TLS.PrivateKey == "" || config.TLS.CertificateChain == "" { // sleep until necessary data is supplied for config.TLS.Enabled == false ||
config.TLS.PortHTTPS == 0 ||
config.TLS.PrivateKey == "" ||
config.TLS.CertificateChain == "" { // sleep until necessary data is supplied
httpsServer.cond.Wait() httpsServer.cond.Wait()
} }
address := net.JoinHostPort(config.BindHost, strconv.Itoa(config.TLS.PortHTTPS)) address := net.JoinHostPort(config.BindHost, strconv.Itoa(config.TLS.PortHTTPS))
@ -215,24 +238,6 @@ func run(args options) {
log.Fatal(err) log.Fatal(err)
} }
} }
}()
// this loop is used as an ability to change listening host and/or port
for {
printHTTPAddresses("http")
// we need to have new instance, because after Shutdown() the Server is not usable
address := net.JoinHostPort(config.BindHost, strconv.Itoa(config.BindPort))
httpServer = &http.Server{
Addr: address,
}
err := httpServer.ListenAndServe()
if err != http.ErrServerClosed {
cleanupAlways()
log.Fatal(err)
}
// We use ErrServerClosed as a sign that we need to rebind on new address, so go back to the start of the loop
}
} }
// Check if the current user has root (administrator) rights // Check if the current user has root (administrator) rights