service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "os"
  17. "yunion.io/x/log"
  18. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  19. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  20. "yunion.io/x/onecloud/pkg/mcp-server/options"
  21. "yunion.io/x/onecloud/pkg/mcp-server/server"
  22. )
  23. func StartService() {
  24. opts := &options.Options
  25. common_options.ParseOptions(opts, os.Args, "mcpserver.conf", "mcpserver")
  26. // 如果配置了认证信息,初始化 auth manager
  27. commonOpts := &opts.CommonOptions
  28. // 只有当所有必需的认证配置都存在时,才初始化 auth manager
  29. if len(commonOpts.AuthURL) > 0 && len(commonOpts.AdminUser) > 0 &&
  30. len(commonOpts.AdminPassword) > 0 && len(commonOpts.AdminProject) > 0 {
  31. app_common.InitAuth(commonOpts, func() {
  32. log.Infof("Auth complete!!")
  33. })
  34. } else {
  35. log.Infof("Auth configuration incomplete, skipping auth initialization. AuthURL: %s, AdminUser: %s, AdminPassword: %s, AdminProject: %s", commonOpts.AuthURL, commonOpts.AdminUser, commonOpts.AdminPassword, commonOpts.AdminProject)
  36. }
  37. // 创建服务器
  38. srv := server.NewServer()
  39. // 初始化服务器
  40. if err := srv.Initialize(); err != nil {
  41. log.Fatalf("Fail to init mcp server: %s", err)
  42. }
  43. // 启动服务器
  44. if err := srv.Start(); err != nil {
  45. log.Fatalf("Fail to start mcp server: %s", err)
  46. }
  47. }