logranger/config.go
Winni Neessen ea1ac393bc
Add SPDX license identifiers and update copyright year
All source code files as well as README.md, .gitignore, and various configuration files have been updated with an SPDX license identifier, changing license from CC0-1.0 to MIT. The copyright year has been updated to 2023, and the copyright holder's email address has been corrected from 'winni@neessen.dev' to 'wn@neessen.dev'. An MIT license text file has been additionally added.
2023-12-15 16:35:41 +01:00

54 lines
1.4 KiB
Go

// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package logranger
import (
"fmt"
"os"
"github.com/kkyr/fig"
)
// Config holds all the global configuration settings that are parsed by fig
type Config struct {
// Server holds server specific configuration values
Server struct {
PIDFile string `fig:"pid_file" default:"/var/run/logranger.pid"`
}
Listener struct {
ListenerUnix struct {
Path string `fig:"path" default:"/var/tmp/logranger.sock"`
} `fig:"unix"`
ListenerTCP struct {
Addr string `fig:"addr" default:"0.0.0.0"`
Port uint `fig:"port" default:"9099"`
} `fig:"tcp"`
ListenerTLS struct {
Addr string `fig:"addr" default:"0.0.0.0"`
Port uint `fig:"port" default:"9099"`
CertPath string `fig:"cert_path"`
KeyPath string `fig:"key_path"`
} `fig:"tls"`
Type ListenerType `fig:"type" default:"unix"`
} `fig:"listener"`
Log struct {
Level string `fig:"level" default:"info"`
} `fig:"log"`
}
// NewConfig returns a new Config object
func NewConfig(p, f string) (*Config, error) {
co := Config{}
_, err := os.Stat(fmt.Sprintf("%s/%s", p, f))
if err != nil {
return &co, fmt.Errorf("failed to read config: %w", err)
}
if err := fig.Load(&co, fig.Dirs(p), fig.File(f), fig.UseEnv("logranger")); err != nil {
return &co, fmt.Errorf("failed to load config: %w", err)
}
return &co, nil
}