Replace env vars with toml config
This commit is contained in:
90
internal/config/config.go
Normal file
90
internal/config/config.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `toml:"server"`
|
||||
Database DatabaseConfig `toml:"database"`
|
||||
AWS AWSConfig `toml:"aws"`
|
||||
Yandex YandexConfig `toml:"yandex"`
|
||||
Telegram TelegramConfig `toml:"telegram"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port int `toml:"port"`
|
||||
ShutdownTimeout int `toml:"shutdown_timeout"`
|
||||
ForceShutdownTimeout int `toml:"force_shutdown_timeout"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Path string `toml:"path"`
|
||||
}
|
||||
|
||||
type AWSConfig struct {
|
||||
Region string `toml:"region"`
|
||||
AccessKey string `toml:"access_key_id"`
|
||||
SecretKey string `toml:"secret_access_key"`
|
||||
BucketName string `toml:"bucket_name"`
|
||||
Endpoint string `toml:"endpoint"`
|
||||
}
|
||||
|
||||
type YandexConfig struct {
|
||||
APIKey string `toml:"api_key"`
|
||||
FolderID string `toml:"folder_id"`
|
||||
}
|
||||
|
||||
type TelegramConfig struct {
|
||||
BotToken string `toml:"bot_token"`
|
||||
UpdateTimeout int `toml:"update_timeout"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns a Config with default values
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Server: ServerConfig{
|
||||
Port: 8080,
|
||||
ShutdownTimeout: 5,
|
||||
ForceShutdownTimeout: 20,
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Path: "data/transcriber.db",
|
||||
},
|
||||
AWS: AWSConfig{
|
||||
Region: "ru-central1",
|
||||
AccessKey: "",
|
||||
SecretKey: "",
|
||||
BucketName: "",
|
||||
Endpoint: "",
|
||||
},
|
||||
Yandex: YandexConfig{
|
||||
APIKey: "",
|
||||
FolderID: "",
|
||||
},
|
||||
Telegram: TelegramConfig{
|
||||
BotToken: "",
|
||||
UpdateTimeout: 10,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration from a TOML file
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
// Check if file exists
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("config file not found: %s", path)
|
||||
}
|
||||
|
||||
config := DefaultConfig()
|
||||
|
||||
// Load configuration from file
|
||||
if _, err := toml.DecodeFile(path, &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode config file: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
Reference in New Issue
Block a user