config

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package config provides configuration management for apt-proxy.

Index

Constants

View Source
const (
	EnvHost        = "APT_PROXY_HOST"
	EnvPort        = "APT_PROXY_PORT"
	EnvMode        = "APT_PROXY_MODE"
	EnvCacheDir    = "APT_PROXY_CACHEDIR"
	EnvDebug       = "APT_PROXY_DEBUG"
	EnvUbuntu      = "APT_PROXY_UBUNTU"
	EnvUbuntuPorts = "APT_PROXY_UBUNTU_PORTS"
	EnvDebian      = "APT_PROXY_DEBIAN"
	EnvCentOS      = "APT_PROXY_CENTOS"
	EnvAlpine      = "APT_PROXY_ALPINE"

	// Cache configuration environment variables
	EnvCacheMaxSize         = "APT_PROXY_CACHE_MAX_SIZE"
	EnvCacheTTL             = "APT_PROXY_CACHE_TTL"
	EnvCacheCleanupInterval = "APT_PROXY_CACHE_CLEANUP_INTERVAL"

	// TLS configuration environment variables
	EnvTLSEnabled  = "APT_PROXY_TLS_ENABLED"
	EnvTLSCertFile = "APT_PROXY_TLS_CERT"
	EnvTLSKeyFile  = "APT_PROXY_TLS_KEY"

	// Security configuration environment variables
	EnvAPIKey        = "APT_PROXY_API_KEY"
	EnvEnableAPIAuth = "APT_PROXY_ENABLE_API_AUTH"

	// Configuration file environment variable
	EnvConfigFile = "APT_PROXY_CONFIG_FILE"
)

Environment variable names for configuration

View Source
const (
	DefaultHost     = "0.0.0.0"
	DefaultPort     = "3142"
	DefaultCacheDir = "./.aptcache"

	// Default cache configuration values (as strings for flag parsing)
	DefaultCacheMaxSizeGB          = 10  // 10 GB
	DefaultCacheTTLHours           = 168 // 7 days
	DefaultCacheCleanupIntervalMin = 60  // 1 hour

	// Default configuration file paths (searched in order)
	DefaultConfigFileName = "apt-proxy.yaml"

	// Default async benchmark setting
	DefaultAsyncBenchmark = true // Enable async mirror benchmark by default for faster startup
)

Default configuration values

View Source
const (
	EnvLogLevel  = "LOG_LEVEL"
	EnvLogFormat = "LOG_FORMAT"
)

Environment variable names for logging configuration

Variables

This section is empty.

Functions

func FindConfigFile

func FindConfigFile() string

FindConfigFile searches for a configuration file in common locations. Returns the path to the first file found, or empty string if none found.

func GetAllowedModes

func GetAllowedModes() []string

GetAllowedModes returns the list of allowed mode values

func GetConfigFilePaths

func GetConfigFilePaths() []string

GetConfigFilePaths returns a slice of paths searched for configuration files. Useful for debugging and logging.

func IsConfigFileProvided

func IsConfigFileProvided() bool

IsConfigFileProvided checks if a config file path was explicitly provided via CLI flag or environment variable.

func ModeToInt

func ModeToInt(mode string) int

ModeToInt converts a validated mode string to its corresponding integer constant. This function should only be called after mode validation via configutil.ResolveEnum.

func UpdateGlobalState

func UpdateGlobalState(config *Config) error

UpdateGlobalState updates the global state with the current configuration, including proxy mode and mirror URLs for all supported distributions. This enables components throughout the application to access configuration.

func ValidateConfig

func ValidateConfig(config *Config) error

ValidateConfig performs validation on the configuration to ensure all required fields are set and valid. Returns an error if validation fails.

Types

type CacheConfig

type CacheConfig struct {
	// MaxSize is the maximum cache size in bytes (default: 10GB)
	MaxSize int64 `yaml:"max_size"`
	// MaxSizeGB is an alternative way to specify max size in GB for YAML config
	MaxSizeGB int64 `yaml:"max_size_gb"`
	// TTL is the time-to-live for cached items (default: 7 days)
	TTL time.Duration `yaml:"ttl"`
	// TTLHours is an alternative way to specify TTL in hours for YAML config
	TTLHours int `yaml:"ttl_hours"`
	// CleanupInterval is the interval between cleanup runs (default: 1 hour)
	CleanupInterval time.Duration `yaml:"cleanup_interval"`
	// CleanupIntervalMin is an alternative way to specify cleanup interval in minutes for YAML config
	CleanupIntervalMin int `yaml:"cleanup_interval_min"`
}

