Skip to content

Commit

Permalink
feat: add gzip for client (#4)
Browse files Browse the repository at this point in the history
* fix

* refactor: change varname

* fix

* style: add header

* feat: handle resp

* fix: resolve resp

* feat: change gzip client logic

* fix: judge shouldCompress

* test: add test for gzip

* test: add test

* feat: add test

* test: add test for gzip

* style: gofumpt

* style: license header

* style: header

* style: header

* test: fix test port

* fix: unit test

* fix: use strings.EqualFold to judge header

* refactor: change package name && merge test file

* refactor: separate client/srv Options && rename package handler

* doc: add readme for gzip for client

* feat: optimize example

* sytle: use printf instead instead of sprintf
  • Loading branch information
zstone12 authored Mar 8, 2023
1 parent 759c00e commit faffd46
Show file tree
Hide file tree
Showing 10 changed files with 880 additions and 229 deletions.
26 changes: 0 additions & 26 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,8 @@ header:
license:
spdx-id: Apache-2.0
copyright-owner: CloudWeGo Authors
content: |
MIT License
Copyright (c) 2019 Gin-Gonic
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.
This file may have been modified by CloudWeGo authors. All CloudWeGo
Modifications are Copyright 2022 CloudWeGo Authors.

paths:
- '**/*.go'
- '**/*.s'
-
comment: on-failure
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ Import it in your code:
import "github.com/hertz-contrib/gzip"
```

### For server

Canonical example:

```go
package main

Expand All @@ -43,7 +46,10 @@ func main() {
}

```


Customized Excluded Extensions

```go
package main

Expand Down Expand Up @@ -123,6 +129,105 @@ func main() {
}
```

### For client

Canonical example:

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression))
_, _, _ = client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
}
```

Customized Excluded Extensions

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression,gzip.WithExcludedPathsForClient([]string{"/api/"})))
_, _, _ = client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
}
```

Customized Excluded Paths

```go
package main

import (
"context"
"fmt"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression, gzip.WithExcludedExtensionsForClient([]string{".pdf", ".mp4"})))
statusCode, body, err := client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
fmt.Printf("%d, %s, %s", statusCode, body, err)
}

```

Customized Excluded Paths

```go
package main

import (
"context"
"fmt"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression, gzip.WithExcludedPathRegexesForClient([]string{".*"})))
statusCode, body, err := client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
fmt.Printf("%d, %s, %s", statusCode, body, err)
}
```

## License

This project is under Apache License. See the [LICENSE](LICENSE) file for the full license text.
104 changes: 102 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

## 使用

下载并且安装它:
下载并且安装它

```sh
go get github.com/hertz-contrib/gzip
```

导入进你的代码:
导入进你的代码

```go
import "github.com/hertz-contrib/gzip"
```
### 服务端

建议示例:
```go
Expand Down Expand Up @@ -123,6 +124,105 @@ func main() {
}
```

### 客户端

建议示例:

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression))
_, _, _ = client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
}
```

自定义排除的扩展

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression,gzip.WithExcludedPathsForClient([]string{"/api/"})))
_, _, _ = client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
}
```

定制的排除路径

```go
package main

import (
"context"
"fmt"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression, gzip.WithExcludedExtensionsForClient([]string{".pdf", ".mp4"})))
statusCode, body, err := client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
fmt.Printf("%d, %s, %s", statusCode, body, err)
}

```

定制的排除路径

```go
package main

import (
"context"
"fmt"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/gzip"
)

func main() {
client, _ := client.NewClient()
client.Use(gzip.GzipForClient(gzip.DefaultCompression, gzip.WithExcludedPathRegexesForClient([]string{".*"})))
statusCode, body, err := client.Post(context.Background(),
[]byte{},
"http://localhost:8080/ping",
&protocol.Args{})
fmt.Printf("%d, %s, %s", statusCode, body, err)
}
```


## 许可证

Expand Down
Loading

0 comments on commit faffd46

Please sign in to comment.