2025-03-27 16:27:41 +08:00

36 lines
624 B
Go

package config
import (
"fmt"
"strconv"
)
// ServerConfig 服务器配置
type ServerConfig struct {
Port int `mapstructure:"port"`
Host string `mapstructure:"host"`
Env string `mapstructure:"env"` // development, production, testing
}
// Validate 验证服务器配置
func (c *ServerConfig) Validate() error {
if c.Port <= 0 {
return fmt.Errorf("server port must be positive")
}
if c.Host == "" {
c.Host = "0.0.0.0"
}
if c.Env == "" {
c.Env = "development"
}
return nil
}
// Address 返回服务器地址
func (c *ServerConfig) Address() string {
return c.Host + ":" + strconv.Itoa(c.Port)
}