vegvisir/pkg/config/config.go
Piotr Biernat 7bb7417f3a
All checks were successful
continuous-integration/drone/push Build is passing
[fix] Fixed all linters warnings
2021-11-15 21:47:25 +01:00

91 lines
1.5 KiB
Go

// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
// Package config main config file structs
package config
import (
"encoding/json"
"io/ioutil"
"os"
)
// Config struct
type Config struct {
Server Server
Cache Cache
Backends map[string]Backend
confPath string
}
// Server struct
type Server struct {
Address string
Port int
}
// Cache struct
type Cache struct {
Type string
Host string
Port int
Username string
Password string
Database string
RouteTTL int
ResponseTTL int
}
// Backend struct
type Backend struct {
PrefixURL string
BackendAddress string
Protocol string
Routes []Route
}
// Route struct
type Route struct {
Pattern string
Target string
}
// DefaultRoute shorthand
var DefaultRoute = Route{
Pattern: "(.*)",
Target: "",
}
// New function
func New(confPath string) *Config {
return &Config{
confPath: confPath,
}
}
// Load function
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
}