@@ -14,9 +14,10 @@ import (
1414)
1515
1616type HTTPRouter struct {
17- mux * http.ServeMux // Default HTTP multiplexer
18- handlers map [string ]quesma_api.HandlersPipe // Map to store custom route handlers
19- mutex sync.RWMutex // Mutex for concurrent access to handlers
17+ mux * http.ServeMux // Default HTTP multiplexer
18+ handlers map [string ]quesma_api.HandlersPipe // Map to store custom route handlers
19+ fallbackHandler quesma_api.HTTPFrontendHandler
20+ mutex sync.RWMutex // Mutex for concurrent access to handlers
2021}
2122
2223func NewHTTPRouter () * HTTPRouter {
@@ -34,13 +35,26 @@ func (router *HTTPRouter) AddRoute(path string, handler quesma_api.HTTPFrontendH
3435 fmt .Printf ("Added route: %s\n " , path )
3536}
3637
38+ func (router * HTTPRouter ) AddFallbackHandler (handler quesma_api.HTTPFrontendHandler ) {
39+ router .mutex .Lock ()
40+ defer router .mutex .Unlock ()
41+ router .fallbackHandler = handler
42+ }
43+
44+ func (router * HTTPRouter ) GetFallbackHandler () quesma_api.HTTPFrontendHandler {
45+ router .mutex .RLock ()
46+ defer router .mutex .RUnlock ()
47+ return router .fallbackHandler
48+ }
49+
3750func (router * HTTPRouter ) Clone () quesma_api.Cloner {
3851 newRouter := NewHTTPRouter ()
3952 router .mutex .Lock ()
4053 defer router .mutex .Unlock ()
4154 for path , handler := range router .handlers {
4255 newRouter .handlers [path ] = handler
4356 }
57+ newRouter .fallbackHandler = router .fallbackHandler
4458 return newRouter
4559}
4660
@@ -97,15 +111,25 @@ func (h *BasicHTTPFrontendConnector) GetRouter() quesma_api.Router {
97111
98112func (h * BasicHTTPFrontendConnector ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
99113 handlerWrapper , exists := h .router .GetHandlers ()[req .URL .Path ]
114+ dispatcher := & quesma_api.Dispatcher {}
100115 if ! exists {
101- h .router .Multiplexer ().ServeHTTP (w , req )
116+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
117+ if h .router .GetFallbackHandler () != nil {
118+ fmt .Printf ("No handler found for path: %s\n " , req .URL .Path )
119+ handler := h .router .GetFallbackHandler ()
120+ _ , message , _ := handler (req )
121+ _ , err := w .Write (message .([]byte ))
122+ if err != nil {
123+ fmt .Printf ("Error writing response: %s\n " , err )
124+ }
125+ }
126+ }).ServeHTTP (w , req )
102127 return
103128 }
104- dispatcher := & quesma_api.Dispatcher {}
105129 http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
106130 metadata , message , _ := handlerWrapper .Handler (req )
107131
108- metadata , message = dispatcher .Dispatch (handlerWrapper .Processors , metadata , message )
132+ _ , message = dispatcher .Dispatch (handlerWrapper .Processors , metadata , message )
109133 _ , err := w .Write (message .([]byte ))
110134 if err != nil {
111135 fmt .Printf ("Error writing response: %s\n " , err )
0 commit comments