Skip to content

Commit

Permalink
Merge pull request #4 from korovindenis/iter16
Browse files Browse the repository at this point in the history
Iter16
  • Loading branch information
korovindenis authored Dec 26, 2023
2 parents 365e3ad + 137393c commit 216431d
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 16 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
# Go workspace file
go.work

.vscode
.vscode

heap

pgadmindata
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gotest:
go test `go list ./... | grep -v test` -count 1

gotestcover:
go test -covermode=count -coverprofile=coverage.out $(shell go list ./... | egrep -v '(/test|/test/mock)$$')
go test -covermode=count -coverprofile=coverage.out $(shell go list ./... | egrep -v '(/test|/test/mock)$$') && go tool cover -func cover.out

get:
@echo " > Checking dependencies"
Expand Down
6 changes: 6 additions & 0 deletions cmd/gophermart/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Main module
// Dependencies are initialized, and the HTTP server is started.
package main

import (
Expand All @@ -17,8 +19,12 @@ import (
"go.uber.org/zap"
)

// Return code for
const (
// a successful exit from the application
ExitSucces = iota

// an unsuccessful exit from the application
ExitWithError
)

Expand Down
5 changes: 5 additions & 0 deletions internal/adapters/accrual/accrual.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (
"github.com/korovindenis/go-market/internal/domain/entity"
)

// max goroutine
const maxWorker = 10

// get/set for BD
type storage interface {
GetAllNotProcessedOrders(ctx context.Context) ([]entity.Order, error)

SetOrderStatusAndAccrual(ctx context.Context, order entity.Order) error
}

// configuration
type config interface {
GetAccrualAddress() string
}
Expand All @@ -27,6 +31,7 @@ type Accrual struct {
config
}

// response data
type accrualRespose struct {
Number string `json:"order"`
Status string `json:"status"`
Expand Down
1 change: 1 addition & 0 deletions internal/adapters/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Auth struct {
config
}

// info in jwt token
type claims struct {
entity.User
jwt.RegisteredClaims
Expand Down
1 change: 1 addition & 0 deletions internal/adapters/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const configDefaultPath = "./configs/config.dev.yaml"

// data in configDefaultPath
type config struct {
App `koanf:"app"`
Httpserver `koanf:"http_server"`
Expand Down
1 change: 1 addition & 0 deletions internal/adapters/ctxinfo/ctxinfo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Work with context
package ctxinfo

import (
Expand Down
1 change: 1 addition & 0 deletions internal/adapters/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// log data
package logger

import (
Expand Down
1 change: 1 addition & 0 deletions internal/adapters/storage/postgresql/init.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// work with db
package postgresql

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/domain/entity/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"github.com/ShiraazMoollatjie/goluhn"
)

// struct for user Balance
type Balance struct {
Current float64 `json:"current"`
Withdrawn float64 `json:"withdrawn"`
}

// struct for update user Balance
type BalanceUpdate struct {
Order string `json:"order"`
Sum float64 `json:"sum"`
Expand Down
2 changes: 2 additions & 0 deletions internal/domain/entity/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Domain entities
package entity
19 changes: 7 additions & 12 deletions internal/domain/entity/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package entity

import "errors"

// define errors
var (
// ErrMetricNotFound = errors.New("metric not set")
ErrEnvVarNotFound = errors.New("env var not set")
// ErrInvalidURLFormat = errors.New("invalid URL format")
ErrMethodNotAllowed = errors.New("method not allowed")
ErrUnsupportedMediaType = errors.New("unsupported media type")
ErrInternalServerError = errors.New("internal server error")
// ErrInputVarIsWrongType = errors.New("metric value is wrong type")
ErrEnvVarNotFound = errors.New("env var not set")
ErrMethodNotAllowed = errors.New("method not allowed")
ErrUnsupportedMediaType = errors.New("unsupported media type")
ErrInternalServerError = errors.New("internal server error")
ErrStatusBadRequest = errors.New("bad request")
ErrUserLoginNotUnique = errors.New("login not unique")
ErrUserLoginUnauthorized = errors.New("user unauthorized")
ErrUnprocessableEntity = errors.New("unprocessable entity")
ErrOrderAlreadyUploadedAnotherUser = errors.New("order already uploaded another user")
ErrOrderAlreadyUploaded = errors.New("order already uploaded")
// ErrInvalidGzipData = errors.New("invalid gzip data")
// ErrReadingRequestBody = errors.New("error reading request body")
ErrNoContent = errors.New("no content")
// ErrNotImplementedServerError = errors.New("not implemented server error")
ErrInsufficientBalance = errors.New("insufficient balance")
ErrNoContent = errors.New("no content")
ErrInsufficientBalance = errors.New("insufficient balance")
)
2 changes: 2 additions & 0 deletions internal/domain/entity/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ShiraazMoollatjie/goluhn"
)

// order status
const (
StatusNew = "NEW"
StatusRegistered = "REGISTERED"
Expand All @@ -14,6 +15,7 @@ const (
StatusProcessing = "PROCESSING"
)

// struct for user Order
type Order struct {
Number string `json:"number"`
Status string `json:"Status"`
Expand Down
1 change: 1 addition & 0 deletions internal/domain/entity/user.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package entity

// struct for User
type User struct {
Login string `json:"login" binding:"required"`
Password string `json:"password" binding:"required"`
Expand Down
2 changes: 2 additions & 0 deletions internal/domain/usecases/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Domain usecases
package usecases
2 changes: 2 additions & 0 deletions internal/port/http/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/korovindenis/go-market/internal/domain/entity"
)

// Used during user registration
func (h *Handler) Register(c *gin.Context) {
ctx := c.Request.Context()
var user entity.User
Expand Down Expand Up @@ -118,6 +119,7 @@ func (h *Handler) Login(c *gin.Context) {
c.Status(http.StatusOK)
}

// Used during user authentication
func (h *Handler) createCookie(token string) (*http.Cookie, error) {
return &http.Cookie{
Name: h.GetTokenName(),
Expand Down
1 change: 1 addition & 0 deletions internal/port/http/handler/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/korovindenis/go-market/internal/domain/entity"
)

// Displays the user's balance, returned as entity.Balance
func (h *Handler) GetBalance(c *gin.Context) {
userID, err := h.GetUserIDFromCtx(c)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/port/http/handler/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Module with handlers
package handler
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type callTimes struct {
getUser int
}

func Example() {
t := &testing.T{}

// run test with testify
TestHandler_AuthRegister(t)
TestHandler_AuthLogin(t)
}

func TestHandler_AuthRegister(t *testing.T) {
config := mocks.NewConfig(t)
usecase := mocks.NewUsecase(t)
Expand Down
2 changes: 2 additions & 0 deletions internal/port/http/handler/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/korovindenis/go-market/internal/domain/entity"
)

// Used to add a new order
func (h *Handler) SetOrder(c *gin.Context) {
ctx := c.Request.Context()

Expand Down Expand Up @@ -61,6 +62,7 @@ func (h *Handler) SetOrder(c *gin.Context) {
c.Status(http.StatusAccepted)
}

// Returns a list of all purchases
func (h *Handler) GetAllOrders(c *gin.Context) {
ctx := c.Request.Context()
userID, err := h.GetUserIDFromCtx(c)
Expand Down
1 change: 1 addition & 0 deletions internal/port/http/handler/withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/korovindenis/go-market/internal/domain/entity"
)

// handler for get Withdrawals
func (h *Handler) Withdrawals(c *gin.Context) {
ctx := c.Request.Context()
userID, err := h.GetUserIDFromCtx(c)
Expand Down
10 changes: 8 additions & 2 deletions internal/port/http/server/server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// init api (http server)
package server

import (
Expand All @@ -10,9 +11,11 @@ import (

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
//"github.com/gin-contrib/pprof"

"github.com/gin-contrib/pprof"
)

// handler for http server
type handler interface {
Register(c *gin.Context)
Login(c *gin.Context)
Expand All @@ -26,6 +29,7 @@ type handler interface {
Withdrawals(c *gin.Context)
}

// middleware for http server
type middleware interface {
CheckMethod() gin.HandlerFunc

Expand All @@ -36,6 +40,7 @@ type middleware interface {
AddUserInfoToCtx() gin.HandlerFunc
}

// configuration
type config interface {
GetServerAddress() string
GetServerMode() string
Expand All @@ -45,6 +50,7 @@ type config interface {
GetServerMaxHeaderBytes() int
}

// run http server
func Run(ctx context.Context, config config, handler handler, middleware middleware) error {
// init http
gin.SetMode(config.GetServerMode())
Expand Down Expand Up @@ -75,7 +81,7 @@ func Run(ctx context.Context, config config, handler handler, middleware middlew
}

// add pprof
//pprof.Register(router)
pprof.Register(router)

// server settings
srv := &http.Server{
Expand Down
Binary file added profiles/base.pprof
Binary file not shown.
25 changes: 25 additions & 0 deletions profiles/pprof diff.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
go tool pprof -top -diff_base=.\profiles\base.pprof .\profiles\result.pprof

File: main.exe
Type: inuse_space
Time: Dec 21, 2023 at 1:46pm (MSK)
Showing nodes accounting for 4.02MB, 100% of 4.02MB total
flat flat% sum% cum cum%
1.01MB 25.13% 25.13% 1.01MB 25.13% github.com/go-playground/validator/v10.map.init.1
1.01MB 25.13% 50.26% 1.01MB 25.13% regexp/syntax.(*compiler).inst (inline)
1MB 24.87% 75.13% 1MB 24.87% regexp/syntax.(*parser).newRegexp (inline)
1MB 24.87% 100% 1MB 24.87% github.com/gabriel-vasile/mimetype/internal/magic.init
0 0% 100% 2.02MB 50.26% github.com/go-playground/validator/v10.init
0 0% 100% 1MB 24.87% github.com/go-playground/validator/v10.init.0
0 0% 100% 2.01MB 50.00% regexp.Compile (inline)
0 0% 100% 2.01MB 50.00% regexp.MustCompile
0 0% 100% 2.01MB 50.00% regexp.compile
0 0% 100% 1.01MB 25.13% regexp/syntax.(*compiler).compile
0 0% 100% 1.01MB 25.13% regexp/syntax.(*compiler).rune
0 0% 100% 1MB 24.87% regexp/syntax.(*parser).literal
0 0% 100% 1.01MB 25.13% regexp/syntax.Compile
0 0% 100% 1MB 24.87% regexp/syntax.Parse (inline)
0 0% 100% 1MB 24.87% regexp/syntax.parse
0 0% 100% 4.02MB 100% runtime.doInit (inline)
0 0% 100% 4.02MB 100% runtime.doInit1
0 0% 100% 4.02MB 100% runtime.main
Binary file added profiles/result.pprof
Binary file not shown.

0 comments on commit 216431d

Please sign in to comment.