Files
2024-07-19 17:04:42 +02:00

42 lines
960 B
Go

package ginserver
import (
"errors"
"log"
"mangezmieux-backend/internal/injector"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
func Start(inj *injector.Injector, port string) {
router := injector.Get[*gin.Engine](inj, routerInjectorKey)
srv := &http.Server{
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 4 * time.Second,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
}