MCPcopy
hub / github.com/gofiber/fiber / processSubAppsRoutes

Method processSubAppsRoutes

mount.go:170–229  ·  view source on GitHub ↗

processSubAppsRoutes adds routes of sub-apps recursively when the server is started

()

Source from the content-addressed store, hash-verified

168
169// processSubAppsRoutes adds routes of sub-apps recursively when the server is started
170func (app *App) processSubAppsRoutes() {
171 for prefix, subApp := range app.mountFields.appList {
172 // skip real app
173 if prefix == "" {
174 continue
175 }
176 // process the inner routes
177 if subApp.hasMountedApps() {
178 subApp.mountFields.subAppsRoutesAdded.Do(func() {
179 subApp.processSubAppsRoutes()
180 })
181 }
182 }
183 var handlersCount uint32
184 // Iterate over the stack of the parent app
185 for m := range app.stack {
186 // Iterate over each route in the stack
187 stackLen := len(app.stack[m])
188 for i := 0; i < stackLen; i++ {
189 route := app.stack[m][i]
190 // Check if the route has a mounted app
191 if !route.mount {
192 if !route.use || (route.use && m == 0) {
193 handlersCount += uint32(len(route.Handlers)) //nolint:gosec // G115 - handler count is always small
194 }
195 continue
196 }
197
198 // Create a slice to hold the sub-app's routes
199 subRoutes := make([]*Route, len(route.group.app.stack[m]))
200
201 // Iterate over the sub-app's routes
202 for j, subAppRoute := range route.group.app.stack[m] {
203 // Clone the sub-app's route
204 subAppRouteClone := app.copyRoute(subAppRoute)
205
206 // Add the parent route's path as a prefix to the sub-app's route
207 app.addPrefixToRoute(route.path, subAppRouteClone, route.group.app.config.RegexHandler, route.group.app.customConstraints...)
208
209 // Add the cloned sub-app's route to the slice of sub-app routes
210 subRoutes[j] = subAppRouteClone
211 }
212
213 // Insert the sub-app's routes into the parent app's stack
214 newStack := make([]*Route, len(app.stack[m])+len(subRoutes)-1)
215 copy(newStack[:i], app.stack[m][:i])
216 copy(newStack[i:i+len(subRoutes)], subRoutes)
217 copy(newStack[i+len(subRoutes):], app.stack[m][i+1:])
218 app.stack[m] = newStack
219
220 i--
221
222 // Mark the parent app's routes as refreshed
223 app.hasRoutesRefreshed = true
224 // update stackLen after appending subRoutes to app.stack[m]
225 stackLen = len(app.stack[m])
226 }
227 }

Callers 1

mountStartupProcessMethod · 0.95

Calls 4

copyRouteMethod · 0.95
addPrefixToRouteMethod · 0.95
hasMountedAppsMethod · 0.80
DoMethod · 0.65

Tested by

no test coverage detected