Skip to content

Commit

Permalink
refactor accrual, handler and package util
Browse files Browse the repository at this point in the history
  • Loading branch information
zelhat committed Jan 23, 2024
1 parent 60838f1 commit 2f5f087
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
22 changes: 5 additions & 17 deletions internal/handler/accrual/accrual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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())
}
}
}
4 changes: 2 additions & 2 deletions internal/handler/checkauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/hash.go → internal/util/hash/hash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package hash

import "golang.org/x/crypto/bcrypt"

Expand Down
2 changes: 1 addition & 1 deletion internal/util/jwt.go → internal/util/jwt/jwt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package jwt

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/util/lun.go → internal/util/lun/lun.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package lun

import "strconv"

Expand Down

0 comments on commit 2f5f087

Please sign in to comment.