mirror of
https://github.com/netscrawler/changeAPI.git
synced 2025-05-06 15:29:53 +00:00
v0.3
This commit is contained in:
parent
59480c95ea
commit
4ac3a2ade0
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -1,2 +1,4 @@
|
|||||||
# Auto detect text files and perform LF normalization
|
# Auto detect text files and perform LF normalization
|
||||||
* text=auto
|
* text=auto
|
||||||
|
|
||||||
|
ghz --insecure --proto="C:\Users\maria\Documents\GitHub\protoss\proto\changeAPI\changeAPI.proto" --call=Converter/Convert --n=1000 --c=10 localhost:44044
|
@ -1,12 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"converter/internal/app"
|
"converter/internal/app"
|
||||||
"converter/internal/config"
|
"converter/internal/config"
|
||||||
"converter/internal/lib/logger/handlers/slogpretty"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -30,31 +27,6 @@ func main() {
|
|||||||
applicaton := app.New(log, cfg.GRPC.Port)
|
applicaton := app.New(log, cfg.GRPC.Port)
|
||||||
go applicaton.GRPCSrv.MustRun()
|
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)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
|
||||||
@ -69,7 +41,9 @@ func setupLogger(env string) *slog.Logger {
|
|||||||
|
|
||||||
switch env {
|
switch env {
|
||||||
case envlocal:
|
case envlocal:
|
||||||
log = setupPrettySlog()
|
log = slog.New(
|
||||||
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
|
||||||
|
)
|
||||||
case envdev:
|
case envdev:
|
||||||
log = slog.New(
|
log = slog.New(
|
||||||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
|
||||||
@ -81,15 +55,3 @@ func setupLogger(env string) *slog.Logger {
|
|||||||
}
|
}
|
||||||
return log
|
return log
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupPrettySlog() *slog.Logger {
|
|
||||||
opts := slogpretty.PrettyHandlerOptions{
|
|
||||||
SlogOpts: &slog.HandlerOptions{
|
|
||||||
Level: slog.LevelDebug,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
handler := opts.NewPrettyHandler(os.Stdout)
|
|
||||||
|
|
||||||
return slog.New(handler)
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,3 @@ token_ttl: 1h
|
|||||||
grpc:
|
grpc:
|
||||||
port: 44044
|
port: 44044
|
||||||
timeout: 10h
|
timeout: 10h
|
||||||
redis:
|
|
||||||
addr: "localhost:6379"
|
|
||||||
password: "1234"
|
|
||||||
db: 0
|
|
@ -6,8 +6,17 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type response struct {
|
||||||
|
ConvertedAmount uint32
|
||||||
|
Rate float32
|
||||||
|
ConvertedAmo uint32
|
||||||
|
ConvertedCurrency string
|
||||||
|
}
|
||||||
|
|
||||||
type Converter interface {
|
type Converter interface {
|
||||||
Convert(ctx context.Context,
|
Convert(ctx context.Context,
|
||||||
amount uint32,
|
amount uint32,
|
||||||
@ -28,20 +37,31 @@ func (s *serverAPI) Convert(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req *cnvrtv1.ConvertRequest) (
|
req *cnvrtv1.ConvertRequest) (
|
||||||
*cnvrtv1.ConvertResponse, error) {
|
*cnvrtv1.ConvertResponse, error) {
|
||||||
|
|
||||||
|
log := setupLogger()
|
||||||
|
log.Info("convertationRequest", slog.Any("req", req))
|
||||||
|
|
||||||
if !isValidCurrency(req.GetTargetCurrency()) {
|
if !isValidCurrency(req.GetTargetCurrency()) {
|
||||||
return nil, status.Error(codes.InvalidArgument, "Invalid target currency")
|
return nil, status.Error(codes.InvalidArgument, "Invalid target currency")
|
||||||
}
|
}
|
||||||
|
|
||||||
convertedAmount, rate, err := s.convert.Convert(ctx, req.GetAmount(), req.GetTargetCurrency())
|
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 {
|
if err != nil {
|
||||||
//todo error handler
|
//todo error handler
|
||||||
return nil, status.Error(codes.Internal, "Internal error")
|
return nil, status.Error(codes.Internal, "Internal error")
|
||||||
}
|
}
|
||||||
return &cnvrtv1.ConvertResponse{
|
return &cnvrtv1.ConvertResponse{
|
||||||
BaseAmount: req.GetAmount(),
|
BaseAmount: baseAmount,
|
||||||
ConvertedAmount: convertedAmount,
|
ConvertedAmount: convertedAmo,
|
||||||
ConvertedCurrency: req.GetTargetCurrency(),
|
ConvertedCurrency: convertedCurrency,
|
||||||
Rate: rate,
|
Rate: Rate,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,3 +77,11 @@ func isValidCurrency(currency string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupLogger() *slog.Logger {
|
||||||
|
var log *slog.Logger
|
||||||
|
log = slog.New(
|
||||||
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
|
||||||
|
)
|
||||||
|
return log
|
||||||
|
}
|
||||||
|
@ -35,7 +35,7 @@ func (c *Converter) Convert(
|
|||||||
)
|
)
|
||||||
log.Info("convertation")
|
log.Info("convertation")
|
||||||
var convertedAmount uint32
|
var convertedAmount uint32
|
||||||
//TODO extract rate
|
|
||||||
rate, err := rateExtract.GetExchangeRate(currency)
|
rate, err := rateExtract.GetExchangeRate(currency)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error extracting rate", sl.Err(err))
|
log.Error("Error extracting rate", sl.Err(err))
|
||||||
|
Loading…
Reference in New Issue
Block a user