| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package api
- import (
- "baize-demo/project/gateway/api/root"
- "baize-demo/project/gateway/api/v1"
- "baize-demo/project/gateway/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"
- "git.sxidc.com/go-framework/baize/framework/gateway"
- "net/http"
- )
- var appInstance *application.App
- var gatewayInstance *gateway.Gateway
- func NewGateway() {
- if appInstance != nil {
- return
- }
- appInstance = baize.NewApplication(config.GetConfig().ApplicationConfig)
- // 注册Router
- appInstance.Api().PrefixRouter().RegisterVersionedRouter("v1")
- // 创建gateway
- gatewayInstance = gateway.NewGateway(appInstance.Api())
- }
- func DestroyGateway() {
- if appInstance == nil {
- return
- }
- gateway.DestroyGateway(gatewayInstance)
- gatewayInstance = nil
- baize.DestroyApplication(appInstance)
- appInstance = nil
- }
- func Start() error {
- // 初始化路由
- for _, router := range RegisteredRouters {
- router.Init(gatewayInstance)
- }
- err := appInstance.Start()
- if err != nil {
- return err
- }
- return nil
- }
- func Finish() error {
- err := appInstance.Finish()
- if err != nil {
- return err
- }
- return nil
- }
- func ServerHttpForTest(w http.ResponseWriter, req *http.Request) {
- appInstance.Api().RootRouter().(*api.RootRouter).ServerHttp(w, req)
- }
- type Router interface {
- Init(gw *gateway.Gateway)
- }
- var RegisteredRouters = []Router{
- &root.Router{},
- &v1.Router{},
- }
|