53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package pkgconfig
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/ardeman/project-legalgo-go/config"
|
|
"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("/api", apiRouter)
|
|
mainCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
svr := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", config.APP_PORT),
|
|
Handler: mainRouter,
|
|
}
|
|
|
|
group, groupCtx := errgroup.WithContext(mainCtx)
|
|
|
|
group.Go(func() error {
|
|
logrus.Infof("Listening to port %d", config.APP_PORT)
|
|
return svr.ListenAndServe()
|
|
})
|
|
|
|
group.Go(func() error {
|
|
<-groupCtx.Done()
|
|
|
|
ctxTimeout, cancel := context.WithTimeout(mainCtx, time.Duration(config.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")
|
|
}
|
|
}
|