package http_binding import ( "context" "errors" "git.sxidc.com/go-tools/api_binding/http_binding/binding_context" "git.sxidc.com/go-tools/api_binding/http_binding/middleware" "git.sxidc.com/service-supports/fslog" "github.com/gin-gonic/gin" "net/http" "time" ) var ( urlPrefix string ) var serverInstance *http.Server var routerInstance *gin.Engine func Init(apiUrlPrefix string, port string) { urlPrefix = apiUrlPrefix routerInstance = gin.New() routerInstance.Use(fslog.GinLogger([]string{"/" + urlPrefix + "/api/version"}), fslog.GinRecovery(), func(c *gin.Context) { middleware.Cors()(&binding_context.Context{Context: c}) }, func(c *gin.Context) { middleware.ReadRequestBodyData()(&binding_context.Context{Context: c}) }) serverInstance = &http.Server{ Addr: ":" + port, Handler: routerInstance, } go func() { if err := serverInstance.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { panic(err) } }() } func Destroy() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := serverInstance.Shutdown(ctx); err != nil { panic(err) } } func GetRouter() *gin.Engine { return routerInstance }