Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit e2a49b8

Browse files
authoredOct 13, 2020
Only listen on one port for HTTP (#67)
Gets rid of the external/internal differentiation.
1 parent 877f7bd commit e2a49b8

File tree

10 files changed

+71
-110
lines changed

10 files changed

+71
-110
lines changed
 

‎cmd/server/config.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import (
88

99
// Config holds configuration data read from a config file.
1010
type Config struct {
11-
ExternalHost string
12-
ExternalPort int
13-
InternalHost string
14-
InternalPort int
15-
Cert string
16-
Key string
17-
Emailer struct {
11+
Host string
12+
Port int
13+
Cert string
14+
Key string
15+
Emailer struct {
1816
Token string
1917
}
2018
Storer struct {

‎cmd/server/main.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ func main() {
2222
}
2323

2424
s := server.New(server.Options{
25-
Emailer: createEmailer(c),
26-
Storer: createStorer(c, logger),
27-
Logger: logger,
28-
ExternalHost: c.ExternalHost,
29-
ExternalPort: c.ExternalPort,
30-
InternalHost: c.InternalHost,
31-
InternalPort: c.InternalPort,
32-
Cert: c.Cert,
33-
Key: c.Key,
25+
Emailer: createEmailer(c),
26+
Storer: createStorer(c, logger),
27+
Logger: logger,
28+
Host: c.Host,
29+
Port: c.Port,
30+
Cert: c.Cert,
31+
Key: c.Key,
3432
})
3533

3634
if err := s.Start(); err != nil {

‎development.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
ExternalHost = "localhost"
2-
ExternalPort = 2020
3-
InternalHost = "localhost"
4-
InternalPort = 2021
1+
Host = "localhost"
2+
Port = 2020
53
Cert = "cert.pem"
64
Key = "key.pem"
75

‎docker-compose.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ services:
1717
dockerfile: Dockerfile
1818
ports:
1919
- "2020:2020"
20-
- "2021:2021"
2120
depends_on:
2221
- db
2322
restart: always

‎docker.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
ExternalHost = "localhost"
2-
ExternalPort = 2020
3-
InternalHost = "localhost"
4-
InternalPort = 2021
1+
Host = ""
2+
Port = 2020
53
Cert = "cert.pem"
64
Key = "key.pem"
75

‎production/etc/ahead.toml

-2
This file was deleted.

‎production/etc/caddy/Caddyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ app.example.com {
55
# to otherhost2:2020
66

77
health_path /health
8-
health_port 2021
8+
health_port 2020
99
health_interval 10s
1010
health_timeout 5s
1111
}

‎server/routes.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"github.com/maragudk/go-ahead/views"
1111
)
1212

13-
func (s *Server) setupExternalRoutes() {
13+
func (s *Server) setupRoutes() {
1414
// The views that can be requested by the browser
15-
s.externalMux.Group(func(r chi.Router) {
15+
s.mux.Group(func(r chi.Router) {
1616
r.Use(middleware.Recoverer, handlers.NoClickjacking, handlers.StrictContentSecurityPolicy)
1717
r.Use(s.sm.LoadAndSave)
1818

@@ -28,7 +28,7 @@ func (s *Server) setupExternalRoutes() {
2828
})
2929

3030
// The REST API
31-
s.externalMux.Route("/api", func(r chi.Router) {
31+
s.mux.Route("/api", func(r chi.Router) {
3232
r.Use(middleware.Recoverer, handlers.JSONHeader)
3333
r.Use(s.sm.LoadAndSave)
3434

@@ -44,9 +44,12 @@ func (s *Server) setupExternalRoutes() {
4444
r.Get("/session", handlers.Session())
4545
})
4646
})
47-
}
4847

49-
func (s *Server) setupInternalRoutes() {
50-
s.internalMux.Get("/health", handlers.Health(s.Storer))
51-
s.internalMux.Get("/metrics", handlers.Metrics())
48+
// Health and metrics
49+
s.mux.Group(func(r chi.Router) {
50+
r.Use(middleware.Recoverer)
51+
52+
r.Get("/health", handlers.Health(s.Storer))
53+
r.Get("/metrics", handlers.Metrics())
54+
})
5255
}

‎server/server.go

+42-72
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,25 @@ import (
2323

2424
// Server takes requests and responds. 😎
2525
type Server struct {
26-
Emailer *comms.Emailer
27-
Storer *storage.Storer
28-
externalAddress string
29-
internalAddress string
30-
externalMux *chi.Mux
31-
internalMux *chi.Mux
32-
log *log.Logger
33-
cert string
34-
key string
35-
sm *scs.SessionManager
26+
Emailer *comms.Emailer
27+
Storer *storage.Storer
28+
address string
29+
mux *chi.Mux
30+
log *log.Logger
31+
cert string
32+
key string
33+
sm *scs.SessionManager
3634
}
3735

3836
// Options for New.
3937
type Options struct {
40-
Emailer *comms.Emailer
41-
Storer *storage.Storer
42-
Logger *log.Logger
43-
ExternalHost string
44-
ExternalPort int
45-
InternalHost string
46-
InternalPort int
47-
Cert string
48-
Key string
38+
Emailer *comms.Emailer
39+
Storer *storage.Storer
40+
Logger *log.Logger
41+
Host string
42+
Port int
43+
Cert string
44+
Key string
4945
}
5046

5147
// New creates a new Server.
@@ -55,20 +51,18 @@ func New(options Options) *Server {
5551
logger = log.New(ioutil.Discard, "", 0)
5652
}
5753
return &Server{
58-
Emailer: options.Emailer,
59-
Storer: options.Storer,
60-
externalAddress: net.JoinHostPort(options.ExternalHost, strconv.Itoa(options.ExternalPort)),
61-
internalAddress: net.JoinHostPort(options.InternalHost, strconv.Itoa(options.InternalPort)),
62-
externalMux: chi.NewRouter(),
63-
internalMux: chi.NewRouter(),
64-
log: logger,
65-
cert: options.Cert,
66-
key: options.Key,
67-
sm: scs.New(),
54+
Emailer: options.Emailer,
55+
Storer: options.Storer,
56+
address: net.JoinHostPort(options.Host, strconv.Itoa(options.Port)),
57+
mux: chi.NewRouter(),
58+
log: logger,
59+
cert: options.Cert,
60+
key: options.Key,
61+
sm: scs.New(),
6862
}
6963
}
7064

71-
// Start the server, setting up listening for HTTP externally and internally, and notify systemd of readiness.
65+
// Start the server, set up listening for HTTP, and notify systemd of readiness.
7266
func (s *Server) Start() error {
7367

7468
if err := s.Storer.Connect(); err != nil {
@@ -78,18 +72,11 @@ func (s *Server) Start() error {
7872
s.sm.Store = postgresstore.NewWithCleanupInterval(s.Storer.DB.DB, 0)
7973
s.sm.Cookie.Secure = true
8074

81-
s.setupInternalRoutes()
82-
s.setupExternalRoutes()
75+
s.setupRoutes()
8376

8477
errs := make(chan error)
8578
go func() {
86-
if err := s.listenAndServeInternal(); err != nil {
87-
errs <- err
88-
}
89-
}()
90-
91-
go func() {
92-
if err := s.listenAndServeExternal(); err != nil {
79+
if err := s.listenAndServe(); err != nil {
9380
errs <- err
9481
}
9582
}()
@@ -102,46 +89,29 @@ func (s *Server) Start() error {
10289
return errors2.Wrap(err, "could not listen and serve")
10390
}
10491

105-
// listenAndServeExternal on the external address.
106-
// Note that all routes should be defined on externalMux before calling this.
107-
func (s *Server) listenAndServeExternal() error {
108-
server := createHTTPServer(s.externalAddress, s.externalMux, s.log)
92+
// listenAndServe for HTTP.
93+
// Note that all routes should be defined on mux before calling this.
94+
func (s *Server) listenAndServe() error {
95+
server := http.Server{
96+
Addr: s.address,
97+
Handler: s.mux,
98+
ReadTimeout: 5 * time.Second,
99+
ReadHeaderTimeout: 5 * time.Second,
100+
WriteTimeout: 5 * time.Second,
101+
IdleTimeout: 5 * time.Second,
102+
ErrorLog: s.log,
103+
}
109104

110105
if s.key != "" && s.cert != "" {
111-
s.log.Printf("Listening for external HTTPS on https://%v\n", s.externalAddress)
106+
s.log.Printf("Listening for HTTPS on https://%v\n", s.address)
112107
if err := server.ListenAndServeTLS(s.cert, s.key); err != nil && !errors.Is(err, http.ErrServerClosed) {
113-
return errors2.Wrap(err, "could not start external https listener")
108+
return errors2.Wrap(err, "could not start https listener")
114109
}
115110
return nil
116111
}
117-
s.log.Printf("Listening for external HTTP on http://%v\n", s.externalAddress)
112+
s.log.Printf("Listening for HTTP on http://%v\n", s.address)
118113
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
119-
return errors2.Wrap(err, "could not start external http listener")
114+
return errors2.Wrap(err, "could not start http listener")
120115
}
121116
return nil
122117
}
123-
124-
// listenAndServeInternal on the internal address.
125-
// Note that all routes should be defined on internalMux before calling this.
126-
func (s *Server) listenAndServeInternal() error {
127-
server := createHTTPServer(s.internalAddress, s.internalMux, s.log)
128-
129-
s.log.Printf("Listening for internal HTTP on http://%v\n", s.internalAddress)
130-
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
131-
return errors2.Wrap(err, "could not start internal http listener")
132-
}
133-
return nil
134-
}
135-
136-
// createHTTPServer with aggressive timeouts.
137-
func createHTTPServer(addr string, handler http.Handler, log *log.Logger) http.Server {
138-
return http.Server{
139-
Addr: addr,
140-
Handler: handler,
141-
ReadTimeout: 5 * time.Second,
142-
ReadHeaderTimeout: 5 * time.Second,
143-
WriteTimeout: 5 * time.Second,
144-
IdleTimeout: 5 * time.Second,
145-
ErrorLog: log,
146-
}
147-
}

‎server/server_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88

99
func TestNewServer(t *testing.T) {
1010
t.Run("returns new server with http addresses set", func(t *testing.T) {
11-
s := New(Options{ExternalPort: 123, InternalPort: 234, InternalHost: "localhost"})
11+
s := New(Options{Port: 123})
1212
require.NotNil(t, s)
13-
require.Equal(t, ":123", s.externalAddress)
14-
require.Equal(t, "localhost:234", s.internalAddress)
13+
require.Equal(t, ":123", s.address)
1514
})
1615
}

0 commit comments

Comments
 (0)
This repository has been archived.