OC LIB -> EXTRA
This commit is contained in:
@@ -218,7 +218,7 @@ func ExtractTokenInfo(request http.Request) (string, string, []string) {
|
||||
return "", "", []string{}
|
||||
}
|
||||
|
||||
func InitAPI(appName string) {
|
||||
func InitAPI(appName string, extraRoutes ...map[string][]string) {
|
||||
InitDaemon(appName)
|
||||
beego.BConfig.Listen.HTTPPort = config.GetConfig().APIPort
|
||||
beego.BConfig.WebConfig.DirectoryIndex = true
|
||||
@@ -232,7 +232,7 @@ func InitAPI(appName string) {
|
||||
})
|
||||
beego.InsertFilter("*", beego.BeforeRouter, c)
|
||||
api := &tools.API{}
|
||||
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo())
|
||||
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo(), extraRoutes...)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
20
tools/api.go
20
tools/api.go
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user