2021-07-10 23:57:45 +02:00
|
|
|
// ___ ____ ___ ___
|
|
|
|
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
|
|
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
|
|
// \ / | _ | _ | \ / || __ | || ||\\
|
|
|
|
// \/ |___ |___ | \/ || ____| || || \\
|
2021-07-23 16:10:37 +02:00
|
|
|
//
|
2021-07-10 23:57:45 +02:00
|
|
|
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
|
|
|
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
|
|
|
|
2021-07-23 16:10:37 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Config struct
|
2021-07-08 22:32:49 +02:00
|
|
|
type Config struct {
|
|
|
|
Server Server
|
2021-07-25 01:02:35 +02:00
|
|
|
Cache Cache
|
2021-07-10 23:57:45 +02:00
|
|
|
Backends map[string]Backend
|
2021-07-24 17:46:22 +02:00
|
|
|
|
|
|
|
confPath string
|
2021-07-08 22:32:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Server struct
|
2021-07-08 22:32:49 +02:00
|
|
|
type Server struct {
|
|
|
|
Address string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Cache struct
|
2021-07-25 01:02:35 +02:00
|
|
|
type Cache struct {
|
|
|
|
Type string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
2021-08-03 00:13:14 +02:00
|
|
|
RouteTTL int
|
|
|
|
ResponseTTL int
|
2021-07-25 01:02:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Backend struct
|
2021-07-10 23:57:45 +02:00
|
|
|
type Backend struct {
|
2021-08-03 00:13:14 +02:00
|
|
|
PrefixURL string
|
2021-07-10 23:57:45 +02:00
|
|
|
BackendAddress string
|
|
|
|
Protocol string
|
|
|
|
Routes []Route
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Route struct
|
2021-07-10 23:57:45 +02:00
|
|
|
type Route struct {
|
|
|
|
Pattern string
|
|
|
|
Target string
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// DefaultRoute shorthand
|
2021-07-10 23:57:45 +02:00
|
|
|
var DefaultRoute = Route{
|
|
|
|
Pattern: "(.*)",
|
|
|
|
Target: "",
|
2021-07-08 22:32:49 +02:00
|
|
|
}
|
2021-07-23 16:10:37 +02:00
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// New function
|
2021-07-24 17:46:22 +02:00
|
|
|
func New(confPath string) *Config {
|
|
|
|
return &Config{
|
|
|
|
confPath: confPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Load function
|
2021-07-24 17:46:22 +02:00
|
|
|
func (c *Config) Load() error {
|
|
|
|
if _, err := os.Stat(c.confPath); err != nil {
|
2021-07-23 16:10:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-24 17:46:22 +02:00
|
|
|
data, err := ioutil.ReadFile(c.confPath)
|
2021-07-23 16:10:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|