Skip to content

Commit 4a93aa7

Browse files
committed
Replace local HTTP helpers with shared package
Use github.com/iqhater/pkg for middleware and headers. Drop unused local cache, limiter, logger, and middleware packages, and update the module to Go 1.26.
1 parent ec624a0 commit 4a93aa7

23 files changed

Lines changed: 29 additions & 1198 deletions

File tree

cmd/api-docs/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/gooddog-rent/hotels_api/pkg/logger"
10-
mid "github.com/gooddog-rent/hotels_api/pkg/middleware"
9+
mid "github.com/iqhater/pkg/middleware"
1110
)
1211

1312
// OpenAPI docs
@@ -19,7 +18,7 @@ func main() {
1918
mux.Handle("/api/", http.StripPrefix("/api/", http.FileServer(http.Dir("./api"))))
2019

2120
// redirect to from / to /api/app/redoc.html
22-
mux.HandleFunc("/", mid.Bind(logger.Log, func(w http.ResponseWriter, r *http.Request) {
21+
mux.HandleFunc("/", mid.Bind(mid.Log, func(w http.ResponseWriter, r *http.Request) {
2322
http.Redirect(w, r, "/api/app/redoc.html", http.StatusMovedPermanently)
2423
}))
2524

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
module github.com/gooddog-rent/hotels_api
22

3-
go 1.24
3+
go 1.26
44

55
require (
66
github.com/Masterminds/squirrel v1.5.4
7+
github.com/iqhater/pkg v0.0.0-20260619181521-8b4449dab11c
78
github.com/joho/godotenv v1.5.1
89
github.com/mattn/go-sqlite3 v1.14.24
910
github.com/pressly/goose/v3 v3.24.1
10-
github.com/sh7dm/brotlihandler v0.0.1
1111
github.com/stretchr/testify v1.10.0
1212
go.uber.org/goleak v1.3.0
13-
golang.org/x/time v0.7.0
1413
)
1514

1615
require (
17-
github.com/andybalholm/brotli v1.1.1 // indirect
16+
github.com/andybalholm/brotli v1.2.1 // indirect
1817
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
1919
github.com/kr/text v0.2.0 // indirect
2020
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
2121
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
@@ -25,5 +25,6 @@ require (
2525
github.com/stretchr/objx v0.5.2 // indirect
2626
go.uber.org/multierr v1.11.0 // indirect
2727
golang.org/x/sync v0.10.0 // indirect
28+
golang.org/x/time v0.15.0 // indirect
2829
gopkg.in/yaml.v3 v3.0.1 // indirect
2930
)

go.sum

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
22
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
3-
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
4-
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
5-
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
3+
github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro=
4+
github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
65
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
76
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
87
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -12,6 +11,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1211
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1312
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
1413
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
14+
github.com/iqhater/pkg v0.0.0-20260619181521-8b4449dab11c h1:qI5ESjxlJ6M3DwMdE6m0JpxNPY0rr9mzh1X0GaeVHYg=
15+
github.com/iqhater/pkg v0.0.0-20260619181521-8b4449dab11c/go.mod h1:DOkyDJn+R4Eqo27TiDDAJeHiwOjvpJFwPLxk5STvvIw=
1516
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
1617
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1718
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -38,8 +39,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
3839
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
3940
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
4041
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
41-
github.com/sh7dm/brotlihandler v0.0.1 h1:Z03e/T3zvAE5xdYrcnGKqtcH8Qv+NWKcPZqmyGQyZBg=
42-
github.com/sh7dm/brotlihandler v0.0.1/go.mod h1:SlI4IqBvHLUChGcMTRS89G2SBGXifdXuL80EsZvPdnA=
4342
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
4443
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
4544
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -55,8 +54,8 @@ golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
5554
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
5655
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
5756
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
58-
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
59-
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
57+
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
58+
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
6059
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6160
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
6261
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/controller/http/v1/hotel.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ func (h *hotelRoutes) GetHotels(w http.ResponseWriter, req *http.Request) {
4444
return
4545
}
4646

47-
// db read timeout
48-
/* contextDBTimeout := 20 * time.Second
49-
ctxTimeout, cancel := context.WithTimeoutCause(req.Context(), contextDBTimeout, errors.New("Read from DB timeout!"))
50-
defer cancel() */
51-
5247
filtered, err := h.hotelService.SearchHotels(req.Context(), text, limit)
5348
if err != nil {
5449
http.Error(w, err.Error(), http.StatusNotFound)

internal/run.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,21 @@ import (
1212
"time"
1313

1414
"github.com/gooddog-rent/hotels_api/internal/service"
15-
"github.com/gooddog-rent/hotels_api/pkg/cache"
16-
"github.com/gooddog-rent/hotels_api/pkg/headers/content"
17-
"github.com/gooddog-rent/hotels_api/pkg/headers/cors"
18-
"github.com/gooddog-rent/hotels_api/pkg/headers/secure"
19-
"github.com/gooddog-rent/hotels_api/pkg/limiter"
20-
"github.com/gooddog-rent/hotels_api/pkg/logger"
2115

2216
"log"
2317
"net/http"
2418
"os/signal"
2519
"syscall"
2620

27-
mid "github.com/gooddog-rent/hotels_api/pkg/middleware"
28-
29-
"github.com/sh7dm/brotlihandler"
21+
"github.com/iqhater/pkg/headers"
22+
mid "github.com/iqhater/pkg/middleware"
3023
)
3124

3225
func Run() {
3326

3427
// init config environments
3528
cfg := NewConfig()
3629

37-
// init cache storage
38-
c := cache.NewCache("2m")
39-
cache.CacheStore = c
40-
4130
// init repositories
4231
repositories, err := infra.NewRepositories(cfg.HOTELS_PATH)
4332
if err != nil {
@@ -61,19 +50,25 @@ func Run() {
6150

6251
// init middlewares per one route
6352
midMain := mid.Middlewares(
64-
c.CacheHeaders,
65-
content.ContentTypeHeaders,
66-
cors.CORSHeaders,
67-
secure.SecureHeaders,
6853
ctrl.ValidateRequest,
69-
// c.CacheResponse,
54+
mid.Compress,
55+
mid.NewCache("2m").CacheResponse,
56+
headers.CORSHeaders(headers.CORSConfig{
57+
AllowOrigins: []string{"*"},
58+
AllowMethods: []string{http.MethodGet, http.MethodOptions},
59+
AllowHeaders: []string{"Accept, Content-Type, Content-Length, Accept-Encoding"},
60+
}),
61+
headers.SecureHeaders,
62+
headers.ContentTypeHeaders("application/json"),
7063
)
7164

7265
// init global middlewares
7366
middlewaresGlobal := mid.Middlewares(
74-
logger.Log,
75-
limiter.Limit,
76-
brotlihandler.CompressHandler,
67+
mid.Recover,
68+
mid.RequestID,
69+
mid.Log,
70+
mid.Limit(3, 7),
71+
mid.ContextTimeout(15*time.Second),
7772
)
7873

7974
mux.HandleFunc("GET /api/v1/hotels", mid.Bind(midMain, controller.GetHotels))

pkg/cache/cache.go

Lines changed: 0 additions & 65 deletions
This file was deleted.

pkg/cache/cache_test.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

pkg/cache/headers.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)