Avoid os.Exit() in function with deferred actions.

This commit is contained in:
Solderpunk 2023-03-02 19:28:51 +01:00
parent a3eecd8391
commit 641f72a558
1 changed files with 8 additions and 3 deletions

11
main.go
View File

@ -13,6 +13,10 @@ import (
)
func main() {
os.Exit(main_body())
}
func main_body() int {
var conf_file string
// Parse args and read config
@ -27,14 +31,14 @@ func main() {
config, err := getConfig(conf_file)
if err != nil {
fmt.Println("Error reading config file " + conf_file)
os.Exit(1)
return 1
}
// Open logfile
logfile, err := os.OpenFile(config.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening log file " + config.LogPath + ".")
os.Exit(2)
return 2
}
defer logfile.Close()
@ -67,7 +71,8 @@ func main() {
http_server.Shutdown(context.Background())
https_server.Shutdown(context.Background())
case err := <-errs:
log.Fatal(err)
log.Println("Fatal: " + err.Error())
}
return 0
}