Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): server dynamic update route #3336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/io/http/httpserver/data_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 EMQ Technologies Co., Ltd.
// Copyright 2022-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,7 @@
endpoint map[string]string
server *http.Server
router *mux.Router
routes map[string]http.HandlerFunc
upgrader websocket.Upgrader
websocketEndpoint map[string]*websocketEndpointContext
}
Expand Down Expand Up @@ -68,6 +69,7 @@
endpoint: map[string]string{},
server: s,
router: r,
routes: map[string]http.HandlerFunc{},
upgrader: upgrader,
}
go func(m *GlobalServerManager) {
Expand Down Expand Up @@ -111,7 +113,7 @@
m.endpoint[key] = topic
}
pubsub.CreatePub(topic)
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
m.routes[endpoint] = func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, err := io.ReadAll(r.Body)
if err != nil {
Expand All @@ -121,6 +123,13 @@
pubsub.ProduceAny(topoContext.Background(), topic, data)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
if h, ok := m.routes[endpoint]; ok {
h(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
}

Check warning on line 132 in internal/io/http/httpserver/data_server.go

View check run for this annotation

Codecov / codecov/patch

internal/io/http/httpserver/data_server.go#L131-L132

Added lines #L131 - L132 were not covered by tests
}).Methods(method)
return topic, nil
}
Expand All @@ -135,6 +144,7 @@
return
}
delete(m.endpoint, key)
delete(m.routes, endpoint)
pubsub.RemovePub(TopicPrefix + key)
}

Expand Down
12 changes: 11 additions & 1 deletion internal/io/http/httpserver/websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,26 @@
rTopic := recvTopic(endpoint, true)
sTopic := sendTopic(endpoint, true)
pubsub.CreatePub(rTopic)
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
m.routes[endpoint] = func(w http.ResponseWriter, r *http.Request) {
c, err := m.upgrader.Upgrade(w, r, nil)
if err != nil {
conf.Log.Errorf("websocket upgrade error: %v", err)
return
}
fmt.Printf("is context updated?: %p\n", ctx)
subCtx, cancel := ctx.WithCancel()
wg := m.AddEndpointConnection(endpoint, c, cancel)
go m.handleProcess(subCtx, endpoint, m.FetchInstanceID(), c, cancel, wg)
conf.Log.Infof("websocket endpint %v create connection", endpoint)
}
m.router.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
if h, ok := m.routes[endpoint]; ok {
h(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
}

Check warning on line 154 in internal/io/http/httpserver/websocket_server.go

View check run for this annotation

Codecov / codecov/patch

internal/io/http/httpserver/websocket_server.go#L153-L154

Added lines #L153 - L154 were not covered by tests
})

conf.Log.Infof("websocker endpoint %v registered success", endpoint)
return rTopic, sTopic, nil
}
Expand All @@ -163,6 +172,7 @@
cancel()
}
delete(m.websocketEndpoint, endpoint)
delete(m.routes, endpoint)
return wctx
}

Expand Down
Loading