Skip to content

Commit 94f8d70

Browse files
authored
fix(http): server dynamic update route (#3336)
Signed-off-by: Jiyong Huang <huangjy@emqx.io>
1 parent b77de3d commit 94f8d70

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

internal/io/http/httpserver/data_server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022-2023 EMQ Technologies Co., Ltd.
1+
// Copyright 2022-2024 EMQ Technologies Co., Ltd.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ type GlobalServerManager struct {
3838
endpoint map[string]string
3939
server *http.Server
4040
router *mux.Router
41+
routes map[string]http.HandlerFunc
4142
upgrader websocket.Upgrader
4243
websocketEndpoint map[string]*websocketEndpointContext
4344
}
@@ -68,6 +69,7 @@ func InitGlobalServerManager(ip string, port int, tlsConf *conf.TlsConf) {
6869
endpoint: map[string]string{},
6970
server: s,
7071
router: r,
72+
routes: map[string]http.HandlerFunc{},
7173
upgrader: upgrader,
7274
}
7375
go func(m *GlobalServerManager) {
@@ -111,7 +113,7 @@ func (m *GlobalServerManager) RegisterEndpoint(endpoint string, method string) (
111113
m.endpoint[key] = topic
112114
}
113115
pubsub.CreatePub(topic)
114-
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
116+
m.routes[endpoint] = func(w http.ResponseWriter, r *http.Request) {
115117
defer r.Body.Close()
116118
data, err := io.ReadAll(r.Body)
117119
if err != nil {
@@ -121,6 +123,13 @@ func (m *GlobalServerManager) RegisterEndpoint(endpoint string, method string) (
121123
pubsub.ProduceAny(topoContext.Background(), topic, data)
122124
w.WriteHeader(http.StatusOK)
123125
_, _ = w.Write([]byte("ok"))
126+
}
127+
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
128+
if h, ok := m.routes[endpoint]; ok {
129+
h(w, r)
130+
} else {
131+
w.WriteHeader(http.StatusNotFound)
132+
}
124133
}).Methods(method)
125134
return topic, nil
126135
}
@@ -135,6 +144,7 @@ func (m *GlobalServerManager) UnregisterEndpoint(endpoint, method string) {
135144
return
136145
}
137146
delete(m.endpoint, key)
147+
delete(m.routes, endpoint)
138148
pubsub.RemovePub(TopicPrefix + key)
139149
}
140150

internal/io/http/httpserver/websocket_server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,26 @@ func (m *GlobalServerManager) RegisterWebSocketEndpoint(ctx api.StreamContext, e
134134
rTopic := recvTopic(endpoint, true)
135135
sTopic := sendTopic(endpoint, true)
136136
pubsub.CreatePub(rTopic)
137-
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
137+
m.routes[endpoint] = func(w http.ResponseWriter, r *http.Request) {
138138
c, err := m.upgrader.Upgrade(w, r, nil)
139139
if err != nil {
140140
conf.Log.Errorf("websocket upgrade error: %v", err)
141141
return
142142
}
143+
fmt.Printf("is context updated?: %p\n", ctx)
143144
subCtx, cancel := ctx.WithCancel()
144145
wg := m.AddEndpointConnection(endpoint, c, cancel)
145146
go m.handleProcess(subCtx, endpoint, m.FetchInstanceID(), c, cancel, wg)
146147
conf.Log.Infof("websocket endpint %v create connection", endpoint)
148+
}
149+
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
150+
if h, ok := m.routes[endpoint]; ok {
151+
h(w, r)
152+
} else {
153+
w.WriteHeader(http.StatusNotFound)
154+
}
147155
})
156+
148157
conf.Log.Infof("websocker endpoint %v registered success", endpoint)
149158
return rTopic, sTopic, nil
150159
}
@@ -163,6 +172,7 @@ func (m *GlobalServerManager) UnRegisterWebSocketEndpoint(endpoint string) *webs
163172
cancel()
164173
}
165174
delete(m.websocketEndpoint, endpoint)
175+
delete(m.routes, endpoint)
166176
return wctx
167177
}
168178

0 commit comments

Comments
 (0)