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
0 commit comments