Mount attaches another app instance as a sub-router along a routing path. It's very useful to split up a large API as many independent routers and compose them as a single service using Mount.
(prefix string, subApp *App)
| 73 | // It's very useful to split up a large API as many independent routers and |
| 74 | // compose them as a single service using Mount. |
| 75 | func (grp *Group) mount(prefix string, subApp *App) Router { |
| 76 | groupPath := getGroupPath(grp.Prefix, prefix) |
| 77 | groupPath = utils.TrimRight(groupPath, '/') |
| 78 | if groupPath == "" { |
| 79 | groupPath = "/" |
| 80 | } |
| 81 | |
| 82 | grp.app.mutex.Lock() |
| 83 | // Support for configs of mounted-apps and sub-mounted-apps |
| 84 | for mountedPrefixes, subApp := range subApp.mountFields.appList { |
| 85 | path := getGroupPath(groupPath, mountedPrefixes) |
| 86 | |
| 87 | subApp.mountFields.mountPath = path |
| 88 | grp.app.mountFields.appList[path] = subApp |
| 89 | } |
| 90 | grp.app.mutex.Unlock() |
| 91 | |
| 92 | // register mounted group |
| 93 | mountGroup := &Group{Prefix: groupPath, app: subApp} |
| 94 | grp.app.register([]string{methodUse}, groupPath, mountGroup) |
| 95 | |
| 96 | // Execute onMount hooks |
| 97 | if err := subApp.hooks.executeOnMountHooks(grp.app); err != nil { |
| 98 | panic(err) |
| 99 | } |
| 100 | |
| 101 | return grp |
| 102 | } |
| 103 | |
| 104 | // MountPath returns the route pattern where the current app instance was mounted as a sub-application. |
| 105 | func (app *App) MountPath() string { |
no test coverage detected