package application import ( "baize-demo/project/server_ds/application/service" "baize-demo/project/server_ds/config" "git.sxidc.com/go-framework/baize" "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/core/application" "net/http" ) var appInstance *application.App func NewApp() { if appInstance != nil { return } appInstance = baize.NewApplication(config.GetConfig().ApplicationConfig) // 注册Router appInstance.Api().PrefixRouter().RegisterVersionedRouter("v1") err := appInstance.Infrastructure().LocalCache().Set("version", "v1.0.0", 5) if err != nil { panic(err) } err = appInstance.Infrastructure().RedisCache().Set("version", "v1.0.0", 5) if err != nil { panic(err) } } func DestroyApp() { if appInstance == nil { return } err := appInstance.Infrastructure().RedisCache().Clear() if err != nil { panic(err) } err = appInstance.Infrastructure().LocalCache().Clear() if err != nil { panic(err) } baize.DestroyApplication(appInstance) appInstance = nil } func Start() error { // 初始化服务 for _, svc := range service.RegisteredServices { err := svc.Init(appInstance) if err != nil { return err } } err := appInstance.Start() if err != nil { return err } return nil } func Finish() error { err := appInstance.Finish() if err != nil { return err } // 销毁服务 for _, svc := range service.RegisteredServices { err := svc.Destroy() if err != nil { return err } } return nil } func ServerHttpForTest(w http.ResponseWriter, req *http.Request) { appInstance.Api().RootRouter().(*api.RootRouter).ServerHttp(w, req) }