|
|
@ -3,7 +3,7 @@ package consumers |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"goInOut/adaptors" |
|
|
|
"goInOut/config" |
|
|
|
"goInOut/consumers/HTTP_PRPXY" |
|
|
|
"goInOut/dbHelper" |
|
|
|
"goInOut/utils" |
|
|
@ -14,7 +14,7 @@ import ( |
|
|
|
|
|
|
|
type consumerHttpProxy struct { |
|
|
|
//数据缓存管道
|
|
|
|
ch chan []adaptors.NeedPush |
|
|
|
routes map[string]config.Router |
|
|
|
//具体配置
|
|
|
|
Info HTTP_PRPXY.ConfigFile |
|
|
|
InApiServer *dbHelper.ApiServerHelper |
|
|
@ -31,6 +31,7 @@ func (the *consumerHttpProxy) LoadConfigJson(cfgStr string) { |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerHttpProxy) Initial(cfg string) error { |
|
|
|
the.routes = map[string]config.Router{} |
|
|
|
the.LoadConfigJson(cfg) |
|
|
|
err := the.InputInitial() |
|
|
|
if err != nil { |
|
|
@ -40,7 +41,6 @@ func (the *consumerHttpProxy) Initial(cfg string) error { |
|
|
|
return err |
|
|
|
} |
|
|
|
func (the *consumerHttpProxy) InputInitial() error { |
|
|
|
the.ch = make(chan []adaptors.NeedPush, 200) |
|
|
|
//数据入口
|
|
|
|
the.InApiServer = dbHelper.NewApiServer( |
|
|
|
the.Info.IoConfig.In.ApiServer.Port, |
|
|
@ -60,18 +60,18 @@ func (the *consumerHttpProxy) OutputInitial() error { |
|
|
|
func (the *consumerHttpProxy) Work() { |
|
|
|
go func() { |
|
|
|
for _, router := range the.Info.IoConfig.In.ApiServer.Routers { |
|
|
|
the.InApiServer.RouteRegister(router, the.onData) |
|
|
|
the.InApiServer.RouteRegister(router.Router, the.onData) |
|
|
|
the.routes[router.Router] = router |
|
|
|
} |
|
|
|
the.InApiServer.Run() |
|
|
|
|
|
|
|
}() |
|
|
|
} |
|
|
|
func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) { |
|
|
|
route := "/onData" |
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
body, err := io.ReadAll(r.Body) |
|
|
|
|
|
|
|
log.Printf("收到 %s 请求 %s", route, body) |
|
|
|
log.Printf("收到 %s 请求 %s", r.RequestURI, body) |
|
|
|
|
|
|
|
bodyObj := HTTP_PRPXY.ABStatus{} |
|
|
|
err = json.Unmarshal(body, &bodyObj) |
|
|
@ -79,18 +79,20 @@ func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) { |
|
|
|
log.Printf("body 解析失败,请检查 %s", err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
idTemplate := the.Info.OtherInfo["idTemplate"] |
|
|
|
routePath := fmt.Sprintf("%s %s", r.Method, r.RequestURI) |
|
|
|
idTemplate := the.routes[routePath] |
|
|
|
|
|
|
|
id, err := utils.TextTemplateMatch(bodyObj, idTemplate) |
|
|
|
id, err := utils.TextTemplateMatch(bodyObj, idTemplate.IdTemplate) |
|
|
|
if err != nil { |
|
|
|
log.Printf("解析id 失败,请检查 %s", err.Error()) |
|
|
|
} |
|
|
|
params := map[string]string{ |
|
|
|
"id": id, |
|
|
|
} |
|
|
|
resp := "" |
|
|
|
//沿用请求路由
|
|
|
|
newUrl := the.outHttpPost.Url + r.URL.String() |
|
|
|
resp, err := the.outHttpPost.HttpPostWithParams(newUrl, string(body), params) |
|
|
|
resp, err = the.outHttpPost.HttpPostWithParams(newUrl, string(body), params) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
@ -98,15 +100,3 @@ func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) { |
|
|
|
defer fmt.Fprintf(w, resp) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerHttpProxy) getAdaptor(flag string) (adaptor adaptors.IAdaptor4) { |
|
|
|
bridgeCode := "" |
|
|
|
if v, ok := the.Info.OtherInfo["bridgeCode"]; ok { |
|
|
|
bridgeCode = v |
|
|
|
} |
|
|
|
if bridgeCode == "" { |
|
|
|
panic("无正确的 bridgeCode") |
|
|
|
} |
|
|
|
|
|
|
|
return adaptor |
|
|
|
} |
|
|
|