Browse Source

update 支持多路由处理

pull/2/head
lucas 2 months ago
parent
commit
9067faeb58
  1. 8
      config/configStruct.go
  2. 17
      configFiles/config_转发http2axy60000端口.json
  3. 32
      consumers/consumerHTTP_PRPXY.go
  4. 9
      dbHelper/apiServerHelper.go

8
config/configStruct.go

@ -40,8 +40,12 @@ type HttpConfig struct {
}
type ApiServerConfig struct {
Port uint `json:"port"`
Routers map[string]string `json:"routers"`
Port uint `json:"port"`
Routers []Router `json:"routers"`
}
type Router struct {
Router string `json:"router"`
IdTemplate string `json:"idTemplate"`
}
type Token struct {

17
configFiles/config_转发http2axy60000端口.json

@ -4,12 +4,18 @@
"in": {
"apiServer": {
"port": 7000,
"userName": "anQuanDai",
"userName": "usr",
"password": "123",
"routers": {
"route1": "POST /upload/work/ABStatus",
"route2": "POST /upload/work/ABHeart"
}
"routers": [
{
"router": "POST /upload/work/ABStatus",
"idTemplate": "{{.Values.UcId}}"
},
{
"router": "POST /upload/work/ABSHeart",
"idTemplate": "{{.Values.WorkId}}"
}
]
}
},
"out": {
@ -20,6 +26,5 @@
}
},
"info": {
"idTemplate": "{{.Values.UcId}}"
}
}

32
consumers/consumerHTTP_PRPXY.go

@ -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
}

9
dbHelper/apiServerHelper.go

@ -2,18 +2,19 @@ package dbHelper
import (
"fmt"
"goInOut/config"
"log"
"net/http"
)
type ApiServerHelper struct {
mux *http.ServeMux
routes map[string]string
routes []config.Router
port uint
server http.Server
}
func NewApiServer(port uint, routes map[string]string) *ApiServerHelper {
func NewApiServer(port uint, routes []config.Router) *ApiServerHelper {
return &ApiServerHelper{
mux: http.NewServeMux(),
routes: routes,
@ -41,10 +42,10 @@ func (the *ApiServerHelper) Run() {
func (the *ApiServerHelper) routeRegister() {
for _, route := range the.routes {
the.mux.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
the.mux.HandleFunc(route.Router, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
s := fmt.Sprintf(`{"route":"%s","resp":"安全带状态应答"}`, route)
println("收到请求", route)
println("收到请求", route.Router)
fmt.Fprintf(w, s)
})
log.Printf("注册路由 %s", route)

Loading…
Cancel
Save