Skip to content

Commit

Permalink
Changed graceful shutdown mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 14, 2022
1 parent 9f90069 commit c96105b
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 437 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/PuerkitoBio/purell v1.2.0
github.com/gojuno/minimock/v3 v3.0.10
github.com/hashicorp/go-multierror v1.1.1
github.com/pseidemann/finish v1.2.0
github.com/pterm/pterm v0.12.49
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.0.0-20221004154528-8021a29435af
Expand All @@ -17,7 +17,6 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/lithammer/fuzzysearch v1.1.5 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQ
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hexdigest/gowrap v1.1.7/go.mod h1:Z+nBFUDLa01iaNM+/jzoOA1JJ7sm51rnYFauKFUB5fs=
github.com/hexdigest/gowrap v1.1.8/go.mod h1:H/JiFmQMp//tedlV8qt2xBdGzmne6bpbaSuiHmygnMw=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -90,6 +85,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/pseidemann/finish v1.2.0 h1:XrEc9FCnBPulyM9NvAptAtcOCZZYHwV0MRCcnCfQlnw=
github.com/pseidemann/finish v1.2.0/go.mod h1:Wl17vXLhlT9a/K7jryhExgJPfbs4+dUpRaauEWt7oQ4=
github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI=
github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg=
github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE=
Expand Down
5 changes: 0 additions & 5 deletions internal/infrastructure/http.go

This file was deleted.

25 changes: 25 additions & 0 deletions internal/infrastructure/httpclient_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package infrastructure

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,6 +16,24 @@ func TestMakeHTTPClient(t *testing.T) {
assert.Equal(t, client, &defaultHTTPClient)
})

t.Run("check redirect should return error", func(t *testing.T) {
t.Run("for default client", func(t *testing.T) {
client, err := MakeHTTPClient("")

assert.NoError(t, err)
err = client.CheckRedirect(nil, nil)
assert.ErrorIs(t, http.ErrUseLastResponse, err)
})

t.Run("for client with proxy", func(t *testing.T) {
client, err := MakeHTTPClient("http://localhost:8000")

assert.NoError(t, err)
err = client.CheckRedirect(nil, nil)
assert.ErrorIs(t, http.ErrUseLastResponse, err)
})
})

t.Run("return configured client where proxy is set", func(t *testing.T) {
client, err := MakeHTTPClient("http://localhost:8000")

Expand All @@ -28,4 +47,10 @@ func TestMakeHTTPClient(t *testing.T) {

assert.EqualError(t, err, "failed to create http client: parse \"http://loca^host:8000\": invalid character \"^\" in host name")
})

t.Run("return error where urls is incorrect", func(t *testing.T) {
_, err := MakeHTTPClient("http://loca^host:8000")

assert.EqualError(t, err, "failed to create http client: parse \"http://loca^host:8000\": invalid character \"^\" in host name")
})
}
7 changes: 7 additions & 0 deletions internal/infrastructure/noop-logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package infrastructure

type NoopLogger struct{}

func (n NoopLogger) Infof(format string, v ...interface{}) {}

func (n NoopLogger) Errorf(format string, v ...interface{}) {}
19 changes: 19 additions & 0 deletions internal/infrastructure/noop-logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package infrastructure_test

import (
"testing"

"github.com/evg4b/uncors/internal/infrastructure"
)

func TestNoopLogger(t *testing.T) {
noopLogger := infrastructure.NoopLogger{}

t.Run("Infof do nothing", func(t *testing.T) {
noopLogger.Infof("")
})

t.Run("Errorf do nothing", func(t *testing.T) {
noopLogger.Errorf("")
})
}
18 changes: 18 additions & 0 deletions internal/infrastructure/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package infrastructure

import (
"net"
"net/http"
"strconv"
"time"
)

const readHeaderTimeout = 30 * time.Second

func NewServer(addr string, port int, handler http.Handler) *http.Server {
return &http.Server{
ReadHeaderTimeout: readHeaderTimeout,
Handler: handler,
Addr: net.JoinHostPort(addr, strconv.Itoa(port)),
}
}
19 changes: 19 additions & 0 deletions internal/infrastructure/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package infrastructure_test

import (
"net/http"
"testing"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/stretchr/testify/assert"
)

func TestNewServer(t *testing.T) {
t.Run("provide correct parameters", func(t *testing.T) {
server := infrastructure.NewServer("0.0.0.0", 3000, http.DefaultServeMux)

assert.NotNil(t, server)
assert.Equal(t, http.DefaultServeMux, server.Handler)
assert.Equal(t, "0.0.0.0:3000", server.Addr)
})
}
4 changes: 2 additions & 2 deletions internal/options/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"strings"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/evg4b/uncors/internal/processor"
"github.com/evg4b/uncors/internal/responseprinter"
"github.com/pterm/pterm"
)
Expand All @@ -15,7 +15,7 @@ func NewOptionsMiddleware() *OptionsMiddleware {
return &OptionsMiddleware{}
}

func (pm *OptionsMiddleware) Wrap(next infrastructure.HandlerFunc) infrastructure.HandlerFunc {
func (pm *OptionsMiddleware) Wrap(next processor.HandlerFunc) processor.HandlerFunc {
optionsWriter := pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Expand Down
7 changes: 4 additions & 3 deletions internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"fmt"
"net/http"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/pterm/pterm"
)

var ErrFailedRequest = errors.New("failed request handler")

type HandlerFunc = func(http.ResponseWriter, *http.Request) error

type HandlingMiddleware interface {
Wrap(next infrastructure.HandlerFunc) infrastructure.HandlerFunc
Wrap(next HandlerFunc) HandlerFunc
}

type RequestProcessor struct {
handlerFunc infrastructure.HandlerFunc
handlerFunc HandlerFunc
}

func NewRequestProcessor(options ...RequestProcessorOption) *RequestProcessor {
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"net/http"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/evg4b/uncors/internal/processor"
"github.com/evg4b/uncors/internal/responseprinter"
"github.com/evg4b/uncors/internal/urlreplacer"
"github.com/pterm/pterm"
Expand All @@ -27,7 +27,7 @@ func NewProxyMiddleware(options ...MiddlewareOption) *ProxyMiddleware {
return middleware
}

func (pm *ProxyMiddleware) Wrap(_ infrastructure.HandlerFunc) infrastructure.HandlerFunc {
func (pm *ProxyMiddleware) Wrap(_ processor.HandlerFunc) processor.HandlerFunc {
proxyWriter := pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Expand Down
30 changes: 0 additions & 30 deletions internal/server/helpers.go

This file was deleted.

69 changes: 0 additions & 69 deletions internal/server/options.go

This file was deleted.

75 changes: 0 additions & 75 deletions internal/server/server.go

This file was deleted.

Loading

0 comments on commit c96105b

Please sign in to comment.