Skip to content

Commit

Permalink
Merge pull request #216 from foomo/gzip-middleware
Browse files Browse the repository at this point in the history
http gzip middleware/roundtripware
  • Loading branch information
franklinkim authored Nov 22, 2024
2 parents 3f78508 + 1e428a4 commit 7e8c5c7
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 39 deletions.
55 changes: 30 additions & 25 deletions CODE_OF_CONDUCT.md → .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Contributor Covenant Code of Conduct

## Our Pledge
Expand All @@ -6,8 +7,8 @@ We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
Expand All @@ -22,17 +23,17 @@ community include:
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
* Focusing on what is best not just for us as individuals, but for the overall
community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

Expand All @@ -52,15 +53,15 @@ decisions when appropriate.

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
Examples of representing our community include using an official email address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
info@bestbytes.de.
info@bestbytes.com.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand All @@ -82,15 +83,15 @@ behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series
of actions.
**Community Impact**: A violation through a single incident or series of
actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
like social media. Violating these terms may lead to a temporary or permanent
ban.

### 3. Temporary Ban

Expand All @@ -106,23 +107,27 @@ Violating these terms may lead to a permanent ban.
### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within
the community.
**Consequence**: A permanent ban from any sort of public interaction within the
community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].

[homepage]: https://www.contributor-covenant.org
Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
[https://www.contributor-covenant.org/translations][translations].

[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations
Binary file added .github/assets/keel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Foomo web framework
Copyright (c) foomo by bestbytes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ doc:
.PHONY: test
## Run tests
test:
@go test -p 1 -coverprofile=coverage.out -race -json ./... | gotestfmt
@GO_TEST_TAGS=-skip go test -coverprofile=coverage.out -race -json ./... | gotestfmt

.PHONY: lint
## Run linter
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# keel

[![Build Status](https://github.com/foomo/keel/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/foomo/keel/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/foomo/keel)](https://goreportcard.com/report/github.com/foomo/keel)
[![Coverage Status](https://coveralls.io/repos/github/foomo/keel/badge.svg?branch=main&)](https://coveralls.io/github/foomo/keel?branch=main)
[![GoDoc](https://godoc.org/github.com/foomo/keel?status.svg)](https://godoc.org/github.com/foomo/keel)

> Opinionated way to run services.
<p align="center">
<img alt="sesamy" src=".github/assets/keel.png"/>
</p>

# keel

> Opinionated way to run services on Kubernetes
## Stack

- Zap
- Viper
- Open Telemetry
- Nats
- Viper
- GoTSRPC
- Temporal
- OpenTelemetry

## Examples

Expand Down Expand Up @@ -64,3 +69,5 @@ Make a pull request...
## License

Distributed under MIT License, please see license file within the code for more details.

_Made with ♥ [foomo](https://www.foomo.org) by [bestbytes](https://www.bestbytes.com)_
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module github.com/foomo/keel
go 1.23.0

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/avast/retry-go/v4 v4.6.0
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/fbiville/markdown-table-formatter v0.3.0
github.com/foomo/go v0.0.3
github.com/foomo/gostandards v0.2.0
github.com/foomo/gotsrpc/v2 v2.8.2
github.com/go-logr/logr v1.4.2
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.6.0
github.com/klauspost/compress v1.17.11
github.com/lib/pq v1.10.9
github.com/mitchellh/mapstructure v1.5.0
github.com/nats-io/nats.go v1.37.0
Expand Down Expand Up @@ -96,7 +99,6 @@ require (
github.com/hashicorp/serf v0.10.1 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJ
github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -88,6 +88,10 @@ github.com/fbiville/markdown-table-formatter v0.3.0 h1:PIm1UNgJrFs8q1htGTw+wnnNY
github.com/fbiville/markdown-table-formatter v0.3.0/go.mod h1:q89TDtSEVDdTaufgSbfHpNVdPU/bmfvqNkrC5HagmLY=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/foomo/go v0.0.3 h1:5pGzcPC78dImuBTT7nsZZnH+GIQUylbCtMkFEH26uZk=
github.com/foomo/go v0.0.3/go.mod h1:x6g64wiQusqaFElnh5rlk9unCgLKmfUWy0YFLejJxio=
github.com/foomo/gostandards v0.2.0 h1:Ryd7TI9yV3Xk5B84DcUDB7KcL3LzQ8NS+TVOrFxTYfA=
github.com/foomo/gostandards v0.2.0/go.mod h1:XQx7Ur6vyvxaIe2cQvAthuhPYDe+d2soibqVcXDXOh4=
github.com/foomo/gotsrpc/v2 v2.8.2 h1:8TNHPIvYIIzfaRYOLK9OP1S1FN+yrrZ3a2UH6TGddtQ=
github.com/foomo/gotsrpc/v2 v2.8.2/go.mod h1:CcBiruenqIcpn+XeuOpedaJcsPeBtLt1XNQwJnhQ7rg=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down
3 changes: 1 addition & 2 deletions net/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"net/url"
"time"

"github.com/foomo/keel/net/http/roundtripware"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"

"github.com/foomo/keel/net/http/roundtripware"
)

type HTTPClientOption func(*http.Client)
Expand Down
102 changes: 102 additions & 0 deletions net/http/middleware/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package middleware

import (
"io"
"net/http"
"sync"

stdhttp "github.com/foomo/gostandards/http"
httputils "github.com/foomo/keel/utils/net/http"
"github.com/klauspost/compress/gzhttp"
"github.com/klauspost/compress/gzip"
"github.com/pkg/errors"
"go.uber.org/zap"
)

type (
GZipOptions struct {
CompressionLevel int
MinSize int
}
GZipOption func(*GZipOptions)
)

var DefaultGZipOptions = GZipOptions{
CompressionLevel: gzip.DefaultCompression,
MinSize: 1024,
}

// GZipWithLevel allows setting a specific compression level for gzip (default: gzip.DefaultCompression).
func GZipWithLevel(v int) GZipOption {
return func(o *GZipOptions) {
o.CompressionLevel = v
}
}

// GZipWithMinSize allows setting a minimum response body length to apply gzip compression (default: 1400 bytes).
func GZipWithMinSize(v int) GZipOption {
return func(o *GZipOptions) {
o.MinSize = v
}
}

// GZip middleware
func GZip(opts ...GZipOption) Middleware {
options := DefaultGZipOptions
for _, opt := range opts {
if opt != nil {
opt(&options)
}
}
return GZipWithOptions(options)
}

// GZipWithOptions middleware
func GZipWithOptions(opts GZipOptions) Middleware {
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
pool := sync.Pool{
New: func() interface{} {
return new(gzip.Reader)
},
}
wrapper, err := gzhttp.NewWrapper(
gzhttp.CompressionLevel(opts.CompressionLevel),
gzhttp.MinSize(opts.MinSize),
)
if err != nil {
panic(err)
}
return wrapper(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(stdhttp.HeaderContentEncoding.String()) != stdhttp.EncodingGzip.String() {
next.ServeHTTP(w, r)
return
}

gr, ok := pool.Get().(*gzip.Reader)
if !ok {
httputils.InternalServerError(l, w, r, errors.New("failed to retrieve gzip pool"))
return
}
defer pool.Put(gr)

b := r.Body
defer b.Close()

if err := gr.Reset(b); errors.Is(err, io.EOF) {
next.ServeHTTP(w, r)
return
} else if err != nil {
httputils.BadRequestServerError(l, w, r, errors.New("failed to reset gzip"))
return
}

defer gr.Close()

r.Header.Del(stdhttp.HeaderContentEncoding.String())

r.Body = gr

next.ServeHTTP(w, r)
}))
}
}
Loading

0 comments on commit 7e8c5c7

Please sign in to comment.