@@ -28,12 +28,12 @@ const resultsPerPage = 50
28
28
29
29
// HTTPServer is the HTTP server for the Charm Cloud backend.
30
30
type HTTPServer struct {
31
- db db.DB
32
- fstore storage.FileStore
33
- cfg * Config
34
- handler http.Handler
35
- server * http.Server
36
- health * http. Server
31
+ db db.DB
32
+ fstore storage.FileStore
33
+ cfg * Config
34
+ server * http.Server
35
+ health * http.Server
36
+ httpScheme string
37
37
}
38
38
39
39
type providerJSON struct {
@@ -59,13 +59,24 @@ func NewHTTPServer(cfg *Config) (*HTTPServer, error) {
59
59
}
60
60
mux := goji .NewMux ()
61
61
s := & HTTPServer {
62
- cfg : cfg ,
63
- handler : mux ,
64
- health : health ,
62
+ cfg : cfg ,
63
+ health : health ,
64
+ httpScheme : "http" ,
65
65
}
66
+ s .server = & http.Server {
67
+ Addr : fmt .Sprintf ("%s:%d" , s .cfg .BindAddr , s .cfg .HTTPPort ),
68
+ Handler : mux ,
69
+ ErrorLog : s .cfg .errorLog ,
70
+ }
71
+ if cfg .UseTLS {
72
+ s .httpScheme = "https"
73
+ s .health .TLSConfig = s .cfg .tlsConfig
74
+ s .server .TLSConfig = s .cfg .tlsConfig
75
+ }
76
+
66
77
jwtMiddleware , err := JWTMiddleware (
67
78
cfg .jwtKeyPair .JWK .Public (),
68
- cfg .httpURL (),
79
+ cfg .httpURL (). String () ,
69
80
[]string {"charm" },
70
81
)
71
82
if err != nil {
@@ -97,24 +108,19 @@ func NewHTTPServer(cfg *Config) (*HTTPServer, error) {
97
108
98
109
// Start start the HTTP and health servers on the ports specified in the Config.
99
110
func (s * HTTPServer ) Start () {
100
- useTLS := s .cfg .httpScheme == "https"
101
- listenAddr := fmt .Sprintf ("%s:%d" , s .cfg .BindAddr , s .cfg .HTTPPort )
102
- s .server = & http.Server {
103
- Addr : listenAddr ,
104
- Handler : s .handler ,
105
- TLSConfig : s .cfg .tlsConfig ,
106
- ErrorLog : s .cfg .errorLog ,
107
- }
108
-
111
+ scheme := strings .ToUpper (s .httpScheme )
109
112
go func () {
110
- log .Printf ("Starting HTTP health server on: %s" , s .health .Addr )
111
- if err := s .health .ListenAndServe (); err != nil {
112
- log .Fatalf ("http health endpoint server exited with error: %s" , err )
113
+ log .Printf ("Starting %s health server on: %s" , scheme , s .health .Addr )
114
+ if s .cfg .UseTLS {
115
+ log .Fatalf ("http health endpoint server exited with error: %s" ,
116
+ s .health .ListenAndServeTLS (s .cfg .TLSCertFile , s .cfg .TLSKeyFile ))
117
+ } else {
118
+ log .Fatalf ("http health endpoint server exited with error: %s" , s .health .ListenAndServe ())
113
119
}
114
120
}()
115
121
116
- log .Printf ("Starting %s server on: %s" , strings . ToUpper ( s . cfg . httpScheme ), listenAddr )
117
- if useTLS {
122
+ log .Printf ("Starting %s server on: %s" , scheme , s . server . Addr )
123
+ if s . cfg . UseTLS {
118
124
log .Fatalf ("Server crashed: %s" , s .server .ListenAndServeTLS (s .cfg .TLSCertFile , s .cfg .TLSKeyFile ))
119
125
} else {
120
126
log .Fatalf ("Server crashed: %s" , s .server .ListenAndServe ())
@@ -123,8 +129,9 @@ func (s *HTTPServer) Start() {
123
129
124
130
// Shutdown gracefully shut down the HTTP and health servers.
125
131
func (s * HTTPServer ) Shutdown (ctx context.Context ) error {
126
- log .Printf ("Stopping %s server on %s" , strings .ToUpper (s .cfg .httpScheme ), s .server .Addr )
127
- log .Printf ("Stopping HTTP health server on %s" , s .health .Addr )
132
+ scheme := strings .ToUpper (s .httpScheme )
133
+ log .Printf ("Stopping %s server on %s" , scheme , s .server .Addr )
134
+ log .Printf ("Stopping %s health server on %s" , scheme , s .health .Addr )
128
135
if err := s .health .Shutdown (ctx ); err != nil {
129
136
return err
130
137
}
0 commit comments