123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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())
- // 启动Version
- appInstance.Api().PrefixRouter().AddRoute(http.MethodGet, "/version", func(c *api.Context) {
- c.JSON(http.StatusOK, map[string]any{
- "version": "v1.0.0",
- })
- })
- }
- 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{},
- }
|