Skip to content

Commit

Permalink
feat(app): use addresses instead of just port numbers for services
Browse files Browse the repository at this point in the history
  • Loading branch information
Icikowski committed Mar 31, 2023
1 parent 2cb4034 commit a7b2ee2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ type Config struct {
Logging LoggingConfig `json:"logging"`
AdminAPIService ServiceConfig `envPrefix:"ADMIN_API_" json:"adminApi"`
ContentService ServiceConfig `envPrefix:"CONTENT_" json:"content"`
HealthProbesPort int `env:"HEALTH_PROBES_PORT" envDefault:"8888" json:"healthProbesPort"`
HealthProbesAddr string `env:"HEALTH_PROBES_ADDR" envDefault:":8888" json:"healthProbesAddr"`
MaximumPayloadSize int64 `env:"MAX_PAYLOAD_SIZE" envDefault:"64" json:"maxPayloadSize"`
}
10 changes: 5 additions & 5 deletions src/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
// ServiceConfig represents the configuration of application's service
// and its security
type ServiceConfig struct {
Port int `env:"PORT" json:"port"`
SecuredPort int `env:"SECURED_PORT" json:"securedPort"`
TLSEnabled bool `env:"TLS_ENABLED" envDefault:"false" json:"tlsEnabled"`
TLSCertPath string `env:"TLS_CERT_PATH" json:"tlsCertPath"`
TLSKeyPath string `env:"TLS_KEY_PATH" json:"tlsKeyPath"`
Address string `env:"ADDR" json:"addr"`
SecuredAddress string `env:"SECURED_ADDR" json:"securedAddr"`
TLSEnabled bool `env:"TLS_ENABLED" envDefault:"false" json:"tlsEnabled"`
TLSCertPath string `env:"TLS_CERT_PATH" json:"tlsCertPath"`
TLSKeyPath string `env:"TLS_KEY_PATH" json:"tlsKeyPath"`

tlsCert tls.Certificate
}
Expand Down
8 changes: 4 additions & 4 deletions src/constants/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import "net/http"

// Configuration defaults
const (
DefaultCfgAdminAPIPort int = 8081
DefaultCfgAdminAPISecuredPort int = 8444
DefaultCfgContentPort int = 8080
DefaultCfgContentSecuredPort int = 8443
DefaultCfgAdminAPIAddr string = ":8081"
DefaultCfgAdminAPISecuredAddr string = ":8444"
DefaultCfgContentAddr string = ":8080"
DefaultCfgContentSecuredAddr string = ":8443"
)

// Response defaults
Expand Down
10 changes: 5 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func init() {

cfg = config.Config{
AdminAPIService: config.ServiceConfig{
Port: constants.DefaultCfgAdminAPIPort,
SecuredPort: constants.DefaultCfgAdminAPISecuredPort,
Address: constants.DefaultCfgAdminAPIAddr,
SecuredAddress: constants.DefaultCfgAdminAPISecuredAddr,
},
ContentService: config.ServiceConfig{
Port: constants.DefaultCfgContentPort,
SecuredPort: constants.DefaultCfgContentSecuredPort,
Address: constants.DefaultCfgContentAddr,
SecuredAddress: constants.DefaultCfgContentSecuredAddr,
},
}

Expand Down Expand Up @@ -76,7 +76,7 @@ func main() {
lf.InstanceFor(constants.ComponentServicesManager),
probes.NewProbesService(
lf.InstanceFor(constants.ComponentHealthProbesService),
cfg.HealthProbesPort,
cfg.HealthProbesAddr,
app, adminApi, cnt,
),
content.NewContentService(
Expand Down
11 changes: 5 additions & 6 deletions src/services/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package admin

import (
"context"
"fmt"
"net/http"

"github.com/Icikowski/GoosyMock/config"
Expand Down Expand Up @@ -90,20 +89,20 @@ func (s *AdminAPIService) buildHandler() {
}

func (s *AdminAPIService) buildServers() (*http.Server, *http.Server) {
s.log.Debug().Dict("ports", zerolog.Dict().
Int("plain", s.cfg.Port).
Int("secured", s.cfg.SecuredPort),
s.log.Debug().Dict("addrs", zerolog.Dict().
Str("plain", s.cfg.Address).
Str("secured", s.cfg.SecuredAddress),
).Bool("sslEnabled", s.cfg.TLSEnabled).Msg("building servers")

plainServer := &http.Server{
Addr: fmt.Sprintf(":%d", s.cfg.Port),
Addr: s.cfg.Address,
Handler: s.handler,
}

var securedServer *http.Server
if s.cfg.TLSEnabled {
securedServer = &http.Server{
Addr: fmt.Sprintf(":%d", s.cfg.SecuredPort),
Addr: s.cfg.SecuredAddress,
TLSConfig: s.cfg.GetTLSConfig(),
Handler: s.handler,
}
Expand Down
11 changes: 5 additions & 6 deletions src/services/content/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package content

import (
"context"
"fmt"
"net/http"

"github.com/Icikowski/GoosyMock/config"
Expand Down Expand Up @@ -69,20 +68,20 @@ func (s *ContentService) buildHandler(routes model.Routes) {
}

func (s *ContentService) buildServers() (*http.Server, *http.Server) {
s.log.Debug().Dict("ports", zerolog.Dict().
Int("plain", s.cfg.Port).
Int("secured", s.cfg.SecuredPort),
s.log.Debug().Dict("addrs", zerolog.Dict().
Str("plain", s.cfg.Address).
Str("secured", s.cfg.SecuredAddress),
).Bool("sslEnabled", s.cfg.TLSEnabled).Msg("building servers")

plainServer := &http.Server{
Addr: fmt.Sprintf(":%d", s.cfg.Port),
Addr: s.cfg.Address,
Handler: s.handler,
}

var securedServer *http.Server
if s.cfg.TLSEnabled {
securedServer = &http.Server{
Addr: fmt.Sprintf(":%d", s.cfg.SecuredPort),
Addr: s.cfg.SecuredAddress,
TLSConfig: s.cfg.GetTLSConfig(),
Handler: s.handler,
}
Expand Down
11 changes: 5 additions & 6 deletions src/services/probes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package probes

import (
"context"
"fmt"
"net/http"

"github.com/Icikowski/kubeprobes"
Expand All @@ -12,7 +11,7 @@ import (
// ProbesService represents the service for health probes
type ProbesService struct {
log zerolog.Logger
port int
addr string
appProbe *kubeprobes.StatefulProbe
adminApiProbe *kubeprobes.StatefulProbe
contentProbe *kubeprobes.StatefulProbe
Expand All @@ -23,12 +22,12 @@ type ProbesService struct {
// NewProbesService creates new Probe service instance
func NewProbesService(
log zerolog.Logger,
port int,
addr string,
appProbe, adminApiProbe, contentProbe *kubeprobes.StatefulProbe,
) *ProbesService {
return &ProbesService{
log: log,
port: port,
addr: addr,
appProbe: appProbe,
adminApiProbe: adminApiProbe,
contentProbe: contentProbe,
Expand All @@ -45,10 +44,10 @@ func NewProbesService(
}

func (s *ProbesService) prepareServer() *http.Server {
s.log.Debug().Int("port", s.port).Msg("building server")
s.log.Debug().Str("addr", s.addr).Msg("building server")

return &http.Server{
Addr: fmt.Sprintf(":%d", s.port),
Addr: s.addr,
Handler: s.probes,
}
}
Expand Down

0 comments on commit a7b2ee2

Please sign in to comment.