handlers.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 appsrv
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "net/http/pprof"
  20. "github.com/gorilla/mux"
  21. "yunion.io/x/pkg/util/version"
  22. )
  23. type FilterHandler func(ctx context.Context, w http.ResponseWriter, r *http.Request)
  24. type TMiddleware func(handler FilterHandler) FilterHandler
  25. func VersionHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  26. fmt.Fprintf(w, "%s", version.GetShortString())
  27. }
  28. func PingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  29. fmt.Fprintf(w, "pong")
  30. }
  31. /*
  32. func CORSHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  33. reqHdrs, enableCors := r.Header["Access-Control-Request-Headers"]
  34. if enableCors {
  35. w.Header().Set("Access-Control-Allow-Origin", getRequestOrigin(r))
  36. allowHdrs := strings.Join(reqHdrs, ",")
  37. allowHdrs = fmt.Sprintf("%s,%s", allowHdrs, "Authorization")
  38. w.Header().Set("Vary", "Origin,Access-Control-Request-Method,Access-Control-Request-Headers")
  39. w.Header().Set("Access-Control-Allow-Headers", allowHdrs)
  40. w.Header().Set("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,PATCH,DELETE")
  41. w.Header().Set("Access-Control-Allow-Credentials", "true")
  42. w.Header().Set("Access-Control-Expose-Headers", allowHdrs)
  43. w.Header().Set("Access-Control-Max-Age", "86400")
  44. }
  45. }*/
  46. func addPProfHandler(prefix string, app *Application) {
  47. pp := "/debug/pprof"
  48. if prefix != "" {
  49. prefix = fmt.Sprintf("%s/%s", prefix, pp)
  50. } else {
  51. prefix = pp
  52. }
  53. app.AddHandler("GET", fmt.Sprintf("%s/", prefix), WhitelistFilter(profIndex)).SetProcessNoTimeout()
  54. app.AddHandler("GET", fmt.Sprintf("%s/cmdline", prefix), WhitelistFilter(profCmdline)).SetProcessNoTimeout()
  55. app.AddHandler("GET", fmt.Sprintf("%s/profile", prefix), WhitelistFilter(profProfile)).SetProcessNoTimeout()
  56. app.AddHandler("GET", fmt.Sprintf("%s/symbol", prefix), WhitelistFilter(profSymbol)).SetProcessNoTimeout()
  57. app.AddHandler("POST", fmt.Sprintf("%s/symbol", prefix), WhitelistFilter(profSymbol)).SetProcessNoTimeout()
  58. app.AddHandler("GET", fmt.Sprintf("%s/trace", prefix), WhitelistFilter(profTrace)).SetProcessNoTimeout()
  59. }
  60. func profIndex(_ context.Context, w http.ResponseWriter, r *http.Request) {
  61. pprof.Index(w, r)
  62. }
  63. func profCmdline(_ context.Context, w http.ResponseWriter, r *http.Request) {
  64. pprof.Cmdline(w, r)
  65. }
  66. func profProfile(_ context.Context, w http.ResponseWriter, r *http.Request) {
  67. pprof.Profile(w, r)
  68. }
  69. func profSymbol(_ context.Context, w http.ResponseWriter, r *http.Request) {
  70. pprof.Symbol(w, r)
  71. }
  72. func profTrace(_ context.Context, w http.ResponseWriter, r *http.Request) {
  73. pprof.Trace(w, r)
  74. }
  75. func AddMiscHandlersToMuxRouter(app *Application, root *mux.Router, enableProfiling bool) {
  76. adapterF := func(appHandleFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
  77. return func(w http.ResponseWriter, r *http.Request) {
  78. appHandleFunc(app.GetContext(), w, r)
  79. }
  80. }
  81. root.HandleFunc("/version", adapterF(VersionHandler))
  82. root.HandleFunc("/stats", adapterF(StatisticHandler))
  83. root.HandleFunc("/ping", adapterF(PingHandler))
  84. root.HandleFunc("/worker_stats", adapterF(WorkerStatsHandler))
  85. if enableProfiling {
  86. pp := "/debug/pprof"
  87. ppPath := func(sufix string) string {
  88. return fmt.Sprintf("%s/%s", pp, sufix)
  89. }
  90. root.HandleFunc(ppPath(""), pprof.Index)
  91. root.HandleFunc(ppPath("cmdline"), pprof.Cmdline)
  92. root.HandleFunc(ppPath("profile"), pprof.Profile)
  93. root.HandleFunc(ppPath("symbol"), pprof.Symbol)
  94. root.HandleFunc(ppPath("trace"), pprof.Trace)
  95. }
  96. }