app.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 app
  15. import (
  16. "yunion.io/x/log"
  17. "yunion.io/x/onecloud/pkg/apigateway/handler"
  18. "yunion.io/x/onecloud/pkg/apigateway/options"
  19. "yunion.io/x/onecloud/pkg/apis/cloudid"
  20. "yunion.io/x/onecloud/pkg/appsrv"
  21. )
  22. type Application struct {
  23. *appsrv.Application
  24. AuthHandler handler.IHandler
  25. MiscHandler handler.IHandler
  26. K8sHandler handler.IHandler
  27. ResourceHandler handler.IHandler
  28. CSRFResourceHandler handler.IHandler
  29. RPCHandler handler.IHandler
  30. CloudIdSAMLHandler handler.IHandler
  31. BackendServiceProxyHandler handler.IHandler
  32. }
  33. func NewApp(app *appsrv.Application) *Application {
  34. svcApp := &Application{
  35. Application: app,
  36. }
  37. return svcApp
  38. }
  39. func (app *Application) InitHandlers() *Application {
  40. // bind auth handlers
  41. app.AuthHandler = handler.NewAuthHandlers("/api/v1/auth", nil)
  42. // bind misc handlers
  43. app.MiscHandler = handler.NewMiscHandler("/api/v1/")
  44. // bind k8s resource handlers
  45. app.K8sHandler = handler.NewK8sResourceHandler("/api/v1/_raw")
  46. // bind restful resource handlers
  47. app.ResourceHandler = handler.NewResourceHandlers("/api").
  48. AddGet(handler.FetchAuthToken).
  49. AddPost(handler.FetchAuthToken).
  50. AddPut(handler.FetchAuthToken).
  51. AddPatch(handler.FetchAuthToken).
  52. AddDelete(handler.FetchAuthToken)
  53. // bind csrf handler
  54. app.CSRFResourceHandler = handler.NewCSRFResourceHandler("/api")
  55. // bind rpc handler
  56. app.RPCHandler = handler.NewRPCHandlers("/api").
  57. AddGet(handler.FetchAuthToken).
  58. AddPost(handler.FetchAuthToken)
  59. if options.Options.EnableBackendServiceProxy {
  60. // bind backend service API proxy
  61. log.Infof("enable backend service proxy")
  62. app.BackendServiceProxyHandler = handler.NewBackendServiceProxyHandler("/api/s/<service>", options.Options.BackendProxyReadWorkerCount, options.Options.BackendProxyWriteWorkerCount)
  63. }
  64. app.CloudIdSAMLHandler = handler.NewProxyHandlerWithService(cloudid.SAML_IDP_PREFIX, cloudid.SERVICE_TYPE)
  65. return app
  66. }
  67. func (app *Application) Bind() {
  68. for _, h := range []handler.IHandler{
  69. app.BackendServiceProxyHandler,
  70. app.CloudIdSAMLHandler,
  71. app.MiscHandler,
  72. app.AuthHandler,
  73. app.K8sHandler,
  74. app.RPCHandler,
  75. app.ResourceHandler,
  76. app.CSRFResourceHandler,
  77. } {
  78. if h != nil {
  79. h.Bind(app.Application)
  80. }
  81. }
  82. }