site_config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package model
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. type HealthCheckConfig struct {
  7. // Protocol settings
  8. Protocol string `json:"protocol"` // http, https, grpc
  9. Method string `json:"method"` // GET, POST, PUT, etc.
  10. Path string `json:"path"` // URL path to check
  11. Headers map[string]string `json:"headers" gorm:"serializer:json"` // Custom headers
  12. Body string `json:"body"` // Request body for POST/PUT
  13. // Response validation
  14. ExpectedStatus []int `json:"expected_status" gorm:"serializer:json"` // Expected HTTP status codes
  15. ExpectedText string `json:"expected_text"` // Text that should be present in response
  16. NotExpectedText string `json:"not_expected_text"` // Text that should NOT be present
  17. ValidateSSL bool `json:"validate_ssl"` // Validate SSL certificate
  18. // GRPC specific settings
  19. GRPCService string `json:"grpc_service"` // GRPC service name
  20. GRPCMethod string `json:"grpc_method"` // GRPC method name
  21. // Advanced settings
  22. DNSResolver string `json:"dns_resolver"` // Custom DNS resolver
  23. SourceIP string `json:"source_ip"` // Source IP for requests
  24. VerifyHostname bool `json:"verify_hostname"` // Verify hostname in SSL cert
  25. ClientCert string `json:"client_cert"` // Client certificate path
  26. ClientKey string `json:"client_key"` // Client key path
  27. }
  28. type SiteConfig struct {
  29. Model
  30. Host string `gorm:"index" json:"host"` // host:port format
  31. Port int `gorm:"index" json:"port"` // port number
  32. Scheme string `gorm:"default:'http'" json:"scheme"` // http, https, grpc, grpcs
  33. DisplayURL string `json:"display_url"` // computed URL for display
  34. CustomOrder int `gorm:"default:0" json:"custom_order"`
  35. HealthCheckEnabled bool `gorm:"default:true" json:"health_check_enabled"`
  36. CheckInterval int `gorm:"default:300" json:"check_interval"` // seconds
  37. Timeout int `gorm:"default:10" json:"timeout"` // seconds
  38. UserAgent string `gorm:"default:'Nginx-UI Site Checker/1.0'" json:"user_agent"`
  39. MaxRedirects int `gorm:"default:3" json:"max_redirects"`
  40. FollowRedirects bool `gorm:"default:true" json:"follow_redirects"`
  41. CheckFavicon bool `gorm:"default:true" json:"check_favicon"`
  42. HealthCheckConfig *HealthCheckConfig `gorm:"serializer:json" json:"health_check_config"`
  43. }
  44. // GetURL returns the computed URL for this site config
  45. func (sc *SiteConfig) GetURL() string {
  46. if sc.DisplayURL != "" {
  47. return sc.DisplayURL
  48. }
  49. return sc.Scheme + "://" + sc.Host
  50. }
  51. // SetFromURL parses a URL and sets the Host, Port, and Scheme fields
  52. func (sc *SiteConfig) SetFromURL(url string) error {
  53. // Parse URL to extract host, port, and scheme
  54. // This is a simplified implementation - you may want to use net/url package
  55. if url == "" {
  56. return nil
  57. }
  58. // Store the original URL as display URL for backward compatibility
  59. sc.DisplayURL = url
  60. // Extract scheme
  61. if strings.HasPrefix(url, "https://") {
  62. sc.Scheme = "https"
  63. url = strings.TrimPrefix(url, "https://")
  64. } else if strings.HasPrefix(url, "http://") {
  65. sc.Scheme = "http"
  66. url = strings.TrimPrefix(url, "http://")
  67. } else if strings.HasPrefix(url, "grpcs://") {
  68. sc.Scheme = "grpcs"
  69. url = strings.TrimPrefix(url, "grpcs://")
  70. } else if strings.HasPrefix(url, "grpc://") {
  71. sc.Scheme = "grpc"
  72. url = strings.TrimPrefix(url, "grpc://")
  73. } else {
  74. sc.Scheme = "http" // default
  75. }
  76. // Extract host and port
  77. if strings.Contains(url, "/") {
  78. url = strings.Split(url, "/")[0]
  79. }
  80. if strings.Contains(url, ":") {
  81. parts := strings.Split(url, ":")
  82. sc.Host = parts[0] + ":" + parts[1]
  83. if len(parts) > 1 {
  84. if port, err := strconv.Atoi(parts[1]); err == nil {
  85. sc.Port = port
  86. }
  87. }
  88. } else {
  89. sc.Host = url + ":80" // default port
  90. sc.Port = 80
  91. if sc.Scheme == "https" || sc.Scheme == "grpcs" {
  92. sc.Host = url + ":443"
  93. sc.Port = 443
  94. }
  95. }
  96. return nil
  97. }