@@ -23,29 +23,25 @@ import (
23
23
24
24
// Server takes requests and responds. 😎
25
25
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
36
34
}
37
35
38
36
// Options for New.
39
37
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
49
45
}
50
46
51
47
// New creates a new Server.
@@ -55,20 +51,18 @@ func New(options Options) *Server {
55
51
logger = log .New (ioutil .Discard , "" , 0 )
56
52
}
57
53
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 (),
68
62
}
69
63
}
70
64
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.
72
66
func (s * Server ) Start () error {
73
67
74
68
if err := s .Storer .Connect (); err != nil {
@@ -78,18 +72,11 @@ func (s *Server) Start() error {
78
72
s .sm .Store = postgresstore .NewWithCleanupInterval (s .Storer .DB .DB , 0 )
79
73
s .sm .Cookie .Secure = true
80
74
81
- s .setupInternalRoutes ()
82
- s .setupExternalRoutes ()
75
+ s .setupRoutes ()
83
76
84
77
errs := make (chan error )
85
78
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 {
93
80
errs <- err
94
81
}
95
82
}()
@@ -102,46 +89,29 @@ func (s *Server) Start() error {
102
89
return errors2 .Wrap (err , "could not listen and serve" )
103
90
}
104
91
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
+ }
109
104
110
105
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 )
112
107
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" )
114
109
}
115
110
return nil
116
111
}
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 )
118
113
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" )
120
115
}
121
116
return nil
122
117
}
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
- }
0 commit comments