From f8040ec08853bd2a444c496da9f989deb43be987 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 4 May 2026 16:27:04 +0200 Subject: [PATCH] Default values for config --- api/Hello Echo.bru | 2 +- cmd/music-agregator/main.go | 17 ++++++++++------- internal/config/config.go | 12 ++++++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/api/Hello Echo.bru b/api/Hello Echo.bru index 6e7a5ac..30c4f2c 100644 --- a/api/Hello Echo.bru +++ b/api/Hello Echo.bru @@ -5,7 +5,7 @@ meta { } grpc { - url: localhost:8081 + url: localhost:3000 method: /music_agregator.hello.v1.HelloService/Echo body: grpc protoPath: ../proto/music_agregator/hello/v1/service.proto diff --git a/cmd/music-agregator/main.go b/cmd/music-agregator/main.go index dea17ce..cf4e22d 100644 --- a/cmd/music-agregator/main.go +++ b/cmd/music-agregator/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "net" "os" @@ -32,10 +33,9 @@ func main() { log.Info().Interface("config", cfg).Msg("Loaded config") // start the grpc in another thread to not block the next http start - go serveGrpc() + go serveGrpc(*cfg) serveHttp() - } func serveHttp() { @@ -49,27 +49,30 @@ func serveHttp() { router.Run() } -func serveGrpc() { +func serveGrpc(config config.Config) { var opts []grpc.ServerOption server := grpc.NewServer(opts...) + helloServiceHandler := hello.NewHelloServer() pb.RegisterHelloServiceServer(server, helloServiceHandler) - listener, err := net.Listen("tcp", "localhost:8081") + listener, err := net.Listen("tcp", fmt.Sprintf("%v:%v", config.App.Host, config.App.Port)) + if err != nil { log.Fatal().Err(err).Msg("Failed to listen on localhost:8081") } + server.Serve(listener) } -func parseConfig(path *string) (config.Config, error) { +func parseConfig(path *string) (*config.Config, error) { f, err := os.Open(*path) if err != nil { log.Error().Str("path", *path).Msg("Unable to opent config by path") - return config.Config{}, err + return nil, err } defer f.Close() - var cfg config.Config + cfg := config.NewConfig() decoder := yaml.NewDecoder(f) err = decoder.Decode(&cfg) if err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index 87de0c5..d11e2c3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,8 +1,16 @@ package config type Config struct { - Server struct { + App struct { Port string `yaml:"port"` Host string `yaml:"host"` - } `yaml:"server"` + } `yaml:"app"` +} + +func NewConfig() *Config { + config := Config{} + config.App.Host = "localhost" + config.App.Port = "8080" + + return &config }