From 68b672cfcd1dd5cb2caf9f49f040ac0788aaad0b Mon Sep 17 00:00:00 2001 From: lucas Date: Tue, 19 Nov 2024 11:29:25 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=89=88=E6=9C=AC=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E9=87=87=E7=94=A8go=201.22=20=20=E5=A2=9E=E5=BC=BA=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=20=E6=B7=BB=E5=8A=A0=20apiServer=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config_转发http2axy60000端口.json | 20 ++++++++++ ...ig_安心云设备数据_最新同步.json | 0 .../config_转发http2axy60000端口.json | 20 ---------- dbHelper/apiServerHelper.go | 37 ++++++++++--------- go.mod | 2 +- 5 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 configFiles/config_转发http2axy60000端口.json rename configFiles/{ => 弃用备份}/config_安心云设备数据_最新同步.json (100%) delete mode 100644 configFiles/弃用备份/config_转发http2axy60000端口.json diff --git a/configFiles/config_转发http2axy60000端口.json b/configFiles/config_转发http2axy60000端口.json new file mode 100644 index 0000000..a45dd7a --- /dev/null +++ b/configFiles/config_转发http2axy60000端口.json @@ -0,0 +1,20 @@ +{ + "consumer": "consumerHttpProxy", + "ioConfig": { + "in": { + "apiServer": { + "port": 7000, + "userName": "anQuanDai", + "password": "123", + "routes": { + "route1": "POST /upload/work/ABStatus", + "route2": "POST /upload/work/ABHeart" + } + } + }, + "out": { + "url": "http://127.0.0.1:4009/write", + "method": "post" + } + } +} \ No newline at end of file diff --git a/configFiles/config_安心云设备数据_最新同步.json b/configFiles/弃用备份/config_安心云设备数据_最新同步.json similarity index 100% rename from configFiles/config_安心云设备数据_最新同步.json rename to configFiles/弃用备份/config_安心云设备数据_最新同步.json diff --git a/configFiles/弃用备份/config_转发http2axy60000端口.json b/configFiles/弃用备份/config_转发http2axy60000端口.json deleted file mode 100644 index d01b113..0000000 --- a/configFiles/弃用备份/config_转发http2axy60000端口.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "consumer": "consumerHttpProxy", - "ioConfig": { - "in": { - "httpServer": { - "port": 19700, - "userName": "goInOut", - "password": "", - "routes": [ - "upload/uds/+", - "upload/ZD/+" - ] - } - }, - "out": { - "url": "http://127.0.0.1:4009/write?u=mingyuexia_wkd&p=mingyuexia_wkd&db=MingYueXia_Bridge&rp=autogen", - "method": "post" - } - } -} \ No newline at end of file diff --git a/dbHelper/apiServerHelper.go b/dbHelper/apiServerHelper.go index 665d224..06c5d79 100644 --- a/dbHelper/apiServerHelper.go +++ b/dbHelper/apiServerHelper.go @@ -7,40 +7,43 @@ import ( ) type ApiServerHelper struct { - mux *http.ServeMux - route map[string]string - port uint + mux *http.ServeMux + routes map[string]string + port uint } func NewApiServer(port uint, routes map[string]string) *ApiServerHelper { return &ApiServerHelper{ - mux: http.NewServeMux(), - route: routes, - port: port, + mux: http.NewServeMux(), + routes: routes, + port: port, } } func (the *ApiServerHelper) Initial() { the.mux = http.NewServeMux() + // 创建 HTTP 服务器 ser := http.Server{ Handler: the.mux, Addr: fmt.Sprintf(":%d", the.port), } + + the.routeRegister() + log.Printf("apiServer监听端口 %d", the.port) go log.Fatal(ser.ListenAndServe()) } func (the *ApiServerHelper) routeRegister() { - the.mux.HandleFunc("GET /nodeList", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - s := `{"a":1}` - fmt.Fprintf(w, s) - }) - - the.mux.HandleFunc("GET /namespaceList", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - s := `{"b":1}` - fmt.Fprintf(w, s) - }) + + for _, rote := range the.routes { + the.mux.HandleFunc(rote, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + s := fmt.Sprintf(`{"rote":"%s","resp":"安全带状态应答"}`, rote) + println("收到请求", rote) + fmt.Fprintf(w, s) + }) + log.Printf("注册路由 %s", rote) + } } diff --git a/go.mod b/go.mod index 685dc9a..e69a73f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module goInOut -go 1.21 +go 1.22 require ( github.com/IBM/sarama v1.43.3