From 2f5f087b989b6516f5f6bf5606432301075a07f8 Mon Sep 17 00:00:00 2001 From: oleg Date: Tue, 23 Jan 2024 19:03:58 +0300 Subject: [PATCH] refactor accrual, handler and package util --- internal/handler/accrual/accrual.go | 22 +++++----------------- internal/handler/checkauth.go | 4 ++-- internal/handler/handler.go | 21 ++++++++++++++------- internal/util/{ => hash}/hash.go | 2 +- internal/util/{ => jwt}/jwt.go | 2 +- internal/util/{ => lun}/lun.go | 2 +- 6 files changed, 24 insertions(+), 29 deletions(-) rename internal/util/{ => hash}/hash.go (97%) rename internal/util/{ => jwt}/jwt.go (98%) rename internal/util/{ => lun}/lun.go (95%) diff --git a/internal/handler/accrual/accrual.go b/internal/handler/accrual/accrual.go index 7d9b33c..4ea8c4c 100644 --- a/internal/handler/accrual/accrual.go +++ b/internal/handler/accrual/accrual.go @@ -11,19 +11,6 @@ import ( "github.com/OlegVankov/fantastic-engine/internal/handler" ) -func sleep(ctx context.Context, interval time.Duration) error { - timer := time.NewTimer(interval) - select { - case <-ctx.Done(): - if !timer.Stop() { - <-timer.C - } - return ctx.Err() - case <-timer.C: - return nil - } -} - func SendAccrual(addr string, handler *handler.Handler) { ctx := context.Background() client := resty.New() @@ -33,7 +20,11 @@ func SendAccrual(addr string, handler *handler.Handler) { Status string `json:"status"` Accrual float64 `json:"accrual"` }{} - for { + + timer := time.NewTimer(time.Duration(5) * time.Second) + defer timer.Stop() + + for _ = range timer.C { orders, err := handler.Repository.GetOrders(ctx) @@ -62,8 +53,5 @@ func SendAccrual(addr string, handler *handler.Handler) { } - if err := sleep(ctx, time.Duration(5)*time.Second); err != nil { - fmt.Printf("[ERROR] %s\n", err.Error()) - } } } diff --git a/internal/handler/checkauth.go b/internal/handler/checkauth.go index 12b218f..9a09224 100644 --- a/internal/handler/checkauth.go +++ b/internal/handler/checkauth.go @@ -4,13 +4,13 @@ import ( "net/http" "strings" - "github.com/OlegVankov/fantastic-engine/internal/util" + "github.com/OlegVankov/fantastic-engine/internal/util/jwt" ) func Auth(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") - username := util.GetUser(token) + username := jwt.GetUser(token) if username == "" { w.WriteHeader(http.StatusUnauthorized) return diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 342686c..d54b1af 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -11,7 +11,9 @@ import ( "github.com/jackc/pgx/v5/pgconn" "github.com/OlegVankov/fantastic-engine/internal/repository" - "github.com/OlegVankov/fantastic-engine/internal/util" + "github.com/OlegVankov/fantastic-engine/internal/util/hash" + "github.com/OlegVankov/fantastic-engine/internal/util/jwt" + "github.com/OlegVankov/fantastic-engine/internal/util/lun" ) type Handler struct { @@ -34,7 +36,7 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) { return } - pass, err := util.StringToHash(c.Password) + pass, err := hash.StringToHash(c.Password) if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -52,7 +54,7 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) { return } - tkn, err := util.CreateToken(user.Login, user.ID) + tkn, err := jwt.CreateToken(user.Login, user.ID) if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -83,12 +85,17 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { return } - if !util.CheckPassword(user.Password, c.Password) { + if !hash.CheckPassword(user.Password, c.Password) { w.WriteHeader(http.StatusUnauthorized) return } - tkn, _ := util.CreateToken(user.Login, user.ID) + tkn, err := jwt.CreateToken(user.Login, user.ID) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + authorization := fmt.Sprintf("Bearer %s", tkn) w.Header().Add("Authorization", authorization) @@ -110,7 +117,7 @@ func (h *Handler) Orders(w http.ResponseWriter, r *http.Request) { number := string(body) - if !util.CheckLun(number) { + if !lun.CheckLun(number) { w.WriteHeader(http.StatusUnprocessableEntity) return } @@ -169,7 +176,7 @@ func (h *Handler) Withdraw(w http.ResponseWriter, r *http.Request) { return } - if !util.CheckLun(withdraw.Order) { + if !lun.CheckLun(withdraw.Order) { w.WriteHeader(http.StatusUnprocessableEntity) return } diff --git a/internal/util/hash.go b/internal/util/hash/hash.go similarity index 97% rename from internal/util/hash.go rename to internal/util/hash/hash.go index 825b477..8f3b28f 100644 --- a/internal/util/hash.go +++ b/internal/util/hash/hash.go @@ -1,4 +1,4 @@ -package util +package hash import "golang.org/x/crypto/bcrypt" diff --git a/internal/util/jwt.go b/internal/util/jwt/jwt.go similarity index 98% rename from internal/util/jwt.go rename to internal/util/jwt/jwt.go index 35dffb3..977d4e6 100644 --- a/internal/util/jwt.go +++ b/internal/util/jwt/jwt.go @@ -1,4 +1,4 @@ -package util +package jwt import ( "fmt" diff --git a/internal/util/lun.go b/internal/util/lun/lun.go similarity index 95% rename from internal/util/lun.go rename to internal/util/lun/lun.go index a7e2a7b..38c877e 100644 --- a/internal/util/lun.go +++ b/internal/util/lun/lun.go @@ -1,4 +1,4 @@ -package util +package lun import "strconv"