Skip to content

Commit

Permalink
chore(deps): updates
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed May 27, 2024
1 parent feecd28 commit 7d265b9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 65 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ require (
github.com/fatih/color v1.17.0
github.com/felixge/httpsnoop v1.0.4
github.com/go-courier/logr v0.3.0
github.com/go-json-experiment/json v0.0.0-20240418180308-af2d5061e6c2
github.com/go-json-experiment/json v0.0.0-20240524174822-2d9f40f7385b
github.com/innoai-tech/openapi-playground v0.0.0-20240429061612-ce5b1c0e47b5
github.com/octohelm/courier v0.0.0-20240516021431-5cf7af6666a1
github.com/octohelm/courier v0.0.0-20240527074539-a9eb7212792b
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b
github.com/octohelm/storage v0.0.0-20240516030302-1ac2cc1ea347
github.com/octohelm/x v0.0.0-20240513022938-1bd86d96adef
Expand Down Expand Up @@ -45,7 +45,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.8.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/aws/aws-sdk-go v1.53.8 // indirect
github.com/aws/aws-sdk-go v1.53.10 // indirect
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down Expand Up @@ -90,7 +90,7 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sys v0.20.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+
github.com/aws/aws-sdk-go v1.38.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.53.8 h1:eoqGb1WOHIrCFKo1d51cMcnt1ralfLFaEqRkC5Zzv8k=
github.com/aws/aws-sdk-go v1.53.8/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.53.10 h1:3enP5l5WtezT9Ql+XZqs56JBf5YUd/FEzTCg///OIGY=
github.com/aws/aws-sdk-go v1.53.10/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 h1:6df1vn4bBlDDo4tARvBm7l6KA9iVMnE3NWizDeWSrps=
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3/go.mod h1:CIWtjkly68+yqLPbvwwR/fjNJA/idrtULjZWh2v1ys0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -134,6 +136,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-json-experiment/json v0.0.0-20240418180308-af2d5061e6c2 h1:lhCu2IkNoFfDdcjHos2ZtLdAsyxLZbkpijNzhvvM6BY=
github.com/go-json-experiment/json v0.0.0-20240418180308-af2d5061e6c2/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA=
github.com/go-json-experiment/json v0.0.0-20240524174822-2d9f40f7385b h1:IM96IiRXFcd7l+mU8Sys9pcggoBLbH/dEgzOESrS8F8=
github.com/go-json-experiment/json v0.0.0-20240524174822-2d9f40f7385b/go.mod h1:uDEMZSTQMj7V6Lxdrx4ZwchmHEGdICbjuY+GQd7j9LM=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand Down Expand Up @@ -241,6 +245,7 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/hashicorp/consul/api v1.28.2 h1:mXfkRHrpHN4YY3RqL09nXU1eHKLNiuAN4kHvDQ16k/8=
Expand Down Expand Up @@ -358,8 +363,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/octohelm/courier v0.0.0-20240516021431-5cf7af6666a1 h1:jTrL+dpYmrjTPAC3HaYBeQHzEuN6Ze7/XSTC28uOg24=
github.com/octohelm/courier v0.0.0-20240516021431-5cf7af6666a1/go.mod h1:lYPuTnuOSyTHOweXCoDQbPphAZ00ZMRtctUTGSI0YNE=
github.com/octohelm/courier v0.0.0-20240527074539-a9eb7212792b h1:4La1JkCW4mZLVH1/FTtT5Dn4wu0li+dWj8T08UmClgM=
github.com/octohelm/courier v0.0.0-20240527074539-a9eb7212792b/go.mod h1:lYPuTnuOSyTHOweXCoDQbPphAZ00ZMRtctUTGSI0YNE=
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b h1:z/XJuBmwnxoPPMl2mY11CsU2w8yiLq5q3bwdmkrPCWo=
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b/go.mod h1:mlnC4bXnp0RdKxBf1u4bFQ0pmyDHUfXI9czal6K8lS0=
github.com/octohelm/storage v0.0.0-20240516030302-1ac2cc1ea347 h1:uZmxLDxIXcQ/hOFVMuY0GM2zL62kMxzo6/gsikJVjxk=
Expand Down Expand Up @@ -505,6 +510,8 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -804,6 +811,7 @@ k8s.io/apimachinery v0.30.1 h1:ZQStsEfo4n65yAdlGTfP/uSHMQSoYzU/oeEbkmF7P2U=
k8s.io/apimachinery v0.30.1/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.1 h1:uC/Ir6A3R46wdkgCV3vbLyNOYyCJ8oZnjtJGKfytl/Q=
k8s.io/client-go v0.30.1/go.mod h1:wrAqLNs2trwiCH/wxxmT/x3hKVH9PuV0GGW0oDoHVqc=
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,13 @@ import (
"github.com/felixge/httpsnoop"
)

const acceptEncoding string = "Accept-Encoding"

type compressResponseWriter struct {
compressor io.Writer
w http.ResponseWriter
}

func (cw *compressResponseWriter) WriteHeader(c int) {
cw.w.Header().Del("Content-Length")
cw.w.WriteHeader(c)
}

func (cw *compressResponseWriter) Write(b []byte) (int, error) {
h := cw.w.Header()
if h.Get("Content-Type") == "" {
h.Set("Content-Type", http.DetectContentType(b))
}
h.Del("Content-Length")

return cw.compressor.Write(b)
}

func (cw *compressResponseWriter) ReadFrom(r io.Reader) (int64, error) {
return io.Copy(cw.compressor, r)
}

type flusher interface {
Flush() error
}

func (w *compressResponseWriter) Flush() {
// Flush compressed data if compressor supports it.
if f, ok := w.compressor.(flusher); ok {
_ = f.Flush()
}
// Flush HTTP response.
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
}

// CompressHandlerLevel gzip compresses HTTP responses with specified compression level
// HandlerLevel gzip compresses HTTP responses with specified compression level
// for clients that support it via the 'Accept-Encoding' header.
//
// The compression level should be gzip.DefaultCompression, gzip.NoCompression,
// or any integer value between gzip.BestSpeed and gzip.BestCompression inclusive.
// gzip.DefaultCompression is used in case of invalid compression level.
func CompressHandlerLevel(level int) func(h http.Handler) http.Handler {
func HandlerLevel(level int) func(h http.Handler) http.Handler {
if level < gzip.DefaultCompression || level > gzip.BestCompression {
level = gzip.DefaultCompression
}
Expand All @@ -80,9 +39,6 @@ func CompressHandlerLevel(level int) func(h http.Handler) http.Handler {
}
}

// always add Accept-Encoding to Vary to prevent intermediate caches corruption
w.Header().Add("Vary", acceptEncoding)

// if we weren't able to identify an encoding we're familiar with, pass on the
// request to the handler and return
if encoding == "" {
Expand All @@ -95,6 +51,9 @@ func CompressHandlerLevel(level int) func(h http.Handler) http.Handler {
return
}

// always add Accept-Encoding to Vary to prevent intermediate caches corruption
w.Header().Add("Vary", acceptEncoding)

// wrap the ResponseWriter with the writer for the chosen encoding
var encWriter io.WriteCloser
switch encoding {
Expand Down Expand Up @@ -135,3 +94,44 @@ func CompressHandlerLevel(level int) func(h http.Handler) http.Handler {
})
}
}

const acceptEncoding string = "Accept-Encoding"

type compressResponseWriter struct {
compressor io.Writer
w http.ResponseWriter
}

func (cw *compressResponseWriter) WriteHeader(c int) {
cw.w.Header().Del("Content-Length")
cw.w.WriteHeader(c)
}

func (cw *compressResponseWriter) Write(b []byte) (int, error) {
h := cw.w.Header()
if h.Get("Content-Type") == "" {
h.Set("Content-Type", http.DetectContentType(b))
}
h.Del("Content-Length")

return cw.compressor.Write(b)
}

func (cw *compressResponseWriter) ReadFrom(r io.Reader) (int64, error) {
return io.Copy(cw.compressor, r)
}

type flusher interface {
Flush() error
}

func (w *compressResponseWriter) Flush() {
// Flush compressed data if compressor supports it.
if f, ok := w.compressor.(flusher); ok {
_ = f.Flush()
}
// Flush HTTP response.
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
}
4 changes: 2 additions & 2 deletions pkg/http/middleware/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
"github.com/innoai-tech/infra/pkg/http/compress"
)

