From ac355d32b4976edccb712666740f3326c1c47512 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 13 Jun 2023 16:26:00 +0900 Subject: [PATCH] =?UTF-8?q?config=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.go | 46 +++++++++++++++++++++++++++++------------- server/http_handler.go | 2 +- server/server.go | 35 +++++++++++++++++++++----------- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/client/client.go b/client/client.go index d4ee06c..3fd7496 100644 --- a/client/client.go +++ b/client/client.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/fs" "os" "os/exec" "os/signal" @@ -34,8 +33,30 @@ type clientConfig struct { HttpAddress string `json:"http_server_address"` } -type totalConfig struct { - Client clientConfig `json:"houston_client"` +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) + if err != nil { + return clientConfig{}, err + } + + if config.Houston == nil { + return clientConfig{}, errors.New(`"houston" object is missing in config.json`) + } + + return config.Houston.Client, nil } type HoustonClient interface { @@ -161,28 +182,24 @@ func (hc *houstonClient) makeOperationQueryRequest() *protos.OperationQueryReque } func NewClient() (HoustonClient, error) { - bt, err := os.ReadFile("config.json") - if errors.Is(err, fs.ErrNotExist) { - return nil, err - } - var config totalConfig - if err := json.Unmarshal(bt, &config); err != nil { + clientConfig, err := loadClientConfig() + if err != nil { return nil, err } - if len(config.Client.GrpcAddress) == 0 { + if len(clientConfig.GrpcAddress) == 0 { return nil, errors.New("client.grpc_server_address is missing") } - if len(config.Client.HttpAddress) == 0 { + if len(clientConfig.HttpAddress) == 0 { return nil, errors.New("client.http_server_address is missing") } var client *grpc.ClientConn for { - logger.Println("grpc.DialContext :", config.Client.GrpcAddress) + logger.Println("grpc.DialContext :", clientConfig.GrpcAddress) dialContext, cancelDial := context.WithTimeout(context.Background(), 15*time.Second) - client, err = grpc.DialContext(dialContext, config.Client.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials())) + client, err = grpc.DialContext(dialContext, clientConfig.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials())) if err == nil { cancelDial() break @@ -220,7 +237,7 @@ func NewClient() (HoustonClient, error) { client: client, extraMetrics: unsafe.Pointer(&map[string]float32{}), deploys: deploys, - httpAddr: config.Client.HttpAddress, + httpAddr: clientConfig.HttpAddress, timestamp: exefi.ModTime().String(), } @@ -267,6 +284,7 @@ func NewClient() (HoustonClient, error) { var dr shared.DeployRequest unmarshal(&dr, resp.Args) + logger.Println(dr.Name, myname) if dr.Name == myname { if srcdir, replacer, err := hc.prepareUpdateSelf(&dr); err == nil { args := []string{ diff --git a/server/http_handler.go b/server/http_handler.go index 4707165..b192120 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -45,7 +45,7 @@ func NewHoustonHandler() HoustonServerWithHandler { } func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error { - storagePath := loadConfig().StoragePath + storagePath := loadServerConfig().StoragePath h.deployPath = path.Join(storagePath, "deploys") h.downloadPath = path.Join(storagePath, "downloads") diff --git a/server/server.go b/server/server.go index ff19955..cef30b3 100644 --- a/server/server.go +++ b/server/server.go @@ -25,10 +25,6 @@ type serverConfig struct { StoragePath string `json:"storage_path"` } -type totalConfig struct { - Server serverConfig `json:"houston_server"` -} - type DeployRequest struct { shared.DeployRequest hostnames []string @@ -111,24 +107,39 @@ type Operation interface { Hosts() map[string]hostSnapshot } -func loadConfig() serverConfig { - var config totalConfig - - config.Server.GrpcPort = 8080 +func loadServerConfig() serverConfig { configFile, err := os.Open("config.json") if err != nil { logger.Error(err) - return config.Server + 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) if err != nil { logger.Error(err) - return config.Server + return serverConfig{ + GrpcPort: 8080, + } } - return config.Server + if config.Houston == nil { + logger.Error(`"houston" object is missing in config.json`) + return serverConfig{ + GrpcPort: 8080, + } + } + + return config.Houston.Server } func NewServer() HoustonServer { @@ -144,7 +155,7 @@ func NewServer() HoustonServer { rpcServer: grpcServer, os: os, ms: ms, - port: loadConfig().GrpcPort, + port: loadServerConfig().GrpcPort, } }