app.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "net"
  17. "os"
  18. "strconv"
  19. "time"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  23. "yunion.io/x/onecloud/pkg/util/seclib2"
  24. )
  25. func InitApp(options *common_options.BaseOptions, dbAccess bool) *appsrv.Application {
  26. // cache := appsrv.NewCache(options.AuthTokenCacheSize)
  27. log.Infof("RequestWorkerCount: %d", options.RequestWorkerCount)
  28. app := appsrv.NewApplication(options.ApplicationID, options.RequestWorkerCount, options.RequestWorkerQueueSize, dbAccess)
  29. app.CORSAllowHosts(options.CorsHosts)
  30. app.SetDefaultTimeout(time.Duration(options.DefaultProcessTimeoutSeconds) * time.Second)
  31. // app.SetContext(appsrv.APP_CONTEXT_KEY_CACHE, cache)
  32. // if dbConn != nil {
  33. // app.SetContext(appsrv.APP_CONTEXT_KEY_DB, dbConn)
  34. //}
  35. appsrv.SetDefaultHandlersWhitelistUserAgents(options.DefaultHandlersWhitelistUserAgents)
  36. if options.EnableAppProfiling {
  37. app.EnableProfiling()
  38. }
  39. if options.AllowTLS1x {
  40. app.AllowTLS1x()
  41. }
  42. return app
  43. }
  44. func ServeForever(app *appsrv.Application, options *common_options.BaseOptions) {
  45. ServeForeverWithCleanup(app, options, nil)
  46. }
  47. func ServeForeverWithCleanup(app *appsrv.Application, options *common_options.BaseOptions, onStop func()) {
  48. ServeForeverExtended(app, options, options.Port, onStop, true)
  49. }
  50. func ServeForeverExtended(app *appsrv.Application, options *common_options.BaseOptions, port int, onStop func(), isMaster bool) {
  51. addr := net.JoinHostPort(options.Address, strconv.Itoa(port))
  52. proto := "http"
  53. if options.EnableSsl {
  54. proto = "https"
  55. }
  56. log.Infof("Start listen on %s://%s, isMaster: %v", proto, addr, isMaster)
  57. var certfile string
  58. var sslfile string
  59. if options.EnableSsl {
  60. certfile = options.SslCertfile
  61. if len(options.SslCaCerts) > 0 {
  62. var err error
  63. certfile, err = seclib2.MergeCaCertFiles(options.SslCaCerts, options.SslCertfile)
  64. if err != nil {
  65. log.Fatalf("fail to merge ca+cert content: %s", err)
  66. }
  67. defer os.Remove(certfile)
  68. }
  69. if len(certfile) == 0 {
  70. log.Fatalf("Missing ssl-certfile")
  71. }
  72. if len(options.SslKeyfile) == 0 {
  73. log.Fatalf("Missing ssl-keyfile")
  74. }
  75. sslfile = options.SslKeyfile
  76. }
  77. app.ListenAndServeTLSWithCleanup2(addr, certfile, sslfile, onStop, isMaster)
  78. }