精简项目:只保留 docker_logs + health + info 三个工具
- 移除 database/middleware 等复杂模块,先聚焦核心功能 - docker_logs:封装 docker logs --tail N <container> - 修复 go.mod 版本为 1.24 匹配 Docker 构建镜像 - Dockerfile 改用 alpine + docker-cli,避免 scratch 无 shell 问题 - docker-compose.yml 简化为单服务定义 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+10
-57
@@ -1,81 +1,34 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Env string `mapstructure:"env"`
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Databases map[string]DatabaseConf `mapstructure:"databases"`
|
||||
Docker DockerConfig `mapstructure:"docker"`
|
||||
Auth AuthConfig `mapstructure:"auth"`
|
||||
RateLimit RateLimitConfig `mapstructure:"rate_limit"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Transport string `mapstructure:"transport"` // stdio | sse
|
||||
Addr string `mapstructure:"addr"` // :8080 for SSE mode
|
||||
}
|
||||
|
||||
type DatabaseConf struct {
|
||||
Driver string `mapstructure:"driver"` // postgres | mysql
|
||||
DSN string `mapstructure:"dsn"`
|
||||
}
|
||||
|
||||
type DockerConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Host string `mapstructure:"host"` // unix:///var/run/docker.sock
|
||||
ContainerPattern string `mapstructure:"container_pattern"`
|
||||
MaxLogBytes int64 `mapstructure:"max_log_bytes"`
|
||||
MaxLogLines int `mapstructure:"max_log_lines"`
|
||||
MaxLogSince time.Duration `mapstructure:"max_log_since"`
|
||||
}
|
||||
|
||||
type AuthConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
APIKeys map[string]string `mapstructure:"api_keys"` // key → description
|
||||
JWTSecret string `mapstructure:"jwt_secret"`
|
||||
}
|
||||
|
||||
type RateLimitConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Rate float64 `mapstructure:"rate"` // 每秒允许的请求数
|
||||
Burst int `mapstructure:"burst"` // 突发允许数
|
||||
}
|
||||
|
||||
func Load(env string) (*Config, error) {
|
||||
func Load() (*Config, error) {
|
||||
v := viper.New()
|
||||
|
||||
v.SetConfigName(env) // dev.yaml / staging.yaml / prod.yaml
|
||||
v.SetConfigName("config")
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath("./config")
|
||||
v.AddConfigPath(".")
|
||||
v.AddConfigPath("/config")
|
||||
|
||||
v.SetEnvPrefix("OPS_MCP")
|
||||
v.AutomaticEnv()
|
||||
v.SetDefault("transport", "stdio")
|
||||
|
||||
// 配置文件不存在不算错误,直接用默认值 + 环境变量
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("read config: %w", err)
|
||||
slog.Warn("no config file found, using defaults and env vars", "err", err)
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
v.SetDefault("server.transport", "stdio")
|
||||
v.SetDefault("server.addr", ":8080")
|
||||
v.SetDefault("docker.max_log_bytes", 1024*1024)
|
||||
v.SetDefault("docker.max_log_lines", 500)
|
||||
v.SetDefault("docker.max_log_since", "1h")
|
||||
v.SetDefault("rate_limit.rate", 10.0)
|
||||
v.SetDefault("rate_limit.burst", 20)
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal config: %w", err)
|
||||
cfg := &Config{}
|
||||
if err := v.Unmarshal(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
+1
-23
@@ -1,23 +1 @@
|
||||
env: dev
|
||||
|
||||
server:
|
||||
transport: stdio
|
||||
addr: ":8080"
|
||||
|
||||
databases:
|
||||
local_pg:
|
||||
driver: postgres
|
||||
dsn: postgres://postgres:postgres@localhost:5432/ops_dev?sslmode=disable
|
||||
|
||||
docker:
|
||||
enabled: false
|
||||
host: unix:///var/run/docker.sock
|
||||
container_pattern: "^(app-|web-|api-).*"
|
||||
|
||||
auth:
|
||||
enabled: false
|
||||
|
||||
rate_limit:
|
||||
enabled: false
|
||||
rate: 50
|
||||
burst: 100
|
||||
transport: stdio
|
||||
|
||||
+1
-24
@@ -1,24 +1 @@
|
||||
env: prod
|
||||
|
||||
server:
|
||||
transport: sse
|
||||
addr: ":8080"
|
||||
|
||||
databases:
|
||||
main_db:
|
||||
driver: postgres
|
||||
dsn: postgres://ops_readonly:${DB_PASSWORD}@db-prod.internal:5432/ops?sslmode=require
|
||||
|
||||
docker:
|
||||
enabled: true
|
||||
host: unix:///var/run/docker.sock
|
||||
container_pattern: "^(app-|web-|api-|worker-).*"
|
||||
|
||||
auth:
|
||||
enabled: true
|
||||
jwt_secret: ${JWT_SECRET}
|
||||
|
||||
rate_limit:
|
||||
enabled: true
|
||||
rate: 10
|
||||
burst: 20
|
||||
transport: sse
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
env: staging
|
||||
|
||||
server:
|
||||
transport: sse
|
||||
addr: ":8080"
|
||||
|
||||
databases:
|
||||
main_db:
|
||||
driver: postgres
|
||||
dsn: postgres://ops_readonly:${DB_PASSWORD}@db-staging.internal:5432/ops?sslmode=require
|
||||
|
||||
docker:
|
||||
enabled: true
|
||||
host: unix:///var/run/docker.sock
|
||||
container_pattern: "^(app-|web-|api-|worker-).*"
|
||||
|
||||
auth:
|
||||
enabled: true
|
||||
jwt_secret: ${JWT_SECRET}
|
||||
|
||||
rate_limit:
|
||||
enabled: true
|
||||
rate: 20
|
||||
burst: 40
|
||||
Reference in New Issue
Block a user