Skip to content

Commit

Permalink
chore(project): done project refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
breadrock1 committed May 29, 2024
1 parent 11f4121 commit e943aa7
Show file tree
Hide file tree
Showing 32 changed files with 429 additions and 507 deletions.
22 changes: 20 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ linters-settings:
lines: 150
statements: 80

depguard:
rules:
main:
allow:
- "github.com/disintegration/imaging"
- "github.com/google/uuid"
- "github.com/hashicorp/golang-lru/v2/expirable"
- "github.com/labstack/echo/v4"
- "github.com/labstack/echo/v4/middleware"
- "github.com/labstack/gommon/log"
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
- "github.com/swaggo/echo-swagger"
- "github.com/stretchr/testify/require"

revive:
rules:
- name: blank-imports
disabled: true

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- depguard
- dogsled
- dupl
- durationcheck
Expand All @@ -26,7 +45,6 @@ linters:
- gocyclo
- godot
- gofmt
- gofumpt
- goheader
- gomoddirectives
- gomodguard
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ build-img:
run-img: build-img
docker run --publish 2891:2891 $(DOCKER_IMG)

run-compose:
docker-compose -f deployments/docker-compose.yml up -d resizer

install-lint-deps:
(which golangci-lint > /dev/null) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.50.1

Expand Down
49 changes: 3 additions & 46 deletions cmd/resizer/resizer.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/labstack/gommon/log"
"image-resize-service/cmd"
"image-resize-service/internal/app"
"image-resize-service/internal/cacher"
"image-resize-service/internal/logger"
"image-resize-service/internal/resizer"
"image-resize-service/internal/server/http"
"image-resize-service/internal/storage"
"image-resize-service/internal/pkg/app"
)

func main() {
config := cmd.Execute()

sCache := cacher.New(&config.Cacher)
sLog := logger.New(&config.Logger)
sRes := resizer.New(&config.Resizer)
sStore := storage.New(&config.Storage)
sApp := app.New(sCache, sLog, sRes, sStore)

ctx, cancel := context.WithCancel(context.Background())
go awaitSystemSignals(cancel)

server := http.New(&config.Server, sApp)
go func() {
if err := server.Start(ctx); err != nil {
sLog.Error(err.Error())
cancel()
}
}()

<-ctx.Done()
cancel()
shutdownServer(ctx, server)
}

func awaitSystemSignals(cancel context.CancelFunc) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
cancel()
}

func shutdownServer(ctx context.Context, server *http.Service) {
if err := server.Stop(ctx); err != nil {
log.Warnf("failed to stop server: %s", err)
return
}
resizeApp := app.New(config)
resizeApp.Run()
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"image-resize-service/internal/config"
"image-resize-service/internal/pkg/config"
)

var rootCmd = &cobra.Command{
Expand Down
22 changes: 22 additions & 0 deletions deployments/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.9"

services:
resizer:
image: resizer:latest
build:
context:
..
dockerfile:
build/Dockerfile
ports:
- "2891:2891"
networks:
- resizer-net
volumes:
- resizer-vol:/opt/resizer/uploads

volumes:
resizer-vol:

networks:
resizer-net:
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ require (

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/OpenPeeDeeP/depguard v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/OpenPeeDeeP/depguard v1.1.1 h1:TSUznLjvp/4IUP+OQ0t/4jF4QUyxIcVX8YnghZdunyA=
github.com/OpenPeeDeeP/depguard v1.1.1/go.mod h1:JtAMzWkmFEzDPyAd+W0NHl1lvpQKTvT9jnRVsohBKpc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,8 +21,6 @@ github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9Z
github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -39,7 +35,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -87,7 +82,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down Expand Up @@ -130,7 +124,6 @@ golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
24 changes: 0 additions & 24 deletions internal/app/app.go

This file was deleted.

34 changes: 34 additions & 0 deletions internal/app/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cache

import (
"time"

"github.com/hashicorp/golang-lru/v2/expirable"
"image-resize-service/internal/pkg/config"
)

type Memcache struct {
expire time.Duration
cacheClient *expirable.LRU[string, string]
}

func Create(config *config.CacheConfig) Memcache {
expireTime := time.Duration(config.ExpireSeconds) * time.Second
cacheInst := expirable.NewLRU[string, string](config.CapacityValues, nil, expireTime)
return Memcache{
expire: expireTime,
cacheClient: cacheInst,
}
}

func (m *Memcache) GetValue(address string) (string, bool) {
value, ok := m.cacheClient.Get(address)
if !ok {
return "", false
}
return value, true
}

func (m *Memcache) StoreValue(address string, imagePath string) {
m.cacheClient.Add(address, imagePath)
}
19 changes: 19 additions & 0 deletions internal/app/cache/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cache

import (
"image-resize-service/internal/pkg/config"
)

type Cache interface {
Service
}

type Service interface {
GetValue(address string) (string, bool)
StoreValue(address string, imagePath string)
}

func New(config *config.CacheConfig) Cache {
cacheService := Create(config)
return &cacheService
}
7 changes: 2 additions & 5 deletions internal/resizer/public.go → internal/app/resizer/public.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package resizer

import (
"image-resize-service/internal/config"
"image-resize-service/internal/resizer/disimage"
)
import "image-resize-service/internal/pkg/config"

type Resizer interface {
ResizeService
Expand All @@ -14,6 +11,6 @@ type ResizeService interface {
}

func New(config *config.ResizerConfig) Resizer {
resizeService := disimage.New(config)
resizeService := CreateResizer(config)
return &resizeService
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package disimage
package resizer

import (
"bytes"
"errors"
"image"

"github.com/disintegration/imaging"
"image-resize-service/internal/config"
"image-resize-service/internal/pkg/config"
)

type Service struct {
TargetQuality int
}

func New(config *config.ResizerConfig) Service {
func CreateResizer(config *config.ResizerConfig) Service {
return Service{
TargetQuality: config.TargetQuality,
}
Expand Down
61 changes: 61 additions & 0 deletions internal/app/server/forms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package server

import (
"errors"
"net/url"
"strconv"

"github.com/labstack/echo/v4"
)

// ResponseForm example.
type ResponseForm struct {
Status int `json:"status" example:"200"`
Message string `json:"message" example:"Done"`
}

// BadRequestForm example.
type BadRequestForm struct {
Status int `json:"status" example:"400"`
Message string `json:"message" example:"Bad Request message"`
}

// InternalErrorForm example.
type InternalErrorForm struct {
Status int `json:"status" example:"503"`
Message string `json:"message" example:"Server Error message"`
}

func createStatusResponse(status int, msg string) *ResponseForm {
return &ResponseForm{Status: status, Message: msg}
}

type FillFormParams struct {
Height int
Width int
ImageAddr *url.URL
}

func (s *Service) extractFormParams(c echo.Context) (*FillFormParams, error) {
var extractErr error
var width, height int
var imageAddr *url.URL

if width, extractErr = strconv.Atoi(c.Param("width")); extractErr != nil {
return nil, errors.New("incorrect request width param")
}

if height, extractErr = strconv.Atoi(c.Param("height")); extractErr != nil {
return nil, errors.New("incorrect request height param")
}

if imageAddr, extractErr = url.Parse(c.Param("image")); extractErr != nil {
return nil, errors.New("incorrect request image path param")
}

return &FillFormParams{
Height: height,
Width: width,
ImageAddr: imageAddr,
}, nil
}
Loading

0 comments on commit e943aa7

Please sign in to comment.