From 3182e91a81b76e2dfb3fe664dc2d3c26337a162d Mon Sep 17 00:00:00 2001 From: Olgun Cengiz <olgun.cengiz@vereign.com> Date: Mon, 15 Oct 2018 22:24:55 +0300 Subject: [PATCH] Viper configuration added --- .gitignore | 1 + config.yaml.sample | 10 ++++++++++ main.go | 20 ++++++++++---------- server/configs.go | 29 +++++++++++++++++++++++++++++ server/server.go | 3 ++- 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 config.yaml.sample create mode 100644 server/configs.go diff --git a/.gitignore b/.gitignore index 514bfbb..3a6b015 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ Gopkg.lock .idea/ .project +config.yaml diff --git a/config.yaml.sample b/config.yaml.sample new file mode 100644 index 0000000..0ec5701 --- /dev/null +++ b/config.yaml.sample @@ -0,0 +1,10 @@ +dataStorageClientUrl: localhost:7777 +grpcClientUrl: localhost:7877 +restClientUrl: localhost:7878 + +# Certificate Related Config +certDir: cert +certFile: server.crt +certKey: server.key +vereignCertFile: vereign_ca.cer +vereignCertKey: vereign_ca.key \ No newline at end of file diff --git a/main.go b/main.go index 0ed788d..076a8b7 100644 --- a/main.go +++ b/main.go @@ -18,31 +18,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. package main import ( - "fmt" "log" "code.vereign.com/code/key-storage-agent/server" - "code.vereign.com/code/viam-apis/utils" + "github.com/spf13/viper" ) // main start a gRPC server and waits for connection func main() { + server.SetConfigValues() // TODO this should be done via configuration or even a certificate repository - certDir := utils.GetCertDirFromFlags() + certDir := viper.GetString("certDir") if certDir == "" { log.Printf("cert-dir cannot be empty") return } - grpcAddress := fmt.Sprintf("%s:%d", "localhost", 7877) - restAddress := fmt.Sprintf("%s:%d", "localhost", 7878) - dataStorageAddress := fmt.Sprintf("%s:%d", "localhost", 7777) + grpcAddress := viper.GetString("grpcClientUrl") + restAddress := viper.GetString("restClientUrl") + dataStorageAddress := viper.GetString("dataStorageClientUrl") - certFilePath := certDir + "/server.crt" - privateKeyFilePath := certDir + "/server.key" - vereignCertFilePath := certDir + "/vereign_ca.cer" - vereignPrivateKeyFilePath := certDir + "/vereign_ca.key" + certFilePath := certDir + "/" + viper.GetString("certFile") + privateKeyFilePath := certDir + "/" + viper.GetString("certKey") + vereignCertFilePath := certDir + "/" + viper.GetString("vereignCertFile") + vereignPrivateKeyFilePath := certDir + "/" + viper.GetString("vereignCertKey") // fire the gRPC server in a goroutine go func() { diff --git a/server/configs.go b/server/configs.go new file mode 100644 index 0000000..6354275 --- /dev/null +++ b/server/configs.go @@ -0,0 +1,29 @@ +package server + +import ( + "log" + "github.com/spf13/viper" +) + +func SetConfigValues() { + // Set Default Values For Config Variables + + // Vereign API Related + viper.SetDefault("grpcClientUrl", "localhost:7877") + viper.SetDefault("restClientUrl", "localhost:7878") + viper.SetDefault("dataStorageClientUrl", "localhost:7777") + + // Certificates Related + viper.SetDefault("certDir", "cert") + viper.SetDefault("certFile", "server.crt") + viper.SetDefault("certKey", "server.key") + viper.SetDefault("vereignCertFile", "vereign_ca.cer") + viper.SetDefault("vereignCertKey", "vereign_ca.key") + + // Read Config File + viper.SetConfigName("config") + viper.AddConfigPath(".") + if err := viper.ReadInConfig(); err != nil { + log.Printf("can't read config: %s, will use default values", err) + } +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index 40e6a42..8a58bd7 100644 --- a/server/server.go +++ b/server/server.go @@ -36,6 +36,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/metadata" + "github.com/spf13/viper" ) // private type for Context keys @@ -69,7 +70,7 @@ func authenticateClient(ctx context.Context, s *handler.KeyStorageServerImpl, in } sessionClient := &client.DataStorageClientImpl{} - sessionClient.SetUpClient(viamAuth, "localhost:7777", pkgCertFile) + sessionClient.SetUpClient(viamAuth, viper.GetString("dataStorageClientUrl"), pkgCertFile) defer sessionClient.CloseClient() if clientAuth.Uuid == viamAuth.Uuid { -- GitLab