vegvisir/pkg/config/config.go

82 lines
1.3 KiB
Go
Raw Normal View History

// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package config
import (
"encoding/json"
"io/ioutil"
"os"
)
2021-07-08 22:32:49 +02:00
type Config struct {
Server Server
Cache Cache
Backends map[string]Backend
confPath string
2021-07-08 22:32:49 +02:00
}
type Server struct {
Address string
Port int
}
type Cache struct {
Type string
Host string
Port int
Username string
Password string
Database string
RouteTtl int
ResponseTtl int
}
type Backend struct {
PrefixUrl string
BackendAddress string
Protocol string
Routes []Route
}
type Route struct {
Pattern string
Target string
}
var DefaultRoute = Route{
Pattern: "(.*)",
Target: "",
2021-07-08 22:32:49 +02:00
}
func New(confPath string) *Config {
return &Config{
confPath: confPath,
}
}
func (c *Config) Load() error {
if _, err := os.Stat(c.confPath); err != nil {
return err
}
data, err := ioutil.ReadFile(c.confPath)
if err != nil {
return err
}
err = json.Unmarshal(data, &c)
if err != nil {
return err
}
return nil
}