CacheConfig holds cache-specific configuration

type Config

type Config struct {
	Debug    bool           `yaml:"debug"`
	CacheDir string         `yaml:"cache_dir"`
	Mode     int            `yaml:"mode"`
	Listen   string         `yaml:"listen"`
	Mirrors  MirrorConfig   `yaml:"mirrors"`
	Cache    CacheConfig    `yaml:"cache"`
	TLS      TLSConfig      `yaml:"tls"`
	Security SecurityConfig `yaml:"security"`
}

Config holds all application configuration

func LoadConfigFile

func LoadConfigFile(path string) (*Config, error)

LoadConfigFile loads configuration from a YAML file. It returns nil if the file does not exist.

func MergeConfigs

func MergeConfigs(base, override *Config) *Config

MergeConfigs merges two configurations, with values from 'override' taking precedence. Zero values in 'override' do not override values in 'base'.

func ParseFlags

func ParseFlags() (*Config, error)

ParseFlags parses command-line flags and returns a Config struct with all application settings. It validates the mode parameter and sets up global state. Configuration priority: CLI flag > environment variable > default value. Returns an error if flag parsing fails or if an invalid mode is specified.

func ParseFlagsWithConfigFile

func ParseFlagsWithConfigFile() (*Config, error)

ParseFlagsWithConfigFile parses command-line flags and optionally loads configuration from a YAML file. Priority: CLI > ENV > Config File > Default.

type MirrorConfig

type MirrorConfig struct {
	Ubuntu      string `yaml:"ubuntu"`
	UbuntuPorts string `yaml:"ubuntu_ports"`
	Debian      string `yaml:"debian"`
	CentOS      string `yaml:"centos"`
	Alpine      string `yaml:"alpine"`
}

MirrorConfig holds mirror-specific configuration

type SecurityConfig

type SecurityConfig struct {
	// APIKey is the key required for accessing protected API endpoints.
	// If empty, API authentication is disabled.
	APIKey string `yaml:"api_key"`
	// EnableAPIAuth enables API authentication when APIKey is set.
	EnableAPIAuth bool `yaml:"enable_api_auth"`
}

SecurityConfig holds security-related configuration

type TLSConfig

type TLSConfig struct {
	// Enabled indicates whether TLS is enabled
	Enabled bool `yaml:"enabled"`
	// CertFile is the path to the TLS certificate file
	CertFile string `yaml:"cert_file"`
	// KeyFile is the path to the TLS private key file
	KeyFile string `yaml:"key_file"`
}

TLSConfig holds TLS/HTTPS configuration

type YAMLConfig

type YAMLConfig struct {
	Server struct {
		Host  string `yaml:"host"`
		Port  string `yaml:"port"`
		Debug bool   `yaml:"debug"`
	} `yaml:"server"`

	Cache struct {
		Dir                string `yaml:"dir"`
		MaxSizeGB          int64  `yaml:"max_size_gb"`
		TTLHours           int    `yaml:"ttl_hours"`
		CleanupIntervalMin int    `yaml:"cleanup_interval_min"`
	} `yaml:"cache"`

	Mirrors struct {
		Ubuntu      string `yaml:"ubuntu"`
		UbuntuPorts string `yaml:"ubuntu_ports"`
		Debian      string `yaml:"debian"`
		CentOS      string `yaml:"centos"`
		Alpine      string `yaml:"alpine"`
	} `yaml:"mirrors"`

	TLS struct {
		Enabled  bool   `yaml:"enabled"`
		CertFile string `yaml:"cert_file"`
		KeyFile  string `yaml:"key_file"`
	} `yaml:"tls"`

	Security struct {
		APIKey        string `yaml:"api_key"`
		EnableAPIAuth bool   `yaml:"enable_api_auth"`
	} `yaml:"security"`

	Mode string `yaml:"mode"`
}

YAMLConfig represents the YAML configuration file structure. It uses a more user-friendly structure that maps to the internal Config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL