-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(project): done project refactoring
- Loading branch information
1 parent
11f4121
commit e943aa7
Showing
32 changed files
with
429 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
internal/resizer/disimage/resizer.go → internal/app/resizer/resizer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.