service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "context"
  17. "os"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. _ "yunion.io/x/sqlchemy/backends"
  21. "yunion.io/x/onecloud/pkg/cloudcommon"
  22. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  26. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  27. "yunion.io/x/onecloud/pkg/cloudnet/models"
  28. "yunion.io/x/onecloud/pkg/cloudnet/options"
  29. "yunion.io/x/onecloud/pkg/mcclient/auth"
  30. )
  31. func StartService() {
  32. opts := &options.Options
  33. common_options.ParseOptions(opts, os.Args, "cloudnet.conf", "cloudnet")
  34. commonOpts := &opts.CommonOptions
  35. app_common.InitAuth(commonOpts, func() {
  36. log.Infof("Auth complete")
  37. })
  38. dbOpts := &opts.DBOptions
  39. baseOpts := &opts.BaseOptions
  40. app := app_common.InitApp(baseOpts, true).
  41. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  42. ctx := context.Background()
  43. session := auth.GetAdminSession(ctx, commonOpts.Region)
  44. notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
  45. })
  46. cloudcommon.InitDB(dbOpts)
  47. InitHandlers(app, opts.IsSlaveNode)
  48. db.EnsureAppSyncDB(app, dbOpts, models.InitDB)
  49. defer cloudcommon.CloseDB()
  50. app_common.ServeForever(app, baseOpts)
  51. }