Base config support

This commit is contained in:
Piotr Biernat 2021-07-08 22:32:49 +02:00
parent f8b05510b0
commit 0675fc61d5
5 changed files with 74 additions and 0 deletions

17
config/config.go Normal file
View File

@ -0,0 +1,17 @@
package config
type Config struct {
Server Server
Services map[string]Service
}
type Server struct {
Address string
Port int
}
type Service struct {
BaseURL string
ForwardTo string
Protocol string
}

18
config/test/sample.json Normal file
View File

@ -0,0 +1,18 @@
{
"server": {
"address": "127.0.0.1",
"port": 8080
},
"services": {
"test-app1": {
"baseUrl": "test/",
"forwardTo": "127.0.0.1:3030/",
"protocol": "http"
},
"test-app2": {
"baseUrl": "test2/",
"forwardTo": "httpbin.org/",
"protocol": "http"
}
}
}

2
go.mod
View File

@ -1,3 +1,5 @@
module vegvisir
go 1.13
require github.com/opentracing/opentracing-go v1.2.0 // indirect

View File

@ -1,4 +1,10 @@
package main
import (
"vegvisir/server"
)
func main() {
s := server.NewServer()
err := s.LoadConfig("config/test/sample.json")
}

31
server/server.go Normal file
View File

@ -0,0 +1,31 @@
package server
import (
"encoding/json"
"io/ioutil"
"os"
"vegvisir/config"
)
type Server struct {
Config config.Config
}
func NewServer() Server {
return Server{}
}
func (s *Server) LoadConfig(file string) error {
if _, err := os.Stat(file); err != nil {
return err
}
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
json.Unmarshal(data, &s.Config)
return nil
}