Skip to content

Commit c0f9745

Browse files
docs: add ExampleGzip and improve compress documentation
- Added ExampleGzip to demonstrate Gzip middleware usage. - Updated README for compress middleware with detailed explanations. - Structured documentation with three ways to use compress: - quick.Handler (default) - quick.HandlerFunc - net/http standard implementation - Included a direct link to the compress documentation.
1 parent 4b4f729 commit c0f9745

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,112 @@ $ go run main.go
26302630
```bash
26312631
$ k6 run benchmark.js
26322632
```
2633+
---
2634+
## 📦 Compression Middleware (compress)
2635+
The compress middleware in Quick enables automatic GZIP compression for HTTP responses, reducing the size of data transferred over the network. This improves performance and bandwidth efficiency, especially for text-based content like JSON, HTML, and CSS.
2636+
2637+
### 🚀 Benefits of Compression
2638+
- ✅ Reduced response size – improves loading speed.
2639+
- ✅ Bandwidth savings – ideal for mobile or limited connections.
2640+
- ✅ Seamless integration – works automatically for compatible clients.
2641+
- ✅ Better user experience – faster response times.
2642+
2643+
🔹 Ways to Use
2644+
Quick provides three different ways to enable GZIP compression:
2645+
2646+
### 🌟 Available Usage Methods
2647+
2648+
Quick provides three different ways to enable GZIP compression:
2649+
2650+
- Using quick.Handler (Default) – Follows Quick’s native syntax.
2651+
- Using quick.HandlerFunc – Alternative method for direct function-based handlers.
2652+
- Using net/http standard implementation – For applications using Go’s native HTTP package.
2653+
2654+
#### For more details on using compress, check the documentation:
2655+
2656+
<h4 align="left">
2657+
<p>
2658+
<a href="middleware/README.md">
2659+
<strong>📖 Compress Documentation</strong>
2660+
</a>
2661+
</p>
2662+
</h4>
2663+
2664+
### 🚀 Usage Example (Default)
2665+
Here is a practical example of enabling the GZIP middleware in Quick using the default approach (quick.Handler)
2666+
2667+
```go
2668+
package main
2669+
2670+
import (
2671+
"log"
2672+
2673+
"github.com/jeffotoni/quick"
2674+
"github.com/jeffotoni/quick/middleware/compress"
2675+
)
2676+
2677+
func main() {
2678+
q := quick.New()
2679+
2680+
// Enable Gzip middleware
2681+
q.Use(compress.Gzip())
2682+
2683+
// Define a route that returns a compressed JSON response
2684+
q.Get("/v1/compress", func(c *quick.Ctx) error {
2685+
// Setting response headers
2686+
c.Set("Content-Type", "application/json")
2687+
// Enabling Gzip compression
2688+
c.Set("Accept-Encoding", "gzip")
2689+
// Defining the response structure
2690+
type response struct {
2691+
Msg string `json:"msg"`
2692+
Headers map[string][]string `json:"headers"`
2693+
}
2694+
2695+
// Returning a JSON response with headers
2696+
return c.Status(200).JSON(&response{
2697+
Msg: "Quick ❤️",
2698+
Headers: c.Headers,
2699+
})
2700+
})
2701+
2702+
// Start the HTTP server on port 8080
2703+
log.Fatal(q.Listen("0.0.0.0:8080"))
2704+
}
2705+
```
2706+
### 📌 cURL
2707+
```bash
2708+
$ curl -X GET http://localhost:8080/v1/compress -H
2709+
"Accept-Encoding: gzip" --compressed -i
2710+
```
2711+
### 📌 Response
2712+
```bash
2713+
{
2714+
"msg":"Quick ❤️",
2715+
"headers":{
2716+
"Accept":[
2717+
"*/*"
2718+
],
2719+
"Accept-Encoding":[
2720+
"gzip"
2721+
],
2722+
"Cache-Control":[
2723+
"no-cache"
2724+
],
2725+
"Connection":[
2726+
"keep-alive"
2727+
],
2728+
"Postman-Token":[
2729+
"e0b65cfe-9516-4803-96df-d443d7e6a95a"
2730+
],
2731+
"User-Agent":[
2732+
"PostmanRuntime/7.43.2"
2733+
]
2734+
}
2735+
}
2736+
```
2737+
2738+
26332739
---
26342740

26352741
## 📚| More Examples
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package compress
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/jeffotoni/quick"
8+
)
9+
10+
// This function is named ExampleGzip()
11+
// it with the Examples type.
12+
func ExampleGzip() {
13+
// Starting Quick framework instance
14+
q := quick.New()
15+
16+
// Enable Gzip middleware
17+
// This will automatically compress responses for clients that support Gzip
18+
q.Use(Gzip())
19+
20+
// Define a route that returns a compressed JSON response
21+
q.Get("/v1/compress", func(c *quick.Ctx) error {
22+
// Setting response headers
23+
c.Set("Content-Type", "application/json")
24+
25+
// Defining the response structure
26+
type response struct {
27+
Msg string `json:"msg"`
28+
Headers map[string][]string `json:"headers"`
29+
}
30+
31+
// Returning a JSON response with headers
32+
return c.Status(200).JSON(&response{
33+
Msg: "Quick ❤️",
34+
Headers: c.Headers,
35+
})
36+
})
37+
38+
// Simulate a GET request with headers using Quick's testing functionality
39+
res, err := q.Qtest(quick.QuickTestOptions{
40+
Method: quick.MethodGet,
41+
URI: "/v1/compress",
42+
Headers: map[string]string{"Accept-Encoding": "gzip"},
43+
})
44+
if err != nil {
45+
log.Fatalf("Error running test request: %v", err)
46+
}
47+
48+
// Print the response body
49+
fmt.Println(res.BodyStr())
50+
51+
// Out put:
52+
// {"msg":"Quick ❤️","headers":{"Accept-Encoding":["gzip"]}}
53+
}

0 commit comments

Comments
 (0)