random-api-go/config/config.go
wood chen 80d18f2b88 feat(config, handlers, middleware): enhance configuration and API request handling
- Added performance settings to the configuration, including max concurrent requests and caching options.
- Updated API request handling to use context for timeouts and improved logging with Unix timestamps.
- Introduced rate limiting middleware to manage request load effectively.
- Enhanced metrics logging to include atomic counters for request counts and improved data structure for performance metrics.
- Implemented caching for CSV content to optimize data retrieval and reduce load times.
2024-12-01 00:14:21 +08:00

142 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
)
const (
EnvBaseURL = "BASE_URL"
DefaultPort = ":5003"
RequestTimeout = 10 * time.Second
)
type Config struct {
Server struct {
Port string `json:"port"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
MaxHeaderBytes int `json:"max_header_bytes"`
} `json:"server"`
Storage struct {
DataDir string `json:"data_dir"`
StatsFile string `json:"stats_file"`
LogFile string `json:"log_file"`
} `json:"storage"`
API struct {
BaseURL string `json:"base_url"`
RequestTimeout time.Duration `json:"request_timeout"`
} `json:"api"`
Performance struct {
MaxConcurrentRequests int `json:"max_concurrent_requests"`
RequestTimeout time.Duration `json:"request_timeout"`
CacheTTL time.Duration `json:"cache_ttl"`
EnableCompression bool `json:"enable_compression"`
} `json:"performance"`
}
var (
cfg Config
RNG *rand.Rand
)
func Load(configFile string) error {
// 尝试创建配置目录
configDir := filepath.Dir(configFile)
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
// 检查配置文件是否存在
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// 创建默认配置
defaultConfig := Config{
Server: struct {
Port string `json:"port"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
MaxHeaderBytes int `json:"max_header_bytes"`
}{
Port: ":5003",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
},
Storage: struct {
DataDir string `json:"data_dir"`
StatsFile string `json:"stats_file"`
LogFile string `json:"log_file"`
}{
DataDir: "/root/data",
StatsFile: "/root/data/stats.json",
LogFile: "/root/data/logs/server.log",
},
API: struct {
BaseURL string `json:"base_url"`
RequestTimeout time.Duration `json:"request_timeout"`
}{
BaseURL: "",
RequestTimeout: 10 * time.Second,
},
Performance: struct {
MaxConcurrentRequests int `json:"max_concurrent_requests"`
RequestTimeout time.Duration `json:"request_timeout"`
CacheTTL time.Duration `json:"cache_ttl"`
EnableCompression bool `json:"enable_compression"`
}{
MaxConcurrentRequests: 100,
RequestTimeout: 10 * time.Second,
CacheTTL: 1 * time.Hour,
EnableCompression: true,
},
}
// 将默认配置写入文件
data, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal default config: %w", err)
}
if err := os.WriteFile(configFile, data, 0644); err != nil {
return fmt.Errorf("failed to write default config: %w", err)
}
cfg = defaultConfig
return nil
}
// 读取现有配置文件
file, err := os.Open(configFile)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return err
}
// 如果环境变量设置了 BASE_URL则覆盖配置文件中的设置
if envBaseURL := os.Getenv(EnvBaseURL); envBaseURL != "" {
cfg.API.BaseURL = envBaseURL
}
return nil
}
func Get() *Config {
return &cfg
}
func InitRNG(r *rand.Rand) {
RNG = r
}