diff --git a/client/client.go b/client/client.go index 0b1b875..27f4554 100644 --- a/client/client.go +++ b/client/client.go @@ -2,7 +2,6 @@ package client import ( "context" - "encoding/json" "errors" "fmt" "io" @@ -49,30 +48,21 @@ type clientConfig struct { var autorun = flagx.String("autorun", "", "") +type outerconfig struct { + Houston *struct { + Client clientConfig `json:"client"` + } `json:"houston"` +} + func loadClientConfig() (clientConfig, error) { - configFile, err := os.Open("config.json") - if err != nil { - return clientConfig{}, err - } - defer configFile.Close() - - var config struct { - Houston *struct { - Client clientConfig `json:"client"` - } `json:"houston"` - } - - dec := json.NewDecoder(configFile) - err = dec.Decode(&config) + var oc outerconfig + err := gocommon.LoadConfig[outerconfig](&oc) if err != nil { + logger.Println(err) return clientConfig{}, err } - if config.Houston == nil { - return clientConfig{}, errors.New(`"houston" object is missing in config.json`) - } - - return config.Houston.Client, nil + return oc.Houston.Client, nil } type HoustonClient interface { diff --git a/server/server.go b/server/server.go index 9fcb378..d01bc9b 100644 --- a/server/server.go +++ b/server/server.go @@ -1,11 +1,10 @@ package server import ( - "encoding/json" "fmt" "net" - "os" + "repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/houston/shared" "repositories.action2quare.com/ayo/houston/shared/protos" @@ -108,24 +107,15 @@ type Operation interface { DeplyingProgress() []deployingProgress } +type outerconfig struct { + Houston *struct { + Server serverConfig `json:"server"` + } `json:"houston"` +} + func loadServerConfig() serverConfig { - configFile, err := os.Open("config.json") - if err != nil { - logger.Println(err) - return serverConfig{ - GrpcPort: 8080, - } - } - defer configFile.Close() - - var config struct { - Houston *struct { - Server serverConfig `json:"server"` - } `json:"houston"` - } - - dec := json.NewDecoder(configFile) - err = dec.Decode(&config) + var oc outerconfig + err := gocommon.LoadConfig[outerconfig](&oc) if err != nil { logger.Println(err) return serverConfig{ @@ -133,14 +123,7 @@ func loadServerConfig() serverConfig { } } - if config.Houston == nil { - logger.Println(`"houston" object is missing in config.json`) - return serverConfig{ - GrpcPort: 8080, - } - } - - return config.Houston.Server + return oc.Houston.Server } func NewServer() HoustonServer {