service.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "math/rand"
  17. "net"
  18. "os"
  19. "strconv"
  20. "time"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/apigateway/app"
  23. "yunion.io/x/onecloud/pkg/apigateway/clientman"
  24. "yunion.io/x/onecloud/pkg/apigateway/options"
  25. "yunion.io/x/onecloud/pkg/apigateway/report"
  26. api "yunion.io/x/onecloud/pkg/apis/apigateway"
  27. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  29. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  30. )
  31. func StartService() {
  32. options.Options = &options.GatewayOptions{}
  33. opts := options.Options
  34. baseOpts := &opts.BaseOptions
  35. commonOpts := &opts.CommonOptions
  36. common_options.ParseOptions(opts, os.Args, "apigateway.conf", api.SERVICE_TYPE)
  37. InitDefaultPolicy()
  38. app_common.InitAuth(commonOpts, func() {
  39. log.Infof("Auth complete.")
  40. })
  41. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange)
  42. if err := clientman.InitClient(); err != nil {
  43. log.Fatalf("Init client token manager: %v", err)
  44. }
  45. serviceApp := app.NewApp(app_common.InitApp(baseOpts, false))
  46. serviceApp.InitHandlers().Bind()
  47. // mods, jmods := modulebase.GetRegisterdModules()
  48. // log.Infof("Modules: %s", jsonutils.Marshal(mods).PrettyString())
  49. // log.Infof("Modules: %s", jsonutils.Marshal(jmods).PrettyString())
  50. if !options.Options.DisableReporting {
  51. cron := cronman.InitCronJobManager(true, 1, opts.TimeZone)
  52. rand.Seed(time.Now().Unix())
  53. cron.AddJobEveryFewDays("AutoReport", 1, rand.Intn(23), rand.Intn(59), 0, report.Report, true)
  54. go cron.Start()
  55. }
  56. listenAddr := net.JoinHostPort(options.Options.Address, strconv.Itoa(options.Options.Port))
  57. if opts.EnableSsl {
  58. serviceApp.ListenAndServeTLS(listenAddr, opts.SslCertfile, opts.SslKeyfile)
  59. } else {
  60. serviceApp.ListenAndServe(listenAddr)
  61. }
  62. }