1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package internal
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
PasswordHash string `yaml:"password_hash"`
}
// Default configuration values
func DefaultConfig() *Config {
config := &Config{}
config.Host = "0.0.0.0"
config.Port = "8080"
return config
}
// LoadConfig loads configuration from file, falls back to defaults if file doesn't exist
func LoadConfig(configPath string) (*Config, error) {
config := DefaultConfig()
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// Config file doesn't exist, use defaults
return config, nil
}
// Read config file
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
// Parse YAML
if err := yaml.Unmarshal(data, config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
return config, nil
}
// GetAddress returns the server address in host:port format
func (c *Config) GetAddress() string {
return fmt.Sprintf("%s:%s", c.Host, c.Port)
}
|