OC LIB -> EXTRA

This commit is contained in:
mr
2026-03-27 12:41:31 +01:00
parent 39cb1c715c
commit 45f2351b2f
2 changed files with 17 additions and 7 deletions

View File

@@ -218,7 +218,7 @@ func ExtractTokenInfo(request http.Request) (string, string, []string) {
return "", "", []string{} return "", "", []string{}
} }
func InitAPI(appName string) { func InitAPI(appName string, extraRoutes ...map[string][]string) {
InitDaemon(appName) InitDaemon(appName)
beego.BConfig.Listen.HTTPPort = config.GetConfig().APIPort beego.BConfig.Listen.HTTPPort = config.GetConfig().APIPort
beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.DirectoryIndex = true
@@ -232,7 +232,7 @@ func InitAPI(appName string) {
}) })
beego.InsertFilter("*", beego.BeforeRouter, c) beego.InsertFilter("*", beego.BeforeRouter, c)
api := &tools.API{} api := &tools.API{}
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo()) api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo(), extraRoutes...)
} }
// //

View File

@@ -60,16 +60,16 @@ func (s State) String() string {
type API struct{} type API struct{}
func (a *API) Discovered(infos []*beego.ControllerInfo) { func (a *API) Discovered(infos []*beego.ControllerInfo, extra ...map[string][]string) {
respondToDiscovery := func(resp NATSResponse) { respondToDiscovery := func(resp NATSResponse) {
var m map[string]interface{} var m map[string]interface{}
json.Unmarshal(resp.Payload, &m) json.Unmarshal(resp.Payload, &m)
if len(m) == 0 { if len(m) == 0 {
a.SubscribeRouter(infos) a.SubscribeRouter(infos, extra...)
} }
} }
a.ListenRouter(respondToDiscovery) a.ListenRouter(respondToDiscovery)
a.SubscribeRouter(infos) a.SubscribeRouter(infos, extra...)
} }
// GetState returns the state of the API // GetState returns the state of the API
@@ -99,11 +99,12 @@ func (a *API) ListenRouter(exec func(msg NATSResponse)) {
}) })
} }
func (a *API) SubscribeRouter(infos []*beego.ControllerInfo) { func (a *API) SubscribeRouter(infos []*beego.ControllerInfo, extra ...map[string][]string) {
nats := NewNATSCaller() nats := NewNATSCaller()
appPrefix := "/" + strings.ReplaceAll(config.GetAppName(), "oc-", "")
discovery := map[string][]string{} discovery := map[string][]string{}
for _, info := range infos { for _, info := range infos {
path := strings.ReplaceAll(info.GetPattern(), "/oc/", "/"+strings.ReplaceAll(config.GetAppName(), "oc-", "")) path := strings.ReplaceAll(info.GetPattern(), "/oc/", appPrefix+"/")
for k, v := range info.GetMethod() { for k, v := range info.GetMethod() {
if discovery[path] == nil { if discovery[path] == nil {
discovery[path] = []string{} discovery[path] = []string{}
@@ -115,6 +116,15 @@ func (a *API) SubscribeRouter(infos []*beego.ControllerInfo) {
} }
} }
} }
for _, extraRoutes := range extra {
for rawPath, methods := range extraRoutes {
path := strings.ReplaceAll(rawPath, "/oc/", appPrefix+"/")
if discovery[path] == nil {
discovery[path] = []string{}
}
discovery[path] = append(discovery[path], methods...)
}
}
b, _ := json.Marshal(discovery) b, _ := json.Marshal(discovery)
go nats.SetNATSPub(DISCOVERY, NATSResponse{ go nats.SetNATSPub(DISCOVERY, NATSResponse{