summaryrefslogtreecommitdiffstats
path: root/internal/config.go
blob: 8d9d961ba5aeb25ffbfb9455c227244ea0f893d3 (plain) (blame)
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
52
53
54
55
56
package internal

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
)

type ServerConfig struct {
	Host  string `yaml:"host"`
	Port  string `yaml:"port"`
	Title string `yaml:"title"`
}

type Config struct {
	Server ServerConfig `yaml:"server"`
}

// Default configuration values
func DefaultConfig() *Config {
	config := &Config{}
	config.Server.Host = "0.0.0.0"
	config.Server.Port = "8080"
	config.Server.Title = "WireGuard Gateway Portal"
	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.Server.Host, c.Server.Port)
}