50 lines
789 B
Go
50 lines
789 B
Go
package routes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
func NewGinRouter(lc fx.Lifecycle) *gin.Engine {
|
|
|
|
router := gin.Default()
|
|
|
|
server := &http.Server{
|
|
Addr: fmt.Sprintf(":%s", "8080"),
|
|
Handler: router,
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
// TODO: log
|
|
return
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
},
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
// TODO: log
|
|
return nil
|
|
}
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
return router
|
|
}
|