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

@@ -60,16 +60,16 @@ func (s State) String() string {
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) {
var m map[string]interface{}
json.Unmarshal(resp.Payload, &m)
if len(m) == 0 {
a.SubscribeRouter(infos)
a.SubscribeRouter(infos, extra...)
}
}
a.ListenRouter(respondToDiscovery)
a.SubscribeRouter(infos)
a.SubscribeRouter(infos, extra...)
}
// 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()
appPrefix := "/" + strings.ReplaceAll(config.GetAppName(), "oc-", "")
discovery := map[string][]string{}
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() {
if discovery[path] == nil {
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)
go nats.SetNATSPub(DISCOVERY, NATSResponse{