Documentation
¶
Overview ¶
Package define defines constants and data structures in the application. Includes default configuration values, timeout durations, limit parameters and other constant definitions.
Index ¶
Constants ¶
const ( PATH_HEALTH = "/health" PATH_HEALTHCHECK = "/healthcheck" PATH_METRICS = "/metrics" )
HTTP path constants. Used for route registration, rate-limit skip paths, and access-log skip paths.
const ( // DEFAULT_TASK_INTERVAL default task interval (seconds) DEFAULT_TASK_INTERVAL = 5 // 5s // DEFAULT_TIMEOUT default timeout (seconds) DEFAULT_TIMEOUT = 5 // DEFAULT_LOCK_TIME default lock expiration time (seconds) DEFAULT_LOCK_TIME = 15 // DEFAULT_MODE default mode DEFAULT_MODE = "DEFAULT" // 1: 2: 3: 4: // RATE_LIMIT_CLEANUP_INTERVAL rate limiter cleanup interval RATE_LIMIT_CLEANUP_INTERVAL = 1 * time.Minute // DEFAULT_PAGE_SIZE default page size DEFAULT_PAGE_SIZE = 100 // MAX_PAGE_SIZE maximum page size MAX_PAGE_SIZE = 1000 // MAX_IDENTIFIER_LENGTH maximum length for user query params (phone/mail/user_id), prevents DoS and log/cache bloat MAX_IDENTIFIER_LENGTH = 512 // MAX_HEADER_BYTES maximum request header size (1MB) MAX_HEADER_BYTES = 1 << 20 // MAX_REQUEST_BODY_SIZE maximum request body size (10KB) MAX_REQUEST_BODY_SIZE = 10 * 1024 // MAX_JSON_SIZE maximum JSON response body size (10MB), prevents memory exhaustion attacks MAX_JSON_SIZE = 10 * 1024 * 1024 // SHUTDOWN_TIMEOUT graceful shutdown timeout SHUTDOWN_TIMEOUT = 5 * time.Second // HTTP_RETRY_MAX_RETRIES HTTP request maximum retry count HTTP_RETRY_MAX_RETRIES = 3 // HTTP_RETRY_DELAY HTTP request retry delay HTTP_RETRY_DELAY = 1 * time.Second // IDLE_TIMEOUT HTTP connection idle timeout IDLE_TIMEOUT = 120 * time.Second // REDIS_CONNECTION_TIMEOUT Redis connection timeout REDIS_CONNECTION_TIMEOUT = 5 * time.Second // DEFAULT_RATE_LIMIT default rate limit: requests per minute DEFAULT_RATE_LIMIT = 60 // DEFAULT_RATE_LIMIT_WINDOW default rate limit time window DEFAULT_RATE_LIMIT_WINDOW = 1 * time.Minute // MAX_VISITORS_MAP_SIZE maximum visitors map size, prevents memory leaks MAX_VISITORS_MAP_SIZE = 10000 // MAX_WHITELIST_SIZE maximum whitelist size MAX_WHITELIST_SIZE = 1000 // DEFAULT_MAX_IDLE_CONNS default maximum idle connections DEFAULT_MAX_IDLE_CONNS = 100 // DEFAULT_MAX_IDLE_CONNS_PER_HOST default maximum idle connections per host DEFAULT_MAX_IDLE_CONNS_PER_HOST = 10 // DEFAULT_IDLE_CONN_TIMEOUT default idle connection timeout DEFAULT_IDLE_CONN_TIMEOUT = 90 * time.Second // DEFAULT_LOAD_DATA_TIMEOUT default data loading timeout DEFAULT_LOAD_DATA_TIMEOUT = 30 * time.Second // DEFAULT_SLICE_POOL_CAPACITY default slice pool capacity DEFAULT_SLICE_POOL_CAPACITY = 100 // SMALL_DATA_THRESHOLD small data threshold, data smaller than this is processed directly without using buffer pool SMALL_DATA_THRESHOLD = 100 // LARGE_DATA_THRESHOLD large data threshold, data larger than this uses streaming JSON encoding LARGE_DATA_THRESHOLD = 10000 // REDIS_RETRY_MAX_RETRIES Redis operation maximum retry count REDIS_RETRY_MAX_RETRIES = 3 // REDIS_RETRY_DELAY Redis operation retry delay REDIS_RETRY_DELAY = 1 * time.Second )
const ( // WARN_RULE_NOT_FOUND rules file not found // Note: This constant is deprecated, please use i18n.T(r, "log.data_file_not_found") instead WARN_RULE_NOT_FOUND = "没有找到规则文件" // WARN_READ_RULE_ERR error reading rules file // Note: This constant is deprecated, please use i18n.T(r, "error.data_load_failed") instead WARN_READ_RULE_ERR = "读取规则文件遇到错误" // WARN_PARSE_RULE_ERR error parsing rules file // Note: This constant is deprecated, please use i18n.T(r, "error.data_parse_failed") instead WARN_PARSE_RULE_ERR = "解析规则文件遇到错误" // ERROR_CAN_NOT_OPEN_RULE error opening rules file // Note: This constant is deprecated, please use i18n.T(r, "error.data_load_failed") instead ERROR_CAN_NOT_OPEN_RULE = "读取规则文件出错" // ERR_REQ_INIT_FAILED network request component initialization failed // Note: This constant is deprecated, please use i18n.T(r, "error.http_request_failed") instead ERR_REQ_INIT_FAILED = "网络请求组件初始化失败" // ERR_GET_CONFIG_FAILED failed to get remote configuration // Note: This constant is deprecated, please use i18n.T(r, "error.config_load_failed") instead ERR_GET_CONFIG_FAILED = "获取远程配置失败" // ERR_READ_CONFIG_FAILED failed to read remote configuration // Note: This constant is deprecated, please use i18n.T(r, "error.config_load_failed") instead ERR_READ_CONFIG_FAILED = "读取远程配置失败" // ERR_PARSE_CONFIG_FAILED failed to parse remote configuration // Note: This constant is deprecated, please use i18n.T(r, "error.config_parse_failed") instead ERR_PARSE_CONFIG_FAILED = "解析远程配置失败" // WARN_GET_REMOTE_FAILED_FALLBACK_LOCAL error getting remote rules, only using local rules // Note: This constant is deprecated, please use i18n.T(r, "log.all_sources_failed") instead WARN_GET_REMOTE_FAILED_FALLBACK_LOCAL = "获取远程规则出错,仅使用本地规则" // INFO_REQ_REMOTE_API request data API // Note: This constant is deprecated, please use i18n.T(r, "log.request_data_api") instead INFO_REQ_REMOTE_API = "请求数据接口 🎩" )
const DEFAULT_DATA_FILE = "./data.json"
DEFAULT_DATA_FILE default local user data file path
const DEFAULT_PORT = 8081
DEFAULT_PORT default port number
const DEFAULT_REDIS = "localhost:6379"
DEFAULT_REDIS default Redis address
const DEFAULT_REMOTE_CONFIG = "http://localhost:8080/data.json"
DEFAULT_REMOTE_CONFIG default remote configuration address
const DEFAULT_REMOTE_KEY = ""
DEFAULT_REMOTE_KEY default remote configuration key
Variables ¶
var SkipPathsHealthAndMetrics = []string{PATH_HEALTH, PATH_HEALTHCHECK, PATH_METRICS}
SkipPathsHealthAndMetrics is the path list to skip for rate limiting and access logging. Reduces log noise and keeps health/metrics probes from consuming rate limit.
Functions ¶
func ParseTrustedProxyIPs ¶ added in v0.8.0
ParseTrustedProxyIPs parses comma-separated string (e.g. TRUSTED_PROXY_IPS env), trims each element, drops empty. Used by main and ip_whitelist so env parsing is explicit and consistent.
Types ¶
type AllowListUser ¶
type AllowListUser struct {
Phone string `json:"phone"` // User phone number (optional if Mail is set)
Mail string `json:"mail"` // User email address (optional if Phone is set)
UserID string `json:"user_id"` // User unique identifier (optional, auto-generated if not provided)
Status string `json:"status"` // User status (e.g., "active", "inactive", "suspended")
Scope []string `json:"scope"` // User permission scope (optional)
Role string `json:"role"` // User role (optional)
DingtalkUserID string `json:"dingtalk_userid,omitempty"` // DingTalk user ID for work notification (optional)
}
AllowListUser represents user information in the allow list.
This struct is used to store user's basic information, including phone number, email address, user ID, status, etc. This information is used to verify and authorize user access.
At least one of Phone or Mail must be non-empty. Email-only users are supported (Phone may be empty).
func (*AllowListUser) IsActive ¶ added in v0.3.0
func (u *AllowListUser) IsActive() bool
IsActive checks if the user status is active.
Returns true if the user status is "active", false otherwise. This method is used to verify if a user is allowed to access the system.
func (*AllowListUser) IsValid ¶ added in v0.3.0
func (u *AllowListUser) IsValid() bool
IsValid checks if the user has a valid status for authentication.
Returns true if the user status is one of the valid statuses (currently only "active"). This method can be extended in the future to support other valid statuses if needed.
func (*AllowListUser) Normalize ¶
func (u *AllowListUser) Normalize()
Normalize normalizes user data, sets default values and generates user_id (if not provided)
This function will: - If user_id is empty, generate based on phone or mail - If status is empty, set to "active" - If scope is nil, set to empty array - If role is empty, set to empty string