package pkgconfig import ( "context" "fmt" "net/http" "os/signal" "syscall" "time" "github.com/go-chi/chi/v5" "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" ) func Router(apiRouter chi.Router) { mainRouter := chi.NewRouter() mainRouter.Mount("/", apiRouter) mainCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() svr := &http.Server{ Addr: fmt.Sprintf(":%d", APP_PORT), Handler: mainRouter, } group, groupCtx := errgroup.WithContext(mainCtx) group.Go(func() error { logrus.Infof("Listening to port %d", APP_PORT) return svr.ListenAndServe() }) group.Go(func() error { <-groupCtx.Done() ctxTimeout, cancel := context.WithTimeout(mainCtx, GRACEFULL_TIMEOUT*time.Second) defer cancel() svr.Shutdown(ctxTimeout) return nil }) if err := group.Wait(); err != nil { logrus.Errorf("system exit, reason: %v", err.Error()) } else { logrus.Info("system exit normally") } }