MCPcopy
hub / github.com/pocketbase/pocketbase / loadMux

Method loadMux

tools/router/router.go:82–158  ·  view source on GitHub ↗
(mux *http.ServeMux, group *RouterGroup[T], parents []*RouterGroup[T])

Source from the content-addressed store, hash-verified

80}
81
82func (r *Router[T]) loadMux(mux *http.ServeMux, group *RouterGroup[T], parents []*RouterGroup[T]) error {
83 for _, child := range group.children {
84 switch v := child.(type) {
85 case *RouterGroup[T]:
86 if err := r.loadMux(mux, v, append(parents, group)); err != nil {
87 return err
88 }
89 case *Route[T]:
90 routeHook := &hook.Hook[T]{}
91
92 var pattern string
93
94 if v.Method != "" {
95 pattern = v.Method + " "
96 }
97
98 // add parent groups middlewares
99 for _, p := range parents {
100 pattern += p.Prefix
101 for _, h := range p.Middlewares {
102 if _, ok := p.excludedMiddlewares[h.Id]; !ok {
103 if _, ok = group.excludedMiddlewares[h.Id]; !ok {
104 if _, ok = v.excludedMiddlewares[h.Id]; !ok {
105 routeHook.Bind(h)
106 }
107 }
108 }
109 }
110 }
111
112 // add current groups middlewares
113 pattern += group.Prefix
114 for _, h := range group.Middlewares {
115 if _, ok := group.excludedMiddlewares[h.Id]; !ok {
116 if _, ok = v.excludedMiddlewares[h.Id]; !ok {
117 routeHook.Bind(h)
118 }
119 }
120 }
121
122 // add current route middlewares
123 pattern += v.Path
124 for _, h := range v.Middlewares {
125 if _, ok := v.excludedMiddlewares[h.Id]; !ok {
126 routeHook.Bind(h)
127 }
128 }
129
130 mux.HandleFunc(pattern, func(resp http.ResponseWriter, req *http.Request) {
131 // wrap the response to add write and status tracking
132 resp = &ResponseWriter{ResponseWriter: resp}
133
134 // wrap the request body to allow multiple reads
135 body := &RereadableReadCloser{ReadCloser: req.Body}
136 defer body.Close()
137
138 req.Body = body
139

Callers 1

BuildMuxMethod · 0.95

Calls 4

BindMethod · 0.95
CloseMethod · 0.95
TriggerMethod · 0.95
ErrorHandlerFunction · 0.85

Tested by

no test coverage detected