Default values for config

This commit is contained in:
Alexander
2026-05-04 16:27:04 +02:00
parent 268ee57913
commit f8040ec088
3 changed files with 21 additions and 10 deletions
+1 -1
View File
@@ -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
+10 -7
View File
@@ -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 {
+10 -2
View File
@@ -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
}