mirror of https://github.com/jlelse/GoBlog
Graceful shutdown
This commit is contained in:
parent
081259fca7
commit
b57d1b2d2f
21
http.go
21
http.go
|
@ -1,10 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func startServer() {
|
func startServer() {
|
||||||
|
@ -19,7 +24,21 @@ func startServer() {
|
||||||
e.GET("/*", servePost)
|
e.GET("/*", servePost)
|
||||||
|
|
||||||
address := ":" + strconv.Itoa(appConfig.server.port)
|
address := ":" + strconv.Itoa(appConfig.server.port)
|
||||||
e.Logger.Fatal(e.Start(address))
|
go func() {
|
||||||
|
if err := e.Start(address); err != nil {
|
||||||
|
log.Println("Shutting down the server")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := e.Shutdown(ctx); err != nil {
|
||||||
|
e.Logger.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func hello(c echo.Context) error {
|
func hello(c echo.Context) error {
|
||||||
|
|
6
main.go
6
main.go
|
@ -15,7 +15,11 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Fatal(closeDb())
|
log.Println("Close database")
|
||||||
|
err := closeDb()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
log.Println("Loaded database")
|
log.Println("Loaded database")
|
||||||
log.Println("Start server")
|
log.Println("Start server")
|
||||||
|
|
Loading…
Reference in New Issue