Add signal handling
This commit is contained in:
parent
828d38639c
commit
2280bc4cf6
|
@ -2,8 +2,12 @@ package website
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.handmade.network/hmn/hmn/config"
|
"git.handmade.network/hmn/hmn/config"
|
||||||
"git.handmade.network/hmn/hmn/db"
|
"git.handmade.network/hmn/hmn/db"
|
||||||
|
@ -44,7 +48,29 @@ var WebsiteCommand = &cobra.Command{
|
||||||
|
|
||||||
rw.Write([]byte(fmt.Sprintf("(%s) %s\n", id, name)))
|
rw.Write([]byte(fmt.Sprintf("(%s) %s\n", id, name)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server := http.Server{
|
||||||
|
Addr: config.Config.Addr,
|
||||||
|
Handler: router,
|
||||||
|
}
|
||||||
|
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals, os.Interrupt)
|
||||||
|
go func() {
|
||||||
|
<-signals
|
||||||
|
logging.Info().Msg("Shutting down the website")
|
||||||
|
timeout, _ := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
server.Shutdown(timeout)
|
||||||
|
|
||||||
|
<-signals
|
||||||
|
logging.Warn().Msg("Forcibly killed the website")
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
|
|
||||||
logging.Info().Str("addr", config.Config.Addr).Msg("Serving the website")
|
logging.Info().Str("addr", config.Config.Addr).Msg("Serving the website")
|
||||||
http.ListenAndServe(config.Config.Addr, router)
|
serverErr := server.ListenAndServe()
|
||||||
|
if !errors.Is(serverErr, http.ErrServerClosed) {
|
||||||
|
logging.Error().Err(serverErr).Msg("Server shut down unexpectedly")
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue