Capstan is a Golang web framework that shares some similarities with others in its segment.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4KB

  1. package capstan
  2. import (
  3. "net/http"
  4. pth "path"
  5. "git.destrealm.org/go/capstan/render"
  6. "github.com/go-chi/chi"
  7. )
  8. type RouterGroup struct {
  9. name string
  10. basePath string
  11. parent *Router
  12. mux chi.Router
  13. middleware chi.Middlewares
  14. headers http.Header
  15. options *RouterOptions
  16. renderer render.Renderer
  17. bindFuncs []func(*Router)
  18. router *Router
  19. }
  20. func NewRouterGroup(path string, parent *Router) *RouterGroup {
  21. rg := &RouterGroup{
  22. headers: http.Header{},
  23. parent: parent,
  24. bindFuncs: make([]func(*Router), 0),
  25. }
  26. if path == "" {
  27. path = "/"
  28. }
  29. router := NewEmptyRouter()
  30. router.app = parent.app
  31. router.dependencies = rg.parent.dependencies
  32. router.urls = rg.parent.urls
  33. router.mux = chi.NewRouter()
  34. router.group = rg
  35. router.routes = rg.parent.routes
  36. router.name = rg.name
  37. router.renderer = rg.parent.renderer
  38. router.afterResponse = make([]func(Context, error) error,
  39. len(rg.parent.afterResponse))
  40. router.beforeResponse = make([]func(Context, *Route) error,
  41. len(rg.parent.beforeResponse))
  42. router.reserved = rg.parent.reserved
  43. copy(router.afterResponse, parent.afterResponse)
  44. copy(router.beforeResponse, parent.beforeResponse)
  45. rg.router = router
  46. rg.basePath = pth.Join(parent.basePath, path)
  47. return rg
  48. }
  49. func (rg *RouterGroup) Add(fn func(*Router)) {
  50. rg.bindFuncs = append(rg.bindFuncs, fn)
  51. // Before and after response funcs are always copied from the parent.
  52. if len(rg.middleware) > 0 {
  53. rg.mux.Use(rg.middleware...)
  54. }
  55. fn(rg.router)
  56. rg.parent.mux.Mount(rg.basePath, rg.router.mux)
  57. }
  58. func (rg *RouterGroup) Header() http.Header {
  59. return rg.headers
  60. }
  61. func (rg *RouterGroup) Middleware(middleware chi.Middlewares) {
  62. rg.middleware = middleware
  63. }
  64. func (rg *RouterGroup) Name(name string) *RouterGroup {
  65. rg.name = name
  66. return rg
  67. }
  68. func (rg *RouterGroup) Options() *RouterOptions {
  69. rg.options = &RouterOptions{}
  70. return rg.options
  71. }
  72. func (rg *RouterGroup) Rebase(path string) {
  73. rg.basePath = pth.Join(rg.parent.basePath, path)
  74. }
  75. func (rg *RouterGroup) Rebind() chi.Router {
  76. return nil
  77. }
  78. func (rg *RouterGroup) RebindSelf() {
  79. rg.mux = chi.NewRouter()
  80. rg.mux.Use(rg.middleware...)
  81. for _, fn := range rg.bindFuncs {
  82. fn(rg.router)
  83. }
  84. rg.parent.mux.Mount(rg.basePath, rg.router.mux)
  85. }
  86. func (rg *RouterGroup) ReplacePath(from, to string) {
  87. }
  88. func (rg *RouterGroup) WithRenderer(renderer render.Renderer) *RouterGroup {
  89. rg.renderer = renderer
  90. return rg
  91. }