Browse Source

update 支持多路由处理

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

6
config/configStruct.go

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

15
configFiles/config_转发http2axy60000端口.json

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

32
consumers/consumerHTTP_PRPXY.go

@ -3,7 +3,7 @@ package consumers
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"goInOut/adaptors" "goInOut/config"
"goInOut/consumers/HTTP_PRPXY" "goInOut/consumers/HTTP_PRPXY"
"goInOut/dbHelper" "goInOut/dbHelper"
"goInOut/utils" "goInOut/utils"
@ -14,7 +14,7 @@ import (
type consumerHttpProxy struct { type consumerHttpProxy struct {
//数据缓存管道 //数据缓存管道
ch chan []adaptors.NeedPush routes map[string]config.Router
//具体配置 //具体配置
Info HTTP_PRPXY.ConfigFile Info HTTP_PRPXY.ConfigFile
InApiServer *dbHelper.ApiServerHelper InApiServer *dbHelper.ApiServerHelper
@ -31,6 +31,7 @@ func (the *consumerHttpProxy) LoadConfigJson(cfgStr string) {
} }
func (the *consumerHttpProxy) Initial(cfg string) error { func (the *consumerHttpProxy) Initial(cfg string) error {
the.routes = map[string]config.Router{}
the.LoadConfigJson(cfg) the.LoadConfigJson(cfg)
err := the.InputInitial() err := the.InputInitial()
if err != nil { if err != nil {
@ -40,7 +41,6 @@ func (the *consumerHttpProxy) Initial(cfg string) error {
return err return err
} }
func (the *consumerHttpProxy) InputInitial() error { func (the *consumerHttpProxy) InputInitial() error {
the.ch = make(chan []adaptors.NeedPush, 200)
//数据入口 //数据入口
the.InApiServer = dbHelper.NewApiServer( the.InApiServer = dbHelper.NewApiServer(
the.Info.IoConfig.In.ApiServer.Port, the.Info.IoConfig.In.ApiServer.Port,
@ -60,18 +60,18 @@ func (the *consumerHttpProxy) OutputInitial() error {
func (the *consumerHttpProxy) Work() { func (the *consumerHttpProxy) Work() {
go func() { go func() {
for _, router := range the.Info.IoConfig.In.ApiServer.Routers { 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() the.InApiServer.Run()
}() }()
} }
func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) { func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) {
route := "/onData"
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
log.Printf("收到 %s 请求 %s", route, body) log.Printf("收到 %s 请求 %s", r.RequestURI, body)
bodyObj := HTTP_PRPXY.ABStatus{} bodyObj := HTTP_PRPXY.ABStatus{}
err = json.Unmarshal(body, &bodyObj) 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()) log.Printf("body 解析失败,请检查 %s", err.Error())
return 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 { if err != nil {
log.Printf("解析id 失败,请检查 %s", err.Error()) log.Printf("解析id 失败,请检查 %s", err.Error())
} }
params := map[string]string{ params := map[string]string{
"id": id, "id": id,
} }
resp := ""
//沿用请求路由 //沿用请求路由
newUrl := the.outHttpPost.Url + r.URL.String() 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 { if err != nil {
return return
} }
@ -98,15 +100,3 @@ func (the *consumerHttpProxy) onData(w http.ResponseWriter, r *http.Request) {
defer fmt.Fprintf(w, resp) 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 ( import (
"fmt" "fmt"
"goInOut/config"
"log" "log"
"net/http" "net/http"
) )
type ApiServerHelper struct { type ApiServerHelper struct {
mux *http.ServeMux mux *http.ServeMux
routes map[string]string routes []config.Router
port uint port uint
server http.Server server http.Server
} }
func NewApiServer(port uint, routes map[string]string) *ApiServerHelper { func NewApiServer(port uint, routes []config.Router) *ApiServerHelper {
return &ApiServerHelper{ return &ApiServerHelper{
mux: http.NewServeMux(), mux: http.NewServeMux(),
routes: routes, routes: routes,
@ -41,10 +42,10 @@ func (the *ApiServerHelper) Run() {
func (the *ApiServerHelper) routeRegister() { func (the *ApiServerHelper) routeRegister() {
for _, route := range the.routes { 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") w.Header().Set("Content-Type", "application/json")
s := fmt.Sprintf(`{"route":"%s","resp":"安全带状态应答"}`, route) s := fmt.Sprintf(`{"route":"%s","resp":"安全带状态应答"}`, route)
println("收到请求", route) println("收到请求", route.Router)
fmt.Fprintf(w, s) fmt.Fprintf(w, s)
}) })
log.Printf("注册路由 %s", route) log.Printf("注册路由 %s", route)

Loading…
Cancel
Save