func CompressLevelHandlerMiddleware(level int) func(h http.Handler) http.Handler {
return compress.CompressHandlerLevel(level)
func CompressHandlerMiddleware(level int) func(h http.Handler) http.Handler {
return compress.HandlerLevel(level)
}
18 changes: 9 additions & 9 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Server struct {
root courier.Router
svc *http.Server

routerHandlers []handler.HandlerMiddleware
globalHandlers []handler.HandlerMiddleware
globalHandlers []handler.Middleware
routerHandlers []handler.Middleware
}

func (s *Server) SetDefaults() {
Expand All @@ -50,11 +50,11 @@ func (s *Server) ApplyRouter(r courier.Router) {
s.root = r
}

func (s *Server) ApplyRouterHandlers(handlers ...handler.HandlerMiddleware) {
func (s *Server) ApplyRouterHandlers(handlers ...handler.Middleware) {
s.routerHandlers = append(s.routerHandlers, handlers...)
}

func (s *Server) ApplyGlobalHandlers(handlers ...handler.HandlerMiddleware) {
func (s *Server) ApplyGlobalHandlers(handlers ...handler.Middleware) {
s.globalHandlers = append(s.globalHandlers, handlers...)
}

Expand All @@ -73,8 +73,10 @@ func (s *Server) Init(ctx context.Context) error {
s.root,
info.App.String(),
append(
[]handler.HandlerMiddleware{
[]handler.Middleware{
middleware.ContextInjectorMiddleware(configuration.ContextInjectorFromContext(ctx)),
middleware.CompressHandlerMiddleware(gzip.DefaultCompression),
middleware.DefaultCORS(s.corsOptions...),
middleware.LogAndMetricHandler(),
},
s.routerHandlers...,
Expand All @@ -85,16 +87,14 @@ func (s *Server) Init(ctx context.Context) error {
return err
}

globalHandlers := append([]handler.HandlerMiddleware{
middleware.CompressLevelHandlerMiddleware(gzip.DefaultCompression),
middleware.DefaultCORS(s.corsOptions...),
globalHandlers := append([]handler.Middleware{
middleware.MetricHandler(otel.GathererContext.From(ctx)),
middleware.PProfHandler(s.EnableDebug),
}, s.globalHandlers...)

globalHandlers = append(globalHandlers, middleware.HealthCheckHandler())

h = handler.ApplyHandlerMiddlewares(globalHandlers...)(h)
h = handler.ApplyMiddlewares(globalHandlers...)(h)

s.svc = &http.Server{
Addr: s.Addr,
Expand Down
4 changes: 2 additions & 2 deletions pkg/http/webapp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ func ServeFS(f fs.FS, optFns ...OptFunc) http.Handler {
html := o.htmlHandler(f)
static := o.staticFileHandler(f)

return handler.ApplyHandlerMiddlewares(
compress.CompressHandlerLevel(gzip.DefaultCompression),
return handler.ApplyMiddlewares(
compress.HandlerLevel(gzip.DefaultCompression),
)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusNoContent)
Expand Down

0 comments on commit 7d265b9

Please sign in to comment.