Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mcp_config.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // subconfigs/service_config.go
  2. package subconfigs
  3. import (
  4. "fmt"
  5. "log"
  6. "git.x2erp.com/qdy/go-base/config/core"
  7. )
  8. // ServiceConfig 数据库配置
  9. type McpServiceConfig struct {
  10. core.BaseConfig
  11. Port int `yaml:"port" desc:"服务监听端口"`
  12. ServiceName string `yaml:"service_name" desc:"服务名称"`
  13. InstanceName string `yaml:"instance_name" desc:"服务实例名称"`
  14. ReadTimeout int `yaml:"read_timeout" desc:"读取超时时间(秒)"`
  15. WriteTimeout int `yaml:"write_timeout" desc:"写入超时时间(秒)"`
  16. IdleTimeout int `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
  17. }
  18. func (c *McpServiceConfig) Description() string {
  19. return "MCP(Model Context Protocol)服务配置"
  20. }
  21. // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
  22. func (c *McpServiceConfig) SetDefaults() {
  23. if c.Port == 0 {
  24. c.Port = 8080
  25. }
  26. if c.IdleTimeout == 0 {
  27. c.IdleTimeout = 60
  28. }
  29. if c.ReadTimeout == 0 {
  30. c.ReadTimeout = 30
  31. }
  32. if c.WriteTimeout == 0 {
  33. c.WriteTimeout = 30
  34. }
  35. }
  36. // Load 从yaml数据加载 - 实现 ConfigLoader 接口
  37. func (c *McpServiceConfig) Load(data map[string]interface{}) error {
  38. // 虽然可能不会被直接调用,但为了接口完整性还是要实现
  39. return c.LoadFromYAML(data, c)
  40. }
  41. // ========== 业务方法 ==========
  42. // Validate 验证配置(数据库配置)
  43. func (c *McpServiceConfig) Validate() error {
  44. if c.Port <= 0 || c.Port > 65535 {
  45. return fmt.Errorf("invalid service port: %d", c.Port)
  46. }
  47. return nil
  48. }
  49. // IsConfigured 判断是否已配置(数据库配置)
  50. func (c *McpServiceConfig) IsConfigured() bool {
  51. if c.Port <= 0 {
  52. log.Println("⚠️ 警告: 微服务 Port 未配置或无效")
  53. return false
  54. }
  55. return true
  56. }
  57. // 自动注册
  58. func init() {
  59. core.Register("mcpservice", &McpServiceConfig{})
  60. }