Skip to content

Commit

Permalink
http/server component refactory, mostly renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrzostowski committed Jan 17, 2019
1 parent 07c7abf commit 8935d83
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
17 changes: 9 additions & 8 deletions http/server/server_internal.go → http/server/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
"github.com/microdevs/missy/log"
)

type serverInternal struct {
type metric struct {
server

health *health.Handler
readiness *readiness.Handler
}

func newServerInternal(c Config, l log.FieldsLogger) *serverInternal {
s := &serverInternal{
func newMetric(c Config, l log.FieldsLogger) *metric {
s := &metric{
// TODO what about TLS on metric external?
server: server{
server: &http.Server{
Addr: c.InternalListen,
Addr: c.MetricListen,
},
router: chi.NewRouter(),
l: l,
Expand All @@ -38,10 +39,10 @@ func newServerInternal(c Config, l log.FieldsLogger) *serverInternal {
return s
}

func (si *serverInternal) SetHealth(health bool) {
si.health.Set(health)
func (m *metric) SetHealth(health bool) {
m.health.Set(health)
}

func (si *serverInternal) SetReadiness(ready bool) {
si.readiness.Set(ready)
func (m *metric) SetReadiness(ready bool) {
m.readiness.Set(ready)
}
6 changes: 3 additions & 3 deletions http/server/server_external.go → http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *server) Routes(f func(r Router) error) error {
func (s *server) ListenAndServe() error {
s.server.Handler = s.router
tls := s.certFile != "" || s.keyFile != ""
s.l.Infof("starting server at '%s', TLS=%v", s.server.Addr, tls)
s.l.Infof("starting external at '%s', TLS=%v", s.server.Addr, tls)
if tls {
return s.server.ListenAndServeTLS(s.certFile, s.keyFile)
}
Expand All @@ -50,7 +50,7 @@ func (s *server) ListenAndServe() error {
func (s *server) Shutdown(ctx context.Context) {
err := s.server.Shutdown(ctx)
if err != nil {
s.l.Errorf("stopping server (shutdown) err: %s", err)
s.l.Errorf("stopping external (shutdown) err: %s", err)
}
s.l.Infof("server stopped listenning at '%s'", s.server.Addr)
s.l.Infof("external stopped listening at '%s'", s.server.Addr)
}
40 changes: 23 additions & 17 deletions http/server/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (

type Router chi.Router

// Server defines the http server
// Server defines the http external
type Server interface {
// TODO we should split listen and serve and notify about listen by chan
ListenAndServe(context.Context, context.CancelFunc)
}

type Servers struct {
internal *serverInternal
metric *metric
external *server

tlsCertFile string
Expand All @@ -34,8 +35,9 @@ type Servers struct {
}

type Config struct {
Listen string `env:"HTTP_SERVER_LISTEN" envDefault:"localhost:8080"`
InternalListen string `env:"HTTP_SERVER_INTERNAL_LISTEN" envDefault:"localhost:5000"`
Name string `env:"HTTP_SERVER_NAME" envDefault:"missy"`
Listen string `env:"HTTP_SERVER_LISTEN" envDefault:"localhost:8080"`
MetricListen string `env:"HTTP_SERVER_METRIC_LISTEN" envDefault:"localhost:5000"`

TLSCertFile string `env:"HTTP_SERVER_CERTFILE"`
TLSKeyFile string `env:"HTTP_SERVER_KEYFILE"`
Expand All @@ -44,32 +46,36 @@ type Config struct {
Shutdown time.Duration `env:"HTTP_SERVER_SHUTDOWN_TIMEOUT" envDefault:"30s"`
}

// New returns a new HTTP server object.
func New(name string, c Config, l log.FieldsLogger) *Servers {
// New returns a newServer HTTP external object.
func New(c Config, l log.FieldsLogger) *Servers {
s := &Servers{
internal: newServerInternal(c, l.WithField("http", []string{"server", "internal"})),
metric: newMetric(c, l.WithField("http", []string{"server", "metric"})),
external: newServer(c, l.WithField("http", []string{"server", "external"})),
shutdown: c.Shutdown,
l: l,
}
go func() {
err := s.internal.ListenAndServe()
if err != http.ErrServerClosed {
l.Fatalf("internal server listen and serve err: %s", err)
}
}()
return s
}

func (s *Servers) ListenAndServe(ctx context.Context, cancel context.CancelFunc) {
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)

// metric external
go func() {
err := s.metric.ListenAndServe()
if err != http.ErrServerClosed {
s.l.Fatalf("metric external listen and serve err: %s", err)
}
}()

// We should set this after external is actually listening
s.SetReadiness(true)
// external external
go func() {
err := s.external.ListenAndServe()
if err != http.ErrServerClosed {
s.l.Errorf("external server listen/serve err: %s", err)
s.l.Errorf("external external listen/serve err: %s", err)
s.SetHealth(false)
s.SetReadiness(false)
}
Expand All @@ -82,7 +88,7 @@ func (s *Servers) ListenAndServe(ctx context.Context, cancel context.CancelFunc)
sthCtx, sthCancel := context.WithTimeout(context.Background(), s.shutdown)
defer sthCancel()
s.external.Shutdown(sthCtx)
s.internal.Shutdown(sthCtx)
s.metric.Shutdown(sthCtx)
}

// RoutesRaw method allows to define routes without any standard builtin middlewares.
Expand All @@ -103,9 +109,9 @@ func (s *Servers) Routes(f func(Router) error) error {
}

func (s *Servers) SetHealth(health bool) {
s.internal.SetHealth(health)
s.metric.SetHealth(health)
}

func (s *Servers) SetReadiness(ready bool) {
s.internal.SetReadiness(ready)
s.metric.SetReadiness(ready)
}
5 changes: 4 additions & 1 deletion messaging/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module github.com/microdevs/missy/messaging
require (
github.com/bouk/monkey v1.0.1
github.com/golang/mock v1.2.0
github.com/microdevs/missy/log v0.0.0-20190116135002-8653ec40eef5
github.com/microdevs/missy/log v0.0.0-20190116154347-07c7abf46253
github.com/pkg/errors v0.8.1
github.com/segmentio/kafka-go v0.2.2
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/sys v0.0.0-20190116151225-e5ecc2a6747c // indirect
)
9 changes: 9 additions & 0 deletions messaging/go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
github.com/bouk/monkey v1.0.1 h1:82kWEtyEjyfkRZb0DaQ5+7O5dJfe3GzF/o97+yUo5d0=
github.com/bouk/monkey v1.0.1/go.mod h1:PG/63f4XEUlVyW1ttIeOJmJhhe1+t9EC/je3eTjvFhE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/microdevs/missy/log v0.0.0-20190116135002-8653ec40eef5 h1:IEycL38TqkmD4jD5eNOJ14PIf9AIZQ4lV4bXlRT5GIs=
github.com/microdevs/missy/log v0.0.0-20190116135002-8653ec40eef5/go.mod h1:W9uWwkMGtliUxwaOpnR8XOoVlNip/r7zFQExwupRksA=
github.com/microdevs/missy/log v0.0.0-20190116154347-07c7abf46253 h1:gq2kJXUXoRD9QMc/hyRx+jO8b54Ug0/o2zAwk4WVjl4=
github.com/microdevs/missy/log v0.0.0-20190116154347-07c7abf46253/go.mod h1:W9uWwkMGtliUxwaOpnR8XOoVlNip/r7zFQExwupRksA=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/kafka-go v0.2.2 h1:KIUln5unPisRL2yyAkZsDR/coiymN9Djunv6JKGQ6JI=
github.com/segmentio/kafka-go v0.2.2/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190116151225-e5ecc2a6747c h1:svsPMPI5t/8N66mbpypEFmUYN2qOh2GCKBIHX6SOeoU=
golang.org/x/sys v0.0.0-20190116151225-e5ecc2a6747c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

0 comments on commit 8935d83

Please sign in to comment.