dispatcher.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handler
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "path"
  20. "strings"
  21. "time"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. )
  24. type handleFunc func(context.Context, http.ResponseWriter, *http.Request)
  25. type SHandler struct {
  26. Method string
  27. Prefix string
  28. MiddlewareFunc appsrv.MiddlewareFunc
  29. HandleFunc handleFunc
  30. }
  31. func NewHandler(method string, mf appsrv.MiddlewareFunc, hf handleFunc, paths ...string) SHandler {
  32. h := SHandler{
  33. Method: method,
  34. MiddlewareFunc: mf,
  35. HandleFunc: hf,
  36. }
  37. h.Prefix = path.Join(paths...)
  38. return h
  39. }
  40. func (h SHandler) indexKey() string {
  41. return fmt.Sprintf("%s-%s", h.Method, h.Prefix)
  42. }
  43. func (h SHandler) Bind(app *appsrv.Application) {
  44. f := h.HandleFunc
  45. if h.MiddlewareFunc != nil {
  46. f = h.MiddlewareFunc(f)
  47. }
  48. segs := appsrv.SplitPath(h.Prefix)
  49. hi := appsrv.NewHandlerInfo(h.Method, segs, f, nil, "", nil)
  50. if h.Method == "GET" {
  51. hi.SetProcessTimeoutCallback(func(hdr *appsrv.SHandlerInfo, r *http.Request) time.Duration {
  52. if len(r.URL.Query().Get("export_keys")) > 0 ||
  53. r.URL.Query().Has("force_no_paging") ||
  54. strings.HasSuffix(r.URL.Path, "/splitable-export") {
  55. return time.Hour * 2
  56. }
  57. return -time.Second
  58. })
  59. }
  60. app.AddHandler3(hi)
  61. }
  62. func NewMethodHandlerFactory(method string, mf appsrv.MiddlewareFunc, prefix string) func(handleFunc, ...string) SHandler {
  63. return func(hf handleFunc, paths ...string) SHandler {
  64. pPaths := []string{prefix}
  65. pPaths = append(pPaths, paths...)
  66. return NewHandler(method, mf, hf, pPaths...)
  67. }
  68. }
  69. type SHandlers struct {
  70. prefix string
  71. handlers map[string]SHandler
  72. }
  73. func NewHandlers(prefix string) *SHandlers {
  74. return &SHandlers{
  75. prefix: prefix,
  76. handlers: make(map[string]SHandler),
  77. }
  78. }
  79. type HandlerPath struct {
  80. f handleFunc
  81. paths []string
  82. }
  83. func (f *SHandlers) GetPrefix() string {
  84. return f.prefix
  85. }
  86. func (f *SHandlers) AddByMethod(method string, mf appsrv.MiddlewareFunc, hs ...HandlerPath) *SHandlers {
  87. newH := NewMethodHandlerFactory(method, mf, f.prefix)
  88. for _, h := range hs {
  89. hd := newH(h.f, h.paths...)
  90. idx := hd.indexKey()
  91. f.handlers[idx] = hd
  92. }
  93. return f
  94. }
  95. func (f *SHandlers) Bind(app *appsrv.Application) {
  96. for _, h := range f.handlers {
  97. h.Bind(app)
  98. }
  99. }
  100. func NewHP(f handleFunc, paths ...string) HandlerPath {
  101. return HandlerPath{f, paths}
  102. }
  103. type IHandler interface {
  104. Bind(*appsrv.Application)
  105. }