service.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 service
  15. import (
  16. "net"
  17. "net/http"
  18. "net/url"
  19. "os"
  20. "strconv"
  21. "github.com/gorilla/mux"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/util/signalutils"
  24. _ "yunion.io/x/sqlchemy/backends"
  25. api "yunion.io/x/onecloud/pkg/apis/webconsole"
  26. "yunion.io/x/onecloud/pkg/appsrv"
  27. "yunion.io/x/onecloud/pkg/cloudcommon"
  28. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  29. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  30. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  31. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  32. "yunion.io/x/onecloud/pkg/webconsole/models"
  33. o "yunion.io/x/onecloud/pkg/webconsole/options"
  34. "yunion.io/x/onecloud/pkg/webconsole/server"
  35. )
  36. func StartService() {
  37. opts := &o.Options
  38. commonOpts := &o.Options.CommonOptions
  39. common_options.ParseOptions(opts, os.Args, "webconsole.conf", api.SERVICE_TYPE)
  40. app_common.InitAuth(commonOpts, func() {
  41. log.Infof("Auth complete")
  42. })
  43. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange)
  44. if opts.ApiServer == "" {
  45. log.Fatalf("--api-server must specified")
  46. }
  47. _, err := url.Parse(opts.ApiServer)
  48. if err != nil {
  49. log.Fatalf("invalid --api-server %s", opts.ApiServer)
  50. }
  51. registerSigTraps()
  52. start()
  53. }
  54. func registerSigTraps() {
  55. signalutils.SetDumpStackSignal()
  56. signalutils.StartTrap()
  57. }
  58. func start() {
  59. baseOpts := &o.Options.BaseOptions
  60. // commonOpts := &o.Options.CommonOptions
  61. app := app_common.InitApp(baseOpts, true)
  62. dbOpts := &o.Options.DBOptions
  63. cloudcommon.InitDB(dbOpts)
  64. initHandlers(app, baseOpts.IsSlaveNode)
  65. db.EnsureAppSyncDB(app, dbOpts, models.InitDB)
  66. root := mux.NewRouter()
  67. root.UseEncodedPath()
  68. // api handler
  69. root.PathPrefix(ApiPathPrefix).Handler(app)
  70. srv := server.NewConnectionServer()
  71. // websocket command text console handler
  72. root.Handle(ConnectPathPrefix, srv)
  73. // websockify graphic console handler
  74. root.Handle(WebsockifyPathPrefix, srv)
  75. // websocketproxy handler
  76. root.Handle(WebsocketProxyPathPrefix, srv)
  77. // misc handler
  78. appsrv.AddMiscHandlersToMuxRouter(app, root, o.Options.EnableAppProfiling)
  79. if !baseOpts.IsSlaveNode {
  80. cron := cronman.InitCronJobManager(true, o.Options.CronJobWorkerCount, o.Options.TimeZone)
  81. cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false)
  82. cron.Start()
  83. defer cron.Stop()
  84. }
  85. addr := net.JoinHostPort(o.Options.Address, strconv.Itoa(o.Options.Port))
  86. log.Infof("Start listen on %s", addr)
  87. if o.Options.EnableSsl {
  88. srv := appsrv.InitHTTPServer(app, addr)
  89. srv.Handler = root
  90. err := srv.ListenAndServeTLS(o.Options.SslCertfile, o.Options.SslKeyfile)
  91. if err != nil && err != http.ErrServerClosed {
  92. log.Fatalf("%v", err)
  93. }
  94. } else {
  95. err := http.ListenAndServe(addr, root)
  96. if err != nil {
  97. log.Fatalf("%v", err)
  98. }
  99. }
  100. }