Skip to content

Commit

Permalink
refactor: small improve (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
microup authored Apr 23, 2024
1 parent bda2dc2 commit 21437ab
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 41 deletions.
23 changes: 11 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
PROJECT_NAME="vbalancer"

.PHONY: all dep build test lint mocks

all: lint test race build

pre-push: lint test race
pre-push: lint test race

build-mocks:
go get github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen

go install github.com/golang/mock/[email protected]

mocks:
mockgen -destination=mocks/mock_peer.go -package=mocks -source=./internal/proxy/peer/peer.go Peer
mockgen -destination=mocks/mock_vlog.go -package=mocks -source=./internal/vlog/vlog.go ILog

init:
init:
go mod tidy
go mod vendor

fmt:
go fmt ./...
go fmt ./...

lint:
lint:
go vet ./...
golangci-lint run -v ./...

test:
test:
go test -v ./...

race: dep ## Run data race detector
go test -race -v ./...

build:
build:
go build -o build/$(PROJECT_NAME) cmd/$(PROJECT_NAME)/$(PROJECT_NAME).go

docker-create:
docker build --tag vbalancer . -f Dockerfile

docker-run:
docker run --restart=always -p 8080:8080 vbalancer
docker run --restart=always -p 8080:8080 vbalancer

docker-delete:
docker rmi vbalancer
4 changes: 2 additions & 2 deletions internal/app/vbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var ErrRecoveredPanic = errors.New("recovered from panic")

// Run this is the function of an application that starts a proxy server.
//
//nolint:funlen,cyclop
//nolint:funlen, cyclop
func Run() {
runtime.GOMAXPROCS(runtime.NumCPU())

Expand Down Expand Up @@ -56,7 +56,7 @@ func Run() {
}
}()

proxy, err := core.GetObjectFromMap(cfg.Proxy, proxy.New())
proxy, err := core.YamlToObject(cfg.Proxy, proxy.New())
if err != nil {
logger.Add(vlog.Fatal, types.ErrCantGetProxyObject, "can't get proxy object")
}
Expand Down
14 changes: 7 additions & 7 deletions internal/core/archiv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ func helperArchiveFile(t *testing.T) {

testFile, err := os.Create(fileName)

assert.Nil(t, err, "failed to create test file")
assert.NoError(t, err, "failed to create test file")

defer os.Remove(fileName)

_, err = testFile.Write([]byte("test data"))

assert.Nil(t, err, "failed to write data to test file")
assert.NoError(t, err, "failed to write data to test file")

err = testFile.Close()

assert.Nil(t, err, "failed to close test file")
assert.NoError(t, err, "failed to close test file")

err = core.ArchiveFile(fileName, extension)

assert.Nil(t, err, "archiving failed")
assert.NoError(t, err, "archiving failed")

archivedFile := strings.TrimSuffix(fileName, filepath.Ext(fileName)) + extension
if _, err = os.Stat(archivedFile); os.IsNotExist(err) {
assert.FailNow(t, "archived file does not exist", err)
}

assert.Nil(t, err, "archiving failed")
assert.NoError(t, err, "archiving failed")

defer os.Remove(archivedFile)

Expand All @@ -66,13 +66,13 @@ func helperArchiveFile(t *testing.T) {
fileInArchive := zipFile.File[0]
zipFileContent, err := fileInArchive.Open()

assert.Nil(t, err, "failed to open file in archive")
assert.NoError(t, err, "failed to open file in archive")

defer zipFileContent.Close()

data, err := io.ReadAll(zipFileContent)

assert.Nil(t, err, "failed to read data from file in archive")
assert.NoError(t, err, "failed to read data from file in archive")

assert.Equal(t, "test data", string(data), "unexpected data in archived file")
}
11 changes: 5 additions & 6 deletions internal/core/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ import (
var ErrIncorectType = errors.New("incorrect types")

//nolint
func GetObjectFromMap[T any](objectMap any, unmarshalObject T) (T, error) {
func YamlToObject[T any](yamlData any, unmarshalObject T) (T, error) {
var objectBytes []byte

var err error

switch p := objectMap.(type) {
switch p := yamlData.(type) {
case map[interface{}]interface{}:
objectBytes, err = yaml.Marshal(p)
if err != nil {
return unmarshalObject, fmt.Errorf("failed to marshal yamlData: %w", err)
}
default:
return unmarshalObject, fmt.Errorf("%w for : %T", ErrIncorectType, p)
}

if err != nil {
return unmarshalObject, fmt.Errorf("failed to marshal objectMap: %w", err)
}

err = yaml.Unmarshal(objectBytes, &unmarshalObject)
if err != nil {
return unmarshalObject, fmt.Errorf("failed to unmarshal output object: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func TestFmtStringWithDelimiter(t *testing.T) {
for _, test := range testCases {
result := core.FmtStringWithDelimiter(delimiter, test.values...)

assert.Equal(t, result, test.want)
assert.Equal(t, test.want, result)
}
}
3 changes: 1 addition & 2 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var ErrConfigPeersIsNil = errors.New("empty list peer in config file")

// Proxy defines the structure for the proxy server.
type Proxy struct {
//
Logger vlog.ILog `yaml:"-" json:"-"`
// Define the default port to listen on
Port string `yaml:"port" json:"port"`
Expand Down Expand Up @@ -239,7 +238,7 @@ func (p *Proxy) updatePort() types.ResultCode {
var proxyPort string

if p.Port == "" || p.Port == ":" {
proxyPort = os.Getenv("ProxyPort")
proxyPort = os.Getenv(types.ProxyPort)
if proxyPort == ":" || proxyPort == "" {
proxyPort = types.DefaultProxyPort
}
Expand Down
21 changes: 11 additions & 10 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"net"
"os"
"testing"

"vbalancer/internal/proxy/peer"
Expand Down Expand Up @@ -70,9 +69,9 @@ func TestCheckNewConnection(t *testing.T) {
// TestGetProxyPort tests the UpdatePort function.
// It validates UpdatePort handles invalid environment variable values,
// default values, and valid custom environment variable values correctly.
//
//nolint:paralleltest
func TestGetProxyPort(t *testing.T) {
t.Parallel()

testCases := []struct {
port string
name string
Expand Down Expand Up @@ -119,16 +118,18 @@ func TestGetProxyPort(t *testing.T) {
//nolint:exhaustivestruct,exhaustruct
prx := &Proxy{}

for _, test := range testCases {
prx.Port = test.port
for _, tc := range testCases {
testCase := tc
t.Run(testCase.name, func(t *testing.T) {
prx.Port = testCase.port

os.Clearenv()
os.Setenv("ProxyPort", test.envVar)
t.Setenv(types.ProxyPort, testCase.envVar)

result := prx.updatePort()
result := prx.updatePort()

assert.Equalf(t, result, test.want, "name: `%s`", test.name)
assert.Equal(t, testCase.want, result, "name: `%s`")

assert.Equalf(t, prx.Port, test.wantValue, "name: `%s`", test.name)
assert.Equal(t, testCase.wantValue, prx.Port, "name: `%s`")
})
}
}
2 changes: 1 addition & 1 deletion internal/proxy/rules/blacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestBlacklist_CheckIpBlacklist(t *testing.T) {
for _, test := range testCases {
err := test.b.Init(ctx)

assert.Nil(t, err, "name: `%s`", test.name)
assert.NoError(t, err, "name: `%s`", test.name)

assert.Equalf(t, test.b.IsBlacklistIP(test.checkedIP), test.want, "name: `%s`", test.name)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/types/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ const (
//
DeafultCountMaxDialAttemptsToPeer = 30
)

// ENVIREMENT.
const (
ProxyPort = "ProxyPort"
)

0 comments on commit 21437ab

Please sign in to comment.