diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100755 index 0000000..4117f0a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,33 @@ +on: + push: + branches: + - v* + +env: + GO111MODULE: "on" + +jobs: + tests_by_makefile: + runs-on: ubuntu-latest + steps: + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ^1.24 + + - name: Check out code + uses: actions/checkout@v3 + + - name: Install libvips-dev + run: | + sudo apt-get update + sudo apt-get install -y libvips-dev + + - name: make lint + run: make lint + + - name: make build + run: make build + + - name: make test + run: make test \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7faf2e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env + +.golangci.bck.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..6c25252 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,83 @@ +version: "2" +run: + build-tags: + - bench + - "" + tests: true +linters: + default: none + enable: + - asciicheck + - bodyclose + - depguard + - dogsled + - dupl + - durationcheck + - errorlint + - exhaustive + - funlen + - gocognit + - goconst + - gocritic + - gocyclo + - godot + - goheader + - goprintffuncname + - gosec + - govet + - importas + - ineffassign + - lll + - makezero + - misspell + - nestif + - nilerr + - noctx + - nolintlint + - prealloc + - predeclared + - revive + - staticcheck + - tagliatelle + - thelper + - unconvert + - unparam + - unused + - whitespace + settings: + funlen: + lines: 150 + statements: 80 + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - linters: + - depguard + - dupl + - errcheck + - gocyclo + - gosec + path: _test\.go + - linters: + - depguard + path: .*\.go + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - gci + - gofmt + - gofumpt + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/.scripts/lint.sh b/.scripts/lint.sh new file mode 100755 index 0000000..4d6aa42 --- /dev/null +++ b/.scripts/lint.sh @@ -0,0 +1,17 @@ +#!/bin/zsh + +sed -i.bak '/- deadcode/d' .golangci.yml +sed -i '' '/- unused/d' .golangci.yml +sed -i '' '/- structcheck/d' .golangci.yml + +for d in $(ls) +do + if [[ $d == internal ]]; then + cd $d + echo "Lint ${d}..." + golangci-lint run ./... + cd .. + fi +done + +mv .golangci.yml.bak .golangci.yml diff --git a/.scripts/sync.sh b/.scripts/sync.sh new file mode 100755 index 0000000..6ab8c46 --- /dev/null +++ b/.scripts/sync.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +dst=$1 +if [[ ! -d "${dst}" ]]; then + echo "Usage: ./.scripts/sync.sh ." + echo "The destination dir should exist" + exit 1 +fi + +GLOBIGNORE=".:..:.git" +for f in *; do + [[ -d "${dst}/${f}" ]] && [[ ! -f "${dst}/${f}/.sync" ]] && continue + + echo "syncing ${f}..." + cp -R "${f}" "${dst}" +done diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a77f32b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Stas Demin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3153fa4 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +BIN := "./bin/previewer" +DOCKER_IMG="previewer:develop" +DOCKER_TEST_IMG="previewer:test" + +GIT_HASH := $(shell git log --format="%h" -n 1) +LDFLAGS := -X main.release="develop" -X main.buildDate=$(shell date -u +%Y-%m-%dT%H:%M:%S) -X main.gitHash=$(GIT_HASH) + +build: + go build -v -o $(BIN) -ldflags "$(LDFLAGS)" ./cmd/previewer + +run: build + $(BIN) -config ./configs/config.toml + +build-img: + docker build \ + --build-arg=LDFLAGS="$(LDFLAGS)" \ + -t $(DOCKER_IMG) \ + -f build/Dockerfile . + +run-img: build-img + docker run $(DOCKER_IMG) + +version: build + $(BIN) version + +test: + go test -race ./internal/... + +integration-test: + set -e ;\ + docker build -t $(DOCKER_TEST_IMG) -f tests/Dockerfile . + test_status_code=0 ;\ + docker run $(DOCKER_TEST_IMG) go test || test_status_code=$$? ;\ + docker stop $(DOCKER_TEST_IMG) ;\ + exit $$test_status_code ; + +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 v2.1.6 + +lint: install-lint-deps + golangci-lint run ./... + +.PHONY: build run build-img run-img version test lint diff --git a/README.md b/README.md new file mode 100644 index 0000000..fd6ce15 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# ТЗ на разработку сервиса "Превьювер изображений" + +## Общее описание +Сервис предназначен для изготовления preview (создания изображения +с новыми размерами на основе имеющегося изображения). + +#### Пример превьюшек в папке [examples](./examples/image-previewer) + +## Архитектура +Сервис представляет собой web-сервер (прокси), загружающий изображения, +масштабирующий/обрезающий их до нужного формата и возвращающий пользователю. + +## Основной обработчик +http://cut-service.com/fill/300/200/raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg + +<---- микросервис ----><- размеры превью -><--------- URL исходного изображения ---------------------------------> + +В URL выше мы видим: +- http://cut-service.com/fill/300/200/ - endpoint нашего сервиса, + в котором 300x200 - это размеры финального изображения. +- https://raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg - + адрес исходного изображения; сервис должен скачать его, произвести resize, закэшировать и отдать клиенту. + +Сервис должен получить URL исходного изображения, скачать его, изменить до необходимых размеров и вернуть как HTTP-ответ. + +- Работаем только с HTTP. +- Ошибки удалённого сервиса или проксируем как есть, или логируем и отвечаем клиенту 502 Bad Gateway. +- Поддержка JPEG является минимальным и достаточным требованием. + +**Важно**: необходимо проксировать все заголовки исходного HTTP запроса к целевому сервису (raw.githubusercontent.com в примере). + +Сервис должен сохранить (кэшировать) полученное preview на локальном диске и при повторном запросе +отдавать изображение с диска, без запроса к удаленному HTTP-серверу. + +Поскольку размер места для кэширования ограничен, то для удаления редко используемых изображений +необходимо использовать алгоритм **"Least Recent Used"**. + +## Конфигурация +Основной параметр конфигурации сервиса - разрешенный размер LRU-кэша. + +Он может измеряться как количеством закэшированных изображений, так и суммой их байт (на выбор разработчика). + +## Развертывание +Развертывание микросервиса должно осуществляться командой `make run` (внутри `docker compose up`) +в директории с проектом. + +## Тестирование +Реализацию алгоритма LRU нужно покрыть unit-тестами. + +Для интеграционного тестирования можно использовать контейнер с Nginx в качестве удаленного HTTP-сервера, +раздающего вам заданный набор изображений. + +Необходимо проверить работу сервера в разных сценариях: +* картинка найдена в кэше; +* удаленный сервер не существует; +* удаленный сервер существует, но изображение не найдено (404 Not Found); +* удаленный сервер существует, но изображение не изображение, а скажем, exe-файл; +* удаленный сервер вернул ошибку; +* удаленный сервер вернул изображение; +* изображение меньше, чем нужный размер; + и пр. + +## Разбалловка +Максимум - **15 баллов** +(при условии выполнения [обязательных требований](./README.md)): + +* Реализован HTTP-сервер, проксирующий запросы к удаленному серверу - 2 балла. +* Реализована нарезка изображений - 2 балла. +* Кэширование нарезанных изображений на диске - 1 балл. +* Ограничение кэша одним из способов (LRU кэш) - 1 балл. +* Прокси сервер правильно передает заголовки запроса - 1 балл. +* Написаны интеграционные тесты - 3 балла. +* Тесты адекватны и полностью покрывают функциональность - 1 балл. +* Проект возможно собрать через `make build`, запустить через `make run` + и протестировать через `make test` - 1 балл. +* Понятность и чистота кода - до 3 баллов. + +#### Зачёт от 10 баллов \ No newline at end of file diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..bf5fbad --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,46 @@ +# Собираем в гошке +FROM golang:1.24 AS build + +ENV BIN_FILE=/opt/previewer/previewer-app +ENV CODE_DIR=/go/src/ +ENV CC=gcc + +WORKDIR ${CODE_DIR} + +# Кэшируем слои с модулями +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY . ${CODE_DIR} + +RUN apt-get update && \ + apt-get -qq install -y libvips-dev + +# Собираем статический бинарник Go +ARG LDFLAGS +RUN CGO_ENABLED=1 PKG_CONFIG_PATH="/usr/lib/pkgconfig" go build \ + -ldflags "$LDFLAGS" \ + -o ${BIN_FILE} cmd/previewer/* + +# На выходе тонкий образ +FROM build AS base + +ENV CGO_ENABLED=1 +ENV TARGETOS=linux +ENV TARGETARCH=amd64 + +LABEL ORGANIZATION="OTUS Online Education" +LABEL SERVICE="previewer" +LABEL MAINTAINERS="otdupli@gmail.com" + +ENV BIN_FILE=/opt/previewer/previewer-app +COPY --from=build ${BIN_FILE} ${BIN_FILE} + +ENV CONFIG_FILE=/etc/previewer/config.toml + +COPY ./configs/config.toml ${CONFIG_FILE} + +RUN chmod +x ${BIN_FILE} + +CMD ["/opt/previewer/previewer-app", "-config", "/etc/previewer/config.toml"] \ No newline at end of file diff --git a/cmd/previewer/main.go b/cmd/previewer/main.go new file mode 100644 index 0000000..fb7a7d3 --- /dev/null +++ b/cmd/previewer/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "context" + "flag" + "fmt" + "net" + "os" + "os/signal" + "syscall" + "time" + + "github.com/DEMAxx/project_work/internal/lrucache" + internalhttp "github.com/DEMAxx/project_work/internal/server/http" + "github.com/DEMAxx/project_work/pkg/config" + "github.com/DEMAxx/project_work/pkg/logger" +) + +const timeout = time.Second * 3 + +var configFile string + +func init() { + flag.StringVar(&configFile, "config", "/etc/calendar/config.toml", "Path to configuration file") +} + +func main() { + flag.Parse() + + if flag.Arg(0) == "version" { + printVersion() + return + } + + cnf := config.MustLoad(configFile) + + logs := logger.MustSetupLogger(config.AppName, cnf.Env, cnf.Debug || cnf.Local, cnf.LogLevel) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + ctx = logs.WithContext(ctx) + + cache := lrucache.NewCache(cnf.Capability, cnf.UploadPath, logs) + + ctx, cancel = signal.NotifyContext(ctx, + syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + defer cancel() + + server := internalhttp.NewServer( + ctx, + &logs, + net.JoinHostPort(cnf.Server.Host, cnf.Server.Port), + cache, + cnf, + ) + + if err := server.Start(); err != nil { + logs.Error().Msg(fmt.Sprintf("failed to start http server: %s", err.Error())) + cancel() + os.Exit(1) //nolint:gocritic + } + + logs.Info().Msg("calendar is running...") + + <-ctx.Done() + + _, cancel = context.WithTimeout(context.Background(), timeout) + defer cancel() + + if err := server.Stop(); err != nil { + logs.Error().Msg(fmt.Sprintf("failed to stop http server: %s", err.Error())) + } +} diff --git a/cmd/previewer/version.go b/cmd/previewer/version.go new file mode 100644 index 0000000..7404eee --- /dev/null +++ b/cmd/previewer/version.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" +) + +var ( + release = "UNKNOWN" + buildDate = "UNKNOWN" + gitHash = "UNKNOWN" +) + +func printVersion() { + if err := json.NewEncoder(os.Stdout).Encode(struct { + Release string + BuildDate string + GitHash string + }{ + Release: release, + BuildDate: buildDate, + GitHash: gitHash, + }); err != nil { + fmt.Printf("error while decode version info: %v\n", err) + } +} diff --git a/configs/config.toml b/configs/config.toml new file mode 100644 index 0000000..c762a1b --- /dev/null +++ b/configs/config.toml @@ -0,0 +1,3 @@ +[Server] +SERVER_HOST = "localhost" +SERVER_PORT = "8001" \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..712a329 --- /dev/null +++ b/go.mod @@ -0,0 +1,32 @@ +module github.com/DEMAxx/project_work + +go 1.24.0 + +require ( + github.com/gofiber/fiber/v2 v2.52.6 + github.com/google/uuid v1.6.0 + github.com/h2non/bimg v1.1.9 + github.com/ilyakaznacheev/cleanenv v1.5.0 + github.com/rs/zerolog v1.34.0 + github.com/stretchr/testify v1.10.0 + golang.org/x/text v0.24.0 +) + +require ( + github.com/BurntSushi/toml v1.2.1 // indirect + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/joho/godotenv v1.5.1 // indirect + github.com/klauspost/compress v1.17.9 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.28.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0166a8c --- /dev/null +++ b/go.sum @@ -0,0 +1,57 @@ +github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI= +github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/h2non/bimg v1.1.9 h1:WH20Nxko9l/HFm4kZCA3Phbgu2cbHvYzxwxn9YROEGg= +github.com/h2non/bimg v1.1.9/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8= +github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= +github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= +github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= +golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ= +olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw= diff --git a/internal/filemodifier/filemodifier.go b/internal/filemodifier/filemodifier.go new file mode 100644 index 0000000..2f7f0bf --- /dev/null +++ b/internal/filemodifier/filemodifier.go @@ -0,0 +1,152 @@ +package filemodifier + +import ( + "context" + "errors" + "fmt" + "net/http" + "strconv" + "strings" + + "github.com/DEMAxx/project_work/internal/filesearch" + "github.com/DEMAxx/project_work/internal/lrucache" + "github.com/DEMAxx/project_work/pkg/config" + "github.com/h2non/bimg" + "github.com/rs/zerolog" +) + +type Modifier interface { + ResizeImage() ([]byte, error) + GetFromCache() (interface{}, bool) +} + +type fileModifier struct { + height int + width int + imageURL string + UploadPath string + fetchedFilePath string + cacheKey lrucache.Key + cache lrucache.Cache + logger *zerolog.Logger + ctx context.Context + r *http.Request +} + +func (fileModifier *fileModifier) ResizeImage() ([]byte, error) { + fetchedFilePath := fmt.Sprintf( + "%s/%d_%d.jpg", + fileModifier.UploadPath, + fileModifier.width, + fileModifier.height, + ) + + client := filesearch.NewClient(fileModifier.ctx, fileModifier.r) + + resp, err := client.FetchFileFromURL(fileModifier.imageURL, fetchedFilePath, fileModifier.logger) //nolint + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.New("failed to fetch image") + } + + image, err := bimg.Read(fileModifier.fetchedFilePath) + if err != nil { + return nil, err + } + + resizedImage, err := bimg.NewImage(image).Process(bimg.Options{ + Width: fileModifier.width, + Height: fileModifier.height, + Crop: true, + Type: bimg.JPEG, + }) + if err != nil { + return nil, err + } + + if err := fileModifier.cache.Set(fileModifier.cacheKey, resizedImage); err { + return nil, errors.New("failed to store image in cache") + } + + return resizedImage, nil +} + +func (fileModifier *fileModifier) GetFromCache() (cachedImage interface{}, found bool) { + cacheKey := lrucache.Key( + fmt.Sprintf( + "%d_%d_%s", + fileModifier.width, + fileModifier.height, + fileModifier.imageURL, + ), + ) + + return fileModifier.cache.Get(cacheKey) +} + +func New( + ctx context.Context, + parts []string, + logger *zerolog.Logger, + cnf *config.Config, + cache lrucache.Cache, + r *http.Request, +) (Modifier, error) { + if len(parts) < 3 { + return nil, errors.New("not enough parts") + } + + height, width, imageURL := parts[0], parts[1], strings.Join(parts[2:], "/") + + logger.Info().Msg( + fmt.Sprintf( + "Extracted vars - height: %s, width: %s, image url: %s", height, width, imageURL, + ), + ) + + if !strings.HasSuffix(imageURL, ".jpg") { + return nil, errors.New("invalid image URL format. Only .jpg files are supported") + } + + // Resize the image + widthInt, err := strconv.Atoi(width) + if err != nil { + return nil, errors.New("invalid width value") + } + + heightInt, err := strconv.Atoi(height) + if err != nil { + return nil, errors.New("invalid height value") + } + + if widthInt <= 0 || heightInt <= 0 { + return nil, errors.New("width or height must be positive") + } + + fetchedFilePath := fmt.Sprintf("%s/%s_%s.jpg", cnf.UploadPath, width, height) + + cacheKey := lrucache.Key( + fmt.Sprintf( + "%d_%d_%s", + widthInt, + heightInt, + imageURL, + ), + ) + + return &fileModifier{ + height: heightInt, + width: widthInt, + imageURL: imageURL, + UploadPath: cnf.UploadPath, + fetchedFilePath: fetchedFilePath, + cacheKey: cacheKey, + cache: cache, + logger: logger, + ctx: ctx, + r: r, + }, nil +} diff --git a/internal/filemodifier/filemodifier_test.go b/internal/filemodifier/filemodifier_test.go new file mode 100644 index 0000000..79e668b --- /dev/null +++ b/internal/filemodifier/filemodifier_test.go @@ -0,0 +1,220 @@ +package filemodifier + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + "testing" + + "github.com/DEMAxx/project_work/internal/lrucache" + "github.com/DEMAxx/project_work/pkg/config" + "github.com/DEMAxx/project_work/pkg/logger" + "github.com/stretchr/testify/assert" +) + +// Путь к директории с тестовыми изображениями. +const testImagesDir = "testdata" + +var cnf = config.Config{ + UploadPath: testImagesDir, + Capability: 1, +} + +func TestResizeImage(t *testing.T) { + fileURL := "raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg" //nolint + log := logger.MustSetupLogger("previewer", "Test", true, "info") + cache := lrucache.NewCache(cnf.Capability, cnf.UploadPath, log) + ctx := context.Background() + r := &http.Request{ + Method: "GET", + URL: &url.URL{ + Scheme: "http", + Host: "localhost", + Path: cnf.UploadPath, + }, + } + + t.Run("success", func(t *testing.T) { + path := fmt.Sprintf("%d/%d/%s", 100, 100, fileURL) + + modifier, err := New(ctx, strings.Split(path, "/"), &log, &cnf, cache, r) + + assert.NoError(t, err) + + cachedImage, found := modifier.GetFromCache() + + assert.False(t, found) + assert.Nil(t, cachedImage) + + resizedImage, err := modifier.ResizeImage() + + assert.NoError(t, err) + assert.NotNil(t, resizedImage) + + cache.Clear() + }) + + t.Run("success different dimensions", func(t *testing.T) { + path := fmt.Sprintf( + "%d/%d/%s", + 200, + 200, + fileURL, + ) + + modifier, err := New(ctx, strings.Split(path, "/"), &log, &cnf, cache, r) + + assert.NoError(t, err) + + cachedImage, found := modifier.GetFromCache() + + assert.False(t, found) + assert.Nil(t, cachedImage) + + resizedImage, err := modifier.ResizeImage() + + assert.NoError(t, err) + assert.NotNil(t, resizedImage) + + cache.Clear() + + path = fmt.Sprintf( + "%d/%d/%s", + 200, + 200, + fileURL, + ) + + modifier, err = New( + ctx, + strings.Split(path, "/"), + &log, + &cnf, + cache, + r, + ) + + assert.NoError(t, err) + + cachedImage, found = modifier.GetFromCache() + + assert.False(t, found) + assert.Nil(t, cachedImage) + + resizedImage, err = modifier.ResizeImage() + + assert.NoError(t, err) + assert.NotNil(t, resizedImage) + + cache.Clear() + }) + + t.Run("success from cache", func(t *testing.T) { + path := fmt.Sprintf( + "%d/%d/%s", + 200, + 200, + fileURL, + ) + + modifier, err := New(ctx, strings.Split(path, "/"), &log, &cnf, cache, r) + + assert.NoError(t, err) + + cachedImage, found := modifier.GetFromCache() + + assert.False(t, found) + assert.Nil(t, cachedImage) + + resizedImage, err := modifier.ResizeImage() + + assert.NoError(t, err) + assert.NotNil(t, resizedImage) + + modifier, err = New(ctx, strings.Split(path, "/"), &log, &cnf, cache, r) + + assert.NoError(t, err) + + cachedImage, found = modifier.GetFromCache() + + assert.True(t, found) + assert.NotNil(t, cachedImage) + }) +} + +func TestFailResizeImage(t *testing.T) { + fileURL := "raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg" //nolint + log := logger.MustSetupLogger("previewer", "Test", true, "info") + cache := lrucache.NewCache(cnf.Capability, cnf.UploadPath, log) + ctx := context.Background() + r := &http.Request{ + Method: "GET", + URL: &url.URL{ + Scheme: "http", + Host: "localhost", + Path: cnf.UploadPath, + }, + } + + t.Run("zero dimensions", func(t *testing.T) { + path := fmt.Sprintf( + "%d/%d/%s", + 0, + 0, + fileURL, + ) + + _, err := New( + ctx, + strings.Split(path, "/"), + &log, + &cnf, + cache, + r, + ) + + assert.Error(t, err) + }) + + t.Run("invalid path", func(t *testing.T) { + path := fmt.Sprintf( + "%d/%d/%s", + 100, + 100, + "test", + ) + + _, err := New( + ctx, + strings.Split(path, "/"), + &log, + &cnf, + cache, + r, + ) + + assert.Error(t, err) + }) + + t.Run("negative dimensions", func(t *testing.T) { + path := fmt.Sprintf( + "%d/%d/%s", + -100, + -100, + fileURL, + ) + + _, err := New( + ctx, + strings.Split(path, "/"), + &log, + &cnf, + cache, + r, + ) + + assert.Error(t, err) + }) +} diff --git a/internal/filesearch/filesearch.go b/internal/filesearch/filesearch.go new file mode 100644 index 0000000..0816500 --- /dev/null +++ b/internal/filesearch/filesearch.go @@ -0,0 +1,97 @@ +package filesearch + +import ( + "context" + "fmt" + "io" + "log/slog" + "net/http" + "os" + "strings" + + "github.com/rs/zerolog" +) + +func NewClient(ctx context.Context, r *http.Request) *Client { + return &Client{ + ctx: ctx, + r: r, + } +} + +type Client struct { + ctx context.Context + r *http.Request +} + +func (p *Client) newHTTPRequest(url string) *http.Request { + prxReq, _ := http.NewRequestWithContext(p.ctx, p.r.Method, url, p.r.Body) + prxQuery := prxReq.URL.Query() + + for key, values := range p.r.URL.Query() { + for _, value := range values { + prxQuery.Add(key, value) + } + } + + for key, values := range p.r.Header { + for _, value := range values { + prxReq.Header.Set(key, value) + } + } + + prxReq.URL.RawQuery = prxQuery.Encode() + + return prxReq +} + +func (p *Client) FetchFileFromURL(imageURL, outputPath string, logger *zerolog.Logger) (*http.Response, error) { + if strings.HasPrefix(imageURL, "http:/") { + imageURL = strings.Trim(strings.Replace(imageURL, "http:/", "", 1), "/") + } + + if strings.HasPrefix(imageURL, "https:/") { + imageURL = strings.Trim(strings.Replace(imageURL, "https:/", "", 1), "/") + } + + imageURL = fmt.Sprintf("https://%s", imageURL) + + req := p.newHTTPRequest(imageURL) + + slog.Debug(fmt.Sprintf("Proxy: IN='%s %s' -> OUT='%s %s'", p.r.Method, p.r.URL.String(), req.Method, req.URL.String())) //nolint + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + logger.Error().Msg("failed to close response body") + } + }(resp.Body) + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to fetch file: %s", resp.Status) + } + + outFile, err := os.Create(outputPath) + if err != nil { + return nil, err + } + + defer func(outFile *os.File) { + err := outFile.Close() + if err != nil { + logger.Error().Msg("failed to close response body") + } + }(outFile) + + _, err = io.Copy(outFile, resp.Body) + if err != nil { + return nil, err + } + + return resp, nil +} diff --git a/internal/filesearch/filesearch_test.go b/internal/filesearch/filesearch_test.go new file mode 100644 index 0000000..6e2ecee --- /dev/null +++ b/internal/filesearch/filesearch_test.go @@ -0,0 +1,71 @@ +package filesearch + +import ( + "context" + "net/http" + "net/url" + "os" + "path/filepath" + "testing" + + "github.com/DEMAxx/project_work/pkg/logger" + "github.com/stretchr/testify/require" +) + +func TestFileSearch(t *testing.T) { + outputPath := filepath.Join(os.TempDir(), "output") + logs := logger.MustSetupLogger("previewer", "Test", true, "info") + ctx := context.Background() + + client := NewClient( + ctx, + &http.Request{ + Method: "GET", + URL: &url.URL{ + Scheme: "http", + Host: "localhost", + Path: outputPath, + }, + }, + ) + + t.Run("success", func(t *testing.T) { + r, err := client.FetchFileFromURL( + "https://raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg", //nolint + outputPath, + &logs, + ) + + require.NoError(t, err) + require.NotNil(t, r) + require.True(t, r.StatusCode == http.StatusOK) + err = r.Body.Close() + require.NoError(t, err) + }) + + t.Run("wrong address", func(t *testing.T) { + r, err := client.FetchFileFromURL( + "https://raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/not_gopher_original.jpg", + outputPath, + &logs, + ) + + require.Error(t, err) + + if err == nil { + err = r.Body.Close() + require.NoError(t, err) + } + }) + + t.Run("not found", func(t *testing.T) { + r, err := client.FetchFileFromURL("localhost:9999/image.png", outputPath, &logs) + require.Error(t, err) + require.ErrorContains(t, err, "connection refused") + + if err == nil { + err = r.Body.Close() + require.NoError(t, err) + } + }) +} diff --git a/internal/lrucache/cache.go b/internal/lrucache/cache.go new file mode 100644 index 0000000..83f7343 --- /dev/null +++ b/internal/lrucache/cache.go @@ -0,0 +1,121 @@ +package lrucache + +import ( + "fmt" + "os" + "sync" + + "github.com/rs/zerolog" +) + +type Key string + +type Cache interface { + Set(key Key, value interface{}) bool + Get(key Key) (interface{}, bool) + Clear() +} + +type lruCache struct { + capacity int + upload string + logger zerolog.Logger + queue List + items map[Key]*cacheItem +} + +type cacheItem struct { + key Key + value interface{} + item *ListItem +} + +var mutex sync.Mutex + +func (lruCache *lruCache) Set(key Key, value interface{}) bool { + mutex.Lock() + defer mutex.Unlock() + + item, ok := lruCache.items[key] + + if ok { + item.value = value + lruCache.queue.MoveToFront(item.item) + + return true + } + + if lruCache.capacity == lruCache.queue.Len() { + back := lruCache.queue.Back() + valKey, ok := back.Value.(Key) + + if !ok { + return false + } + + delete(lruCache.items, valKey) + lruCache.queue.Remove(back) + + err := os.Remove(fmt.Sprintf("%s/%s", lruCache.upload, valKey)) + if err != nil { + lruCache.logger.Error().Err(err) + return false + } + } + newItem := lruCache.queue.PushFront(key) + + lruCache.items[key] = &cacheItem{ + key: key, + value: value, + item: newItem, + } + + return false +} + +func (lruCache *lruCache) Get(key Key) (interface{}, bool) { + mutex.Lock() + defer mutex.Unlock() + + item, ok := lruCache.items[key] + + if !ok { + return nil, false + } + + lruCache.queue.MoveToFront(item.item) + return item.value, true +} + +func (lruCache *lruCache) Clear() { + mutex.Lock() + defer mutex.Unlock() + + lruCache.queue = new(list) + lruCache.items = make(map[Key]*cacheItem, lruCache.capacity) + + err := os.RemoveAll(fmt.Sprintf("%s/*", lruCache.upload)) + if err != nil { + lruCache.logger.Error().Err(err) + } +} + +func NewCache(capacity int, upload string, logger zerolog.Logger) Cache { + if _, err := os.Stat(upload); err != nil { + if os.IsNotExist(err) { + if err = os.Mkdir(upload, os.ModePerm); err != nil { + logger.Error().Err(err).Msg("failed to create directory") + } + } else { + logger.Error().Err(err).Msg("failed with directory") + } + } + + return &lruCache{ + capacity: capacity, + upload: upload, + logger: logger, + queue: new(list), + items: make(map[Key]*cacheItem, capacity), + } +} diff --git a/internal/lrucache/cache_test.go b/internal/lrucache/cache_test.go new file mode 100644 index 0000000..95c4e1b --- /dev/null +++ b/internal/lrucache/cache_test.go @@ -0,0 +1,97 @@ +package lrucache + +import ( + "math/rand" + "strconv" + "sync" + "testing" + + "github.com/rs/zerolog" + "github.com/stretchr/testify/require" +) + +func TestCache(t *testing.T) { + upload := "/tmp" + logger := zerolog.Logger{} + + t.Run("empty cache", func(t *testing.T) { + c := NewCache(10, upload, logger) + + _, ok := c.Get("aaa") + require.False(t, ok) + + _, ok = c.Get("bbb") + require.False(t, ok) + }) + + t.Run("simple", func(t *testing.T) { + c := NewCache(5, upload, logger) + + wasInCache := c.Set("aaa", 100) + require.False(t, wasInCache) + + wasInCache = c.Set("bbb", 200) + require.False(t, wasInCache) + + val, ok := c.Get("aaa") + require.True(t, ok) + require.Equal(t, 100, val) + + val, ok = c.Get("bbb") + require.True(t, ok) + require.Equal(t, 200, val) + + wasInCache = c.Set("aaa", 300) + require.True(t, wasInCache) + + val, ok = c.Get("aaa") + require.True(t, ok) + require.Equal(t, 300, val) + + val, ok = c.Get("ccc") + require.False(t, ok) + require.Nil(t, val) + }) + + t.Run("purge logic", func(t *testing.T) { + c := NewCache(1, upload, logger) + + for _, v := range [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9} { + c.Set(Key(strconv.Itoa(v)), v) + } + + x, _ := c.Get(Key(strconv.Itoa(9))) + + require.Equal(t, 9, x) + + x, y := c.Get(Key(strconv.Itoa(1))) + + require.Nil(t, x) + require.False(t, y) + }) +} + +func TestCacheMultithreading(_ *testing.T) { + upload := "/tmp" + logger := zerolog.Logger{} + + c := NewCache(10, upload, logger) + wg := &sync.WaitGroup{} + wg.Add(2) + + go func() { + defer wg.Done() + for i := 0; i < 1_000_000; i++ { + c.Set(Key(strconv.Itoa(i)), i) + } + }() + + go func() { + defer wg.Done() + for i := 0; i < 1_000_000; i++ { + c.Get(Key(strconv.Itoa(rand.Intn(1_000_000)))) + } + }() + + wg.Wait() +} diff --git a/internal/lrucache/list.go b/internal/lrucache/list.go new file mode 100644 index 0000000..a7dcb6d --- /dev/null +++ b/internal/lrucache/list.go @@ -0,0 +1,118 @@ +package lrucache + +type List interface { + Len() int + Front() *ListItem + Back() *ListItem + PushFront(v interface{}) *ListItem + PushBack(v interface{}) *ListItem + Remove(i *ListItem) + MoveToFront(i *ListItem) +} + +type ListItem struct { + Value interface{} + Next *ListItem + Prev *ListItem +} + +type list struct { + len int + front *ListItem + back *ListItem +} + +func (l list) Len() int { + return l.len +} + +func (l list) Front() *ListItem { + return l.front +} + +func (l list) Back() *ListItem { + return l.back +} + +func (l *list) PushFront(v interface{}) *ListItem { + item := new(ListItem) + item.Value = v + item.Next = l.front + + if l.len == 0 { + l.back = item + } else { + l.front.Prev = item + } + + l.front = item + l.len++ + + return item +} + +func (l *list) PushBack(v interface{}) *ListItem { + item := new(ListItem) + item.Value = v + + if l.len == 0 { + l.front = item + } else { + l.back.Next = item + } + + item.Prev = l.back + l.back = item + l.len++ + + return item +} + +func (l *list) Remove(item *ListItem) { + if item == nil { + panic("ListItem is nil") + } + + if item.Prev != nil { + item.Prev.Next = item.Next + } else { + l.front = item.Next + } + + if item.Next != nil { + item.Next.Prev = item.Prev + } else { + l.back = item.Prev + } + + item.Next = nil + item.Prev = nil + l.len-- +} + +func (l *list) MoveToFront(item *ListItem) { + exNext := item.Next + exPrev := item.Prev + + if exPrev == nil { + return + } + + if exNext == nil { + exPrev.Next = nil + l.back = exPrev + } else { + exPrev.Next = item.Next + exNext.Prev = item.Prev + } + + exFront := l.Front() + exFront.Prev = item + item.Next = exFront + item.Prev = nil + l.front = item +} + +func NewList() List { + return new(list) +} diff --git a/internal/lrucache/list_test.go b/internal/lrucache/list_test.go new file mode 100644 index 0000000..44b7567 --- /dev/null +++ b/internal/lrucache/list_test.go @@ -0,0 +1,65 @@ +package lrucache + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestList(t *testing.T) { + t.Run("empty list", func(t *testing.T) { + l := NewList() + + require.Equal(t, 0, l.Len()) + require.Nil(t, l.Front()) + require.Nil(t, l.Back()) + }) + + t.Run("complex", func(t *testing.T) { + l := NewList() + + l.PushFront(10) // [10] + l.PushBack(20) // [10, 20] + l.PushBack(30) // [10, 20, 30] + require.Equal(t, 3, l.Len()) + + middle := l.Front().Next // 20 + l.Remove(middle) // [10, 30] + require.Equal(t, 2, l.Len()) + + for i, v := range [...]int{40, 50, 60, 70, 80} { + if i%2 == 0 { + l.PushFront(v) + } else { + l.PushBack(v) + } + } // [80, 60, 40, 10, 30, 50, 70] + + require.Equal(t, 7, l.Len()) + require.Equal(t, 80, l.Front().Value) + require.Equal(t, 70, l.Back().Value) + + l.MoveToFront(l.Front()) // [80, 60, 40, 10, 30, 50, 70] + l.MoveToFront(l.Back()) // [70, 80, 60, 40, 10, 30, 50] + + elems := make([]int, 0, l.Len()) + for i := l.Front(); i != nil; i = i.Next { + elems = append(elems, i.Value.(int)) + } + require.Equal(t, []int{70, 80, 60, 40, 10, 30, 50}, elems) + }) +} + +func TestListMove(t *testing.T) { + t.Run("move", func(t *testing.T) { + l := NewList() + + for _, v := range [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9} { + l.PushBack(v) + l.MoveToFront(l.Back()) + } + + require.Equal(t, 9, l.Front().Value) + require.Equal(t, 1, l.Back().Value) + }) +} diff --git a/internal/server/http/middleware.go b/internal/server/http/middleware.go new file mode 100644 index 0000000..a972c41 --- /dev/null +++ b/internal/server/http/middleware.go @@ -0,0 +1,29 @@ +package internalhttp + +import ( + "fmt" + "net/http" + "time" + + "github.com/rs/zerolog" +) + +func LoggingMiddleware(next http.Handler, logg *zerolog.Logger) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + clientIP := r.RemoteAddr + dateTime := time.Now().Format(time.RFC3339) + method := r.Method + path := r.URL.Path + httpVersion := r.Proto + userAgent := r.Header.Get("User-Agent") + + logg.Info().Msg( + fmt.Sprintf( + "Client IP: %s, DateTime: %s, Method: %s, Path: %s, HTTP Version: %s, User Agent: %s", + clientIP, dateTime, method, path, httpVersion, userAgent, + ), + ) + + next.ServeHTTP(w, r) + }) +} diff --git a/internal/server/http/server.go b/internal/server/http/server.go new file mode 100644 index 0000000..4dad324 --- /dev/null +++ b/internal/server/http/server.go @@ -0,0 +1,149 @@ +package internalhttp + +import ( + "context" + "errors" + "fmt" + "net/http" + "strings" + "time" + + "github.com/DEMAxx/project_work/internal/filemodifier" + "github.com/DEMAxx/project_work/internal/lrucache" + "github.com/DEMAxx/project_work/pkg/config" + "github.com/rs/zerolog" +) + +const TIMEOUT = 5 * time.Second + +type Server struct { + ctx context.Context + httpServer *http.Server + logger *zerolog.Logger + cache lrucache.Cache +} + +func NewServer( + ctx context.Context, + logger *zerolog.Logger, + hostAndPort string, + cache lrucache.Cache, + cnf *config.Config, +) *Server { + mux := http.NewServeMux() + + mux.Handle("/hello", LoggingMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + clientIP := r.RemoteAddr + dateTime := time.Now().Format(time.RFC3339) + method := r.Method + path := r.URL.Path + httpVersion := r.Proto + userAgent := r.Header.Get("User-Agent") + + logger.Info().Msg( + fmt.Sprintf( + "Client IP: %s, DateTime: %s, Method: %s, Path: %s, HTTP Version: %s, User Agent: %s", + clientIP, dateTime, method, path, httpVersion, userAgent, + ), + ) + + write, err := w.Write([]byte("Hello, World!")) + if err != nil { + return + } + logger.Info().Msg(fmt.Sprintf("response: %d", write)) + }), logger)) + + mux.Handle("/fill/", LoggingMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path[len("/fill/"):] + + if path == "" { + http.Error(w, "Missing URL parameter", http.StatusBadRequest) + return + } + + modifier, err := filemodifier.New( + ctx, + strings.Split(path, "/"), + logger, + cnf, + cache, + r, + ) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + cachedImage, found := modifier.GetFromCache() + + if found { + logger.Info().Msg("Image retrieved from cache") + + w.Header().Set("Content-Type", "image/jpeg") + w.WriteHeader(http.StatusOK) + _, err := w.Write(cachedImage.([]byte)) + if err != nil { + logger.Error().Msg("Failed to write cached image to response") + } + return + } + + resizedImage, err := modifier.ResizeImage() + if err != nil { + http.Error(w, fmt.Sprintf("Failed to modify image: %s", err), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "image/jpeg") + w.WriteHeader(http.StatusOK) + _, err = w.Write(resizedImage) + if err != nil { + logger.Error().Msg("Failed to write response body") + return + } + }), logger)) + + return &Server{ + ctx: ctx, + httpServer: &http.Server{ + Addr: hostAndPort, + Handler: mux, + ReadHeaderTimeout: TIMEOUT, + }, + logger: logger, + cache: cache, + } +} + +func (s *Server) Start() error { + s.logger.Info().Msg(fmt.Sprintf("Starting HTTP server on %s...", s.httpServer.Addr)) + + // Start HTTP server + go func() { + s.logger.Info().Msg("HTTP server start...") + + if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + s.logger.Error().Msg(fmt.Sprintf("HTTP server ListenAndServe: %s", err.Error())) + } + }() + + <-s.ctx.Done() + return s.Stop() +} + +func (s *Server) Stop() error { + s.logger.Info().Msg("Stopping HTTP server...") + + // Stop HTTP server + shutdownCtx, cancel := context.WithTimeout(s.ctx, 5*time.Second) + defer cancel() + + if err := s.httpServer.Shutdown(shutdownCtx); err != nil { + s.logger.Error().Msg(fmt.Sprintf("HTTP server Shutdown: %s", err.Error())) + return err + } + + s.logger.Info().Msg("HTTP server stopped") + return nil +} diff --git a/internal/server/http/server_test.go b/internal/server/http/server_test.go new file mode 100644 index 0000000..51c9797 --- /dev/null +++ b/internal/server/http/server_test.go @@ -0,0 +1,69 @@ +package internalhttp + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/DEMAxx/project_work/internal/lrucache" + "github.com/DEMAxx/project_work/pkg/config" + "github.com/DEMAxx/project_work/pkg/logger" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const testImagesDir = "testdata" + +func TestServer(t *testing.T) { + ctx := context.Background() + logs := logger.MustSetupLogger(config.AppName, "test", true, "INFO") + cnf := config.Config{ + Capability: 10, + UploadPath: testImagesDir, + } + + cache := lrucache.NewCache(cnf.Capability, cnf.UploadPath, logs) + server := NewServer(ctx, &logs, "localhost:8080", cache, &cnf) + + t.Run("hello", func(t *testing.T) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/hello", nil) + require.NoError(t, err) + + rec := httptest.NewRecorder() + + server.httpServer.Handler.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "Hello, World!", rec.Body.String()) + }) + + t.Run("fill success", func(t *testing.T) { + err := os.MkdirAll(testImagesDir, 0o755) + + require.NoError(t, err) + + fileURL := "raw.githubusercontent.com/OtusGolang/final_project/master/examples/image-previewer/_gopher_original_1024x504.jpg" //nolint + path := fmt.Sprintf( + "/fill/%d/%d/%s", + 100, + 100, + fileURL, + ) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil) + require.NoError(t, err) + + rec := httptest.NewRecorder() + + server.httpServer.Handler.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "image/jpeg", rec.Header().Get("Content-Type")) + + err = os.RemoveAll(testImagesDir) + require.NoError(t, err) + }) +} diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..5c981af --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,37 @@ +package config + +import ( + "log" + + "github.com/ilyakaznacheev/cleanenv" +) + +const AppName = "previewer" + +type Config struct { + Timeout struct { + Read byte `toml:"TIMEOUT_READ" env:"TIMEOUT_READ" env-default:"5"` + Write byte `toml:"TIMEOUT_WRITE" env:"TIMEOUT_WRITE" env-default:"5"` + Shutdown byte `toml:"TIMEOUT_SHUTDOWN" env:"TIMEOUT_SHUTDOWN" env-default:"3"` + } + Server struct { + Host string `toml:"SERVER_HOST" env:"SERVER_HOST" env-default:"localhost"` + Port string `toml:"port" env:"SERVER_PORT" env-default:"8000"` + } + Capability int `toml:"CAPABILITY" env:"CAPABILITY" env-default:"10"` + Debug bool `toml:"APP_DEBUG" env:"APP_DEBUG" env-default:"true"` + Env string `toml:"APP_ENV" env:"APP_ENV" env-default:"local"` + Local bool `toml:"LOCAL" env:"LOCAL"` + LogLevel string `toml:"LOG_LEVEL" env:"LOG_LEVEL" env-default:"info"` + UploadPath string `toml:"UPLOAD_PATH" env:"UPLOAD_PATH" env-default:"/tmp"` +} + +func MustLoad(configFile string) *Config { + cfg := Config{} + + if err := cleanenv.ReadConfig(configFile, &cfg); err != nil { + log.Fatalf("Error loading config: %v", err) + } + + return &cfg +} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go new file mode 100644 index 0000000..b1a89f5 --- /dev/null +++ b/pkg/logger/logger.go @@ -0,0 +1,34 @@ +package logger + +import ( + "log" + "os" + "time" + + "github.com/rs/zerolog" + zlog "github.com/rs/zerolog/log" +) + +func MustSetupLogger(app, stage string, debug bool, level string) zerolog.Logger { + zerolog.MessageFieldName = "rest" + zerolog.LevelFieldName = "severity" + zerolog.TimestampFieldName = "timestamp" + zerolog.TimeFieldFormat = time.RFC3339Nano + + var logs zerolog.Logger + + if debug { + logs = zlog.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + } else { + logs = zlog.Output(os.Stderr) + } + + parsedLvl, err := zerolog.ParseLevel(level) + if err != nil { + log.Fatalf("Error loading config: %v", err) + } + + zlog.Logger = logs.Level(parsedLvl).With().Str("service", app).Str("stage", stage).Logger() + + return zlog.Logger +} diff --git a/tests/Dockerfile b/tests/Dockerfile new file mode 100755 index 0000000..7b02ceb --- /dev/null +++ b/tests/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.24 + +RUN mkdir -p /opt/integration_tests +WORKDIR /opt/integration_tests + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY tests . + +CMD ["go", "test"] diff --git a/tests/features/notification.feature b/tests/features/notification.feature new file mode 100755 index 0000000..5f11a81 --- /dev/null +++ b/tests/features/notification.feature @@ -0,0 +1,13 @@ +# file: features/notification.feature + +# http://localhost:8088/ +# http://reg_service:8088/ + +Feature: Resize image + As API to resize images + + Scenario: Registration service is available + When I am working + + Scenario: Notification event is received + When I am working diff --git a/tests/go.mod b/tests/go.mod new file mode 100644 index 0000000..8fa3626 --- /dev/null +++ b/tests/go.mod @@ -0,0 +1,18 @@ +module godog_example/integration_tests + +go 1.24.0 + +require ( + github.com/cucumber/godog v0.15.0 + github.com/cucumber/messages-go/v16 v16.0.1 +) + +require ( + github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect + github.com/cucumber/messages/go/v21 v21.0.1 // indirect + github.com/gofrs/uuid v4.3.1+incompatible // indirect + github.com/hashicorp/go-immutable-radix v1.3.1 // indirect + github.com/hashicorp/go-memdb v1.3.4 // indirect + github.com/hashicorp/golang-lru v0.5.4 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/tests/go.sum b/tests/go.sum new file mode 100644 index 0000000..50bc1eb --- /dev/null +++ b/tests/go.sum @@ -0,0 +1,326 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= +cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= +cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= +cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= +cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= +cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= +cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= +cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= +dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw= +github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI= +github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0= +github.com/cucumber/godog v0.12.0 h1:xVOc9ML+1joT0CqcdQTpfXiT7G1hOLbCmlUnYOyJ80w= +github.com/cucumber/godog v0.12.0/go.mod h1:u6SD7IXC49dLpPN35kal0oYEjsXZWee4pW6Tm9t5pIc= +github.com/cucumber/godog v0.15.0 h1:51AL8lBXF3f0cyA5CV4TnJFCTHpgiy+1x1Hb3TtZUmo= +github.com/cucumber/godog v0.15.0/go.mod h1:FX3rzIDybWABU4kuIXLZ/qtqEe1Ac5RdXmqvACJOces= +github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages-go/v16 v16.0.1 h1:fvkpwsLgnIm0qugftrw2YwNlio+ABe2Iu94Ap8GMYIY= +github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages/go/v21 v21.0.1 h1:wzA0LxwjlWQYZd32VTlAVDTkW6inOFmSM+RuOwHZiMI= +github.com/cucumber/messages/go/v21 v21.0.1/go.mod h1:zheH/2HS9JLVFukdrsPWoPdmUtmYQAQPLk7w5vWsk5s= +github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay93k7Rn3Dee7iyPJjs= +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= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI= +github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= +github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= +github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-memdb v1.3.0/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= +github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c= +github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= +github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= +github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= +golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= +google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/tests/main_test.go b/tests/main_test.go new file mode 100755 index 0000000..2139f12 --- /dev/null +++ b/tests/main_test.go @@ -0,0 +1,32 @@ +package scripts + +import ( + "log" + "os" + "testing" + "time" + + "github.com/cucumber/godog" +) + +const delay = 5 * time.Second + +func TestMain(m *testing.M) { + log.Printf("wait %s for service availability...", delay) + time.Sleep(delay) + + status := godog.TestSuite{ + Name: "integration", + ScenarioInitializer: InitializeScenario, + Options: &godog.Options{ + Format: "progress", // Замените на "pretty" для лучшего вывода + Paths: []string{"features"}, + Randomize: 0, // Последовательный порядок исполнения + }, + }.Run() + + if st := m.Run(); st > status { + status = st + } + os.Exit(status) +} diff --git a/tests/resize_test.go b/tests/resize_test.go new file mode 100755 index 0000000..b54fd9d --- /dev/null +++ b/tests/resize_test.go @@ -0,0 +1,55 @@ +package scripts + +import ( + "fmt" + "os" + "sync" + "time" + + "github.com/cucumber/godog" + //"github.com/cucumber/messages-go/v16" +) + +var amqpDSN = os.Getenv("TESTS_AMQP_DSN") + +func init() { + if amqpDSN == "" { + amqpDSN = "amqp://guest:guest@localhost:5672/" + } +} + +type notifyTest struct { + messages [][]byte + messagesMutex *sync.RWMutex + stopSignal chan struct{} + + responseStatusCode int + responseBody []byte +} + +func panicOnErr(err error) { + if err != nil { + panic(err) + } +} + +func (test *notifyTest) iReceiveEventWithText(text string) error { + time.Sleep(3 * time.Second) // На всякий случай ждём обработки евента + + test.messagesMutex.RLock() + defer test.messagesMutex.RUnlock() + + for _, msg := range test.messages { + if string(msg) == text { + return nil + } + } + return fmt.Errorf("event with text '%s' was not found in %s", text, test.messages) +} + +func InitializeScenario(s *godog.ScenarioContext) { + test := new(notifyTest) + + s.Step(`^I am working "([^"]*)"$`, test.iReceiveEventWithText) + +}