diff --git a/.gitattributes b/.gitattributes index dfe0770..a14226b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,4 @@ # Auto detect text files and perform LF normalization * text=auto + +ghz --insecure --proto="C:\Users\maria\Documents\GitHub\protoss\proto\changeAPI\changeAPI.proto" --call=Converter/Convert --n=1000 --c=10 localhost:44044 \ No newline at end of file diff --git a/cmd/converter/main.go b/cmd/converter/main.go index d92dd1e..4fbd988 100644 --- a/cmd/converter/main.go +++ b/cmd/converter/main.go @@ -1,12 +1,9 @@ package main import ( - "context" "converter/internal/app" "converter/internal/config" - "converter/internal/lib/logger/handlers/slogpretty" "fmt" - "github.com/redis/go-redis/v9" "log/slog" "os" "os/signal" @@ -30,31 +27,6 @@ func main() { applicaton := app.New(log, cfg.GRPC.Port) go applicaton.GRPCSrv.MustRun() - // TODO: инициализировать приложение app - - //TODO:redis - client := redis.NewClient(&redis.Options{ - Addr: cfg.Redis.Addr, - Password: cfg.Redis.Password, - DB: cfg.Redis.DB, - }) - - ctx := context.Background() - - err := client.Set(ctx, "key", "value1", 0).Err() - if err != nil { - log.Info("err") - } - - val, err := client.Get(ctx, "key").Result() - if err != nil { - log.Info("err") - } - log.Info("key", slog.Any("val", val)) - - // TODO: запустить gRPC сервер приложения - - // Graceful shutdown stop := make(chan os.Signal, 1) signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT) @@ -69,7 +41,9 @@ func setupLogger(env string) *slog.Logger { switch env { case envlocal: - log = setupPrettySlog() + log = slog.New( + slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}), + ) case envdev: log = slog.New( slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}), @@ -81,15 +55,3 @@ func setupLogger(env string) *slog.Logger { } return log } - -func setupPrettySlog() *slog.Logger { - opts := slogpretty.PrettyHandlerOptions{ - SlogOpts: &slog.HandlerOptions{ - Level: slog.LevelDebug, - }, - } - - handler := opts.NewPrettyHandler(os.Stdout) - - return slog.New(handler) -} diff --git a/config/local.yaml b/config/local.yaml index 374baaf..1dd38e3 100644 --- a/config/local.yaml +++ b/config/local.yaml @@ -3,7 +3,3 @@ token_ttl: 1h grpc: port: 44044 timeout: 10h -redis: - addr: "localhost:6379" - password: "1234" - db: 0 \ No newline at end of file diff --git a/internal/grpc/cnvrt/server.go b/internal/grpc/cnvrt/server.go index b5d94dd..d8f3261 100644 --- a/internal/grpc/cnvrt/server.go +++ b/internal/grpc/cnvrt/server.go @@ -6,8 +6,17 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "log/slog" + "os" ) +type response struct { + ConvertedAmount uint32 + Rate float32 + ConvertedAmo uint32 + ConvertedCurrency string +} + type Converter interface { Convert(ctx context.Context, amount uint32, @@ -28,20 +37,31 @@ func (s *serverAPI) Convert( ctx context.Context, req *cnvrtv1.ConvertRequest) ( *cnvrtv1.ConvertResponse, error) { + + log := setupLogger() + log.Info("convertationRequest", slog.Any("req", req)) + if !isValidCurrency(req.GetTargetCurrency()) { return nil, status.Error(codes.InvalidArgument, "Invalid target currency") } convertedAmount, rate, err := s.convert.Convert(ctx, req.GetAmount(), req.GetTargetCurrency()) + baseAmount := req.GetAmount() + convertedAmo := convertedAmount + convertedCurrency := req.GetTargetCurrency() + Rate := rate + r := response{baseAmount, rate, convertedAmo, convertedCurrency} + log.Info("ConvertationResponse", slog.Any("res", r)) + if err != nil { //todo error handler return nil, status.Error(codes.Internal, "Internal error") } return &cnvrtv1.ConvertResponse{ - BaseAmount: req.GetAmount(), - ConvertedAmount: convertedAmount, - ConvertedCurrency: req.GetTargetCurrency(), - Rate: rate, + BaseAmount: baseAmount, + ConvertedAmount: convertedAmo, + ConvertedCurrency: convertedCurrency, + Rate: Rate, }, nil } @@ -57,3 +77,11 @@ func isValidCurrency(currency string) bool { } return false } + +func setupLogger() *slog.Logger { + var log *slog.Logger + log = slog.New( + slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}), + ) + return log +} diff --git a/internal/services/converter/converter.go b/internal/services/converter/converter.go index 98fa44d..3d1540a 100644 --- a/internal/services/converter/converter.go +++ b/internal/services/converter/converter.go @@ -35,7 +35,7 @@ func (c *Converter) Convert( ) log.Info("convertation") var convertedAmount uint32 - //TODO extract rate + rate, err := rateExtract.GetExchangeRate(currency) if err != nil { log.Error("Error extracting rate", sl.Err(err))