feat: introduce YAML configuration

This commit is contained in:
2025-05-06 13:19:20 +02:00
parent 6017bf86f9
commit 3bbb689cac
9 changed files with 170 additions and 25 deletions

38
pkg/config/config.go Normal file
View File

@@ -0,0 +1,38 @@
package config
import (
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
Rcond RcondConfig `yaml:"rcond"`
}
type RcondConfig struct {
Addr string `yaml:"addr"`
ApiToken string `yaml:"api_token"`
}
func LoadConfig(path string) (*Config, error) {
yamlFile, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func SaveConfig(path string, config *Config) error {
yamlFile, err := yaml.Marshal(config)
if err != nil {
return err
}
return os.WriteFile(path, yamlFile, 0644)
}

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"time"
"github.com/0x1d/rcond/pkg/config"
"github.com/gorilla/mux"
)
@@ -15,11 +16,15 @@ type Server struct {
apiToken string
}
func NewServer(addr string, apiToken string) *Server {
func NewServer(cfg *config.Config) *Server {
if cfg.Rcond.Addr == "" || cfg.Rcond.ApiToken == "" {
panic("addr or api_token is not set")
}
router := mux.NewRouter()
srv := &http.Server{
Addr: addr,
Addr: cfg.Rcond.Addr,
Handler: router,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
@@ -28,7 +33,7 @@ func NewServer(addr string, apiToken string) *Server {
return &Server{
router: router,
srv: srv,
apiToken: apiToken,
apiToken: cfg.Rcond.ApiToken,
}
}