Skip to content

Commit 4b4f729

Browse files
committedMar 19, 2025
docs: Add compression middleware examples and improve README organization
- Added example using `quick.Handler` for GZIP compression - Added example using `quick.HandlerFunc` for alternative middleware syntax - Added example using native `net/http` middleware for GZIP compression - Improved README structure to clarify middleware options - Added cURL examples for testing compressed and uncompressed responses - Ensured consistency in formatting and documentation across examples
1 parent 6e34889 commit 4b4f729

File tree

6 files changed

+361
-76
lines changed

6 files changed

+361
-76
lines changed
 

‎example/middleware/compress/README.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ When a client sends a request with the header Accept-Encoding: gzip, the middlew
1515
- ✅ Saves bandwidth and enhances user experience
1616
- ✅ Works seamlessly with Quick’s request-handling flow
1717

18-
#### Example Usage
19-
🔹 Enabling GZIP Compression in Quick
18+
19+
20+
#### 🔹 Using Quick Default Middleware (quick.Handler)
2021
```go
2122
package main
2223

@@ -53,6 +54,66 @@ func main() {
5354
}
5455

5556
```
57+
#### 🔹 Using Quick HandlerFunc Middleware (quick.HandlerFunc)
58+
```go
59+
package main
60+
61+
import (
62+
"log"
63+
"github.com/jeffotoni/quick"
64+
"github.com/jeffotoni/quick/middleware/compress"
65+
)
66+
67+
func main() {
68+
q := quick.New()
69+
70+
// Enable GZIP middleware using HandlerFunc version
71+
q.Use(compress.Gzip())
72+
73+
// Define a compressed response route
74+
q.Get("/v1/compress", func(c *quick.Ctx) error {
75+
c.Set("Content-Type", "application/json")
76+
77+
type response struct {
78+
Msg string `json:"msg"`
79+
Headers map[string][]string `json:"headers"`
80+
}
81+
82+
return c.Status(200).JSON(&response{
83+
Msg: "Quick ❤️",
84+
Headers: c.Headers,
85+
})
86+
})
87+
88+
log.Fatal(q.Listen(":8080"))
89+
}
90+
```
91+
#### 🔹 Using Pure net/http Middleware
92+
```go
93+
package main
94+
95+
import (
96+
"log"
97+
"net/http"
98+
"github.com/jeffotoni/quick/middleware/compress"
99+
)
100+
101+
func main() {
102+
mux := http.NewServeMux()
103+
104+
// Route with compression enabled using the middleware
105+
mux.Handle("/v1/compress", compress.Gzip()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106+
w.Header().Set("Content-Type", "application/json")
107+
w.WriteHeader(http.StatusOK)
108+
w.Write([]byte(`{"message": "Hello, net/http with Gzip!"}`))
109+
})))
110+
111+
log.Println("Server running at http://localhost:8080")
112+
log.Fatal(http.ListenAndServe(":8080", mux))
113+
}
114+
```
115+
116+
56117
#### 📌 Testing with cURL
57118

58119
##### 🔹Request Without GZIP (Uncompressed Response):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/jeffotoni/quick"
7+
"github.com/jeffotoni/quick/middleware/compress"
8+
)
9+
10+
func main() {
11+
q := quick.New()
12+
13+
// Enable Gzip middleware
14+
// This will automatically compress responses for clients that support Gzip
15+
q.Use(compress.Gzip())
16+
17+
// Define a route that returns a compressed JSON response
18+
q.Get("/v1/compress", func(c *quick.Ctx) error {
19+
// Setting response headers
20+
c.Set("Content-Type", "application/json")
21+
c.Set("Accept-Encoding", "gzip") // Enabling Gzip compression
22+
23+
// Defining the response structure
24+
type my struct {
25+
Msg string `json:"msg"`
26+
Headers map[string][]string `json:"headers"`
27+
}
28+
29+
// Returning a JSON response with headers
30+
return c.Status(200).JSON(&my{
31+
Msg: "Quick ❤️",
32+
Headers: c.Headers,
33+
})
34+
})
35+
36+
// Start the HTTP server on port 8080
37+
// The server will listen for incoming requests at http://localhost:8080
38+
log.Fatal(q.Listen("0.0.0.0:8080"))
39+
}
40+
41+
// $ curl -X GET http://localhost:8080/v1/compress -H "Accept-Encoding: gzip" --compressed -i
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
//"github.com/jeffotoni/quick/middleware/compress"
7+
)
8+
9+
func main() {
10+
mux := http.NewServeMux()
11+
12+
// Route with compression enabled using the Gzip middleware
13+
// mux.Handle("/v1/compress", compress.Gzip()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
// w.Header().Set("Content-Type", "application/json")
15+
// w.WriteHeader(http.StatusOK)
16+
// w.Write([]byte(`{"message": "Hello, net/http with Gzip!"}`))
17+
// })))
18+
19+
// Starting the HTTP server on port 8080
20+
log.Println("Server running at http://localhost:8080")
21+
log.Fatal(http.ListenAndServe(":8080", mux))
22+
}
23+
24+
// $ curl -X GET http://localhost:8080/v1/compress -H "Accept-Encoding: gzip" --compressed -i

‎example/middleware/compress/compress.gzip/compress_gzip.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,35 @@ import (
77
"github.com/jeffotoni/quick/middleware/compress"
88
)
99

10-
// curl -i -H 'Accept-Encoding: gzip' -GET localhost:8080/v1/compress
1110
func main() {
1211
q := quick.New()
12+
13+
// Enable Gzip middleware
14+
// This will automatically compress responses for clients that support Gzip
1315
q.Use(compress.Gzip())
16+
17+
// Define a route that returns a compressed JSON response
1418
q.Get("/v1/compress", func(c *quick.Ctx) error {
19+
// Setting response headers
1520
c.Set("Content-Type", "application/json")
16-
c.Set("Accept-Encoding", "gzip")
21+
c.Set("Accept-Encoding", "gzip") // Enabling Gzip compression
1722

23+
// Defining the response structure
1824
type my struct {
1925
Msg string `json:"msg"`
2026
Headers map[string][]string `json:"headers"`
2127
}
2228

29+
// Returning a JSON response with headers
2330
return c.Status(200).JSON(&my{
2431
Msg: "Quick ❤️",
2532
Headers: c.Headers,
2633
})
2734
})
2835

36+
// Start the HTTP server on port 8080
37+
// The server will listen for incoming requests at http://localhost:8080
2938
log.Fatal(q.Listen("0.0.0.0:8080"))
30-
3139
}
40+
41+
//$ curl -X GET http://localhost:8080/v1/compress -H "Accept-Encoding: gzip" --compressed -i

0 commit comments

Comments
 (0)