Browse Source

update 版本默认采用go 1.22 增强路由

添加 apiServer功能
pull/2/head
lucas 2 months ago
parent
commit
68b672cfcd
  1. 20
      configFiles/config_转发http2axy60000端口.json
  2. 0
      configFiles/弃用备份/config_安心云设备数据_最新同步.json
  3. 20
      configFiles/弃用备份/config_转发http2axy60000端口.json
  4. 37
      dbHelper/apiServerHelper.go
  5. 2
      go.mod

20
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"
}
}
}

0
configFiles/config_安心云设备数据_最新同步.json → configFiles/弃用备份/config_安心云设备数据_最新同步.json

20
configFiles/弃用备份/config_转发http2axy60000端口.json

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

37
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)
}
}

2
go.mod

@ -1,6 +1,6 @@
module goInOut
go 1.21
go 1.22
require (
github.com/IBM/sarama v1.43.3

Loading…
Cancel
Save