diff --git a/Makefile b/Makefile index 6c00e69..a8172cd 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ build: - go build -o bin ./previewer/main.go + go build -o bin ./cmd/main.go test: - go test -race ./previewer/... + go test -race ./cmd/... ./internal/... lint: install-lint-deps - golangci-lint run ./previewer/... + golangci-lint run ./previewer/... ./internal/... install-lint-deps: rm -rf $(shell go env GOPATH)/bin/golangci-lint diff --git a/examples/gopher_1024x252.jpg b/assets/gopher_1024x252.jpg similarity index 100% rename from examples/gopher_1024x252.jpg rename to assets/gopher_1024x252.jpg diff --git a/examples/gopher_2000x1000.jpg b/assets/gopher_2000x1000.jpg similarity index 100% rename from examples/gopher_2000x1000.jpg rename to assets/gopher_2000x1000.jpg diff --git a/examples/gopher_200x700.jpg b/assets/gopher_200x700.jpg similarity index 100% rename from examples/gopher_200x700.jpg rename to assets/gopher_200x700.jpg diff --git a/examples/gopher_256x126.jpg b/assets/gopher_256x126.jpg similarity index 100% rename from examples/gopher_256x126.jpg rename to assets/gopher_256x126.jpg diff --git a/examples/gopher_333x666.jpg b/assets/gopher_333x666.jpg similarity index 100% rename from examples/gopher_333x666.jpg rename to assets/gopher_333x666.jpg diff --git a/examples/gopher_500x500.jpg b/assets/gopher_500x500.jpg similarity index 100% rename from examples/gopher_500x500.jpg rename to assets/gopher_500x500.jpg diff --git a/examples/gopher_50x50.jpg b/assets/gopher_50x50.jpg similarity index 100% rename from examples/gopher_50x50.jpg rename to assets/gopher_50x50.jpg diff --git a/assets/gopher_original_1024x504.jpg b/assets/gopher_original_1024x504.jpg new file mode 100644 index 0000000..efc1a05 Binary files /dev/null and b/assets/gopher_original_1024x504.jpg differ diff --git a/previewer/main.go b/cmd/main.go similarity index 85% rename from previewer/main.go rename to cmd/main.go index a4b5a22..07ae475 100644 --- a/previewer/main.go +++ b/cmd/main.go @@ -6,8 +6,8 @@ import ( "os" "os/signal" - "github.com/tiburon-777/OTUS_Project/previewer/application" - "github.com/tiburon-777/OTUS_Project/previewer/config" + "github.com/tiburon-777/OTUS_Project/internal/application" + "github.com/tiburon-777/OTUS_Project/internal/config" ) var ConfigFile = flag.String("config", "/etc/previewer.conf", "Path to configuration file") diff --git a/previewer/main_test.go b/cmd/main_test.go similarity index 76% rename from previewer/main_test.go rename to cmd/main_test.go index b67553d..d86de8e 100644 --- a/previewer/main_test.go +++ b/cmd/main_test.go @@ -6,15 +6,24 @@ import ( "io/ioutil" "log" "net/http" + "strconv" "sync" "testing" "time" ) +const testPortBase = 3000 + func TestIntegrationPositive(t *testing.T) { + testPort := strconv.Itoa(testPortBase + 1) wg := sync.WaitGroup{} - server := &http.Server{Addr: ":3000", Handler: http.FileServer(http.Dir("../examples"))} - go server.ListenAndServe() + server := &http.Server{Addr: "localhost:" + testPort, Handler: http.FileServer(http.Dir("../assets"))} + go func() { + err := server.ListenAndServe() + if err != nil { + log.Println(err.Error()) + } + }() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) go func(ctx context.Context) { main() @@ -24,10 +33,10 @@ func TestIntegrationPositive(t *testing.T) { wg.Add(2) t.Run("remote server return jpeg", func(t *testing.T) { defer wg.Done() - body, _, err := request("http://localhost:8080/fill/1024/504/localhost:3000/gopher_original_1024x504.jpg", 15*time.Second) + body, resp, err := request("http://localhost:8080/fill/1024/504/localhost:"+testPort+"/gopher_original_1024x504.jpg", 15*time.Second) require.NoError(t, err) require.NotNil(t, body) - //require.Equal(t, 200, resp.StatusCode) + require.Equal(t, 200, resp.StatusCode) }) t.Run("found pic in cache", func(t *testing.T) { defer wg.Done() @@ -42,10 +51,15 @@ func TestIntegrationPositive(t *testing.T) { } func TestIntegrationNegative(t *testing.T) { - // Развернуть веб сервис со статическими картинками + testPort := strconv.Itoa(testPortBase + 2) wg := sync.WaitGroup{} - server := &http.Server{Addr: ":3000", Handler: http.FileServer(http.Dir("../examples"))} - go server.ListenAndServe() + server := &http.Server{Addr: "localhost:" + testPort, Handler: http.FileServer(http.Dir("../assets"))} + go func() { + err := server.ListenAndServe() + if err != nil { + log.Println(err.Error()) + } + }() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) // Запустить наше приложение go func(ctx context.Context) { diff --git a/previewer/application/application.go b/internal/application/application.go similarity index 82% rename from previewer/application/application.go rename to internal/application/application.go index d58ce24..23e8856 100644 --- a/previewer/application/application.go +++ b/internal/application/application.go @@ -5,9 +5,9 @@ import ( "net" "net/http" - "github.com/tiburon-777/OTUS_Project/previewer/cache" - "github.com/tiburon-777/OTUS_Project/previewer/config" - "github.com/tiburon-777/OTUS_Project/previewer/logger" + "github.com/tiburon-777/OTUS_Project/internal/cache" + "github.com/tiburon-777/OTUS_Project/internal/config" + "github.com/tiburon-777/OTUS_Project/internal/logger" ) type App struct { diff --git a/previewer/application/handlers.go b/internal/application/handlers.go similarity index 87% rename from previewer/application/handlers.go rename to internal/application/handlers.go index 069e7bd..a1aecb7 100644 --- a/previewer/application/handlers.go +++ b/internal/application/handlers.go @@ -5,10 +5,10 @@ import ( "net/http" "time" - "github.com/tiburon-777/OTUS_Project/previewer/cache" - "github.com/tiburon-777/OTUS_Project/previewer/config" - "github.com/tiburon-777/OTUS_Project/previewer/converter" - "github.com/tiburon-777/OTUS_Project/previewer/logger" + "github.com/tiburon-777/OTUS_Project/internal/cache" + "github.com/tiburon-777/OTUS_Project/internal/config" + "github.com/tiburon-777/OTUS_Project/internal/converter" + "github.com/tiburon-777/OTUS_Project/internal/logger" ) func handler(c cache.Cache, conf config.Config) http.Handler { diff --git a/previewer/application/query.go b/internal/application/query.go similarity index 97% rename from previewer/application/query.go rename to internal/application/query.go index d9c319f..1c46935 100644 --- a/previewer/application/query.go +++ b/internal/application/query.go @@ -46,7 +46,7 @@ func (q Query) fromOrigin(timeout time.Duration) ([]byte, *http.Response, error) client := &http.Client{} ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - req, err := http.NewRequestWithContext(ctx, "GET", "http://"+q.URL.Host+"/"+q.URL.Path, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "http://"+q.URL.Host+q.URL.Path, nil) if err != nil { return nil, nil, err } diff --git a/previewer/application/query_test.go b/internal/application/query_test.go similarity index 100% rename from previewer/application/query_test.go rename to internal/application/query_test.go diff --git a/previewer/cache/cache.go b/internal/cache/cache.go similarity index 100% rename from previewer/cache/cache.go rename to internal/cache/cache.go diff --git a/previewer/cache/cache_test.go b/internal/cache/cache_test.go similarity index 100% rename from previewer/cache/cache_test.go rename to internal/cache/cache_test.go diff --git a/previewer/cache/list.go b/internal/cache/list.go similarity index 100% rename from previewer/cache/list.go rename to internal/cache/list.go diff --git a/previewer/cache/list_test.go b/internal/cache/list_test.go similarity index 100% rename from previewer/cache/list_test.go rename to internal/cache/list_test.go diff --git a/previewer/config/config.go b/internal/config/config.go similarity index 100% rename from previewer/config/config.go rename to internal/config/config.go diff --git a/previewer/config/config_test.go b/internal/config/config_test.go similarity index 100% rename from previewer/config/config_test.go rename to internal/config/config_test.go diff --git a/previewer/converter/converter.go b/internal/converter/converter.go similarity index 100% rename from previewer/converter/converter.go rename to internal/converter/converter.go diff --git a/previewer/converter/converter_test.go b/internal/converter/converter_test.go similarity index 100% rename from previewer/converter/converter_test.go rename to internal/converter/converter_test.go diff --git a/previewer/logger/logger.go b/internal/logger/logger.go similarity index 100% rename from previewer/logger/logger.go rename to internal/logger/logger.go diff --git a/previewer/logger/logger_test.go b/internal/logger/logger_test.go similarity index 100% rename from previewer/logger/logger_test.go rename to internal/logger/logger_test.go