feat(http): use tls keys when provided

Ayman Bagabas created

Change summary

server/config/config.go | 6 ++++++
server/config/file.go   | 6 ++++++
server/http.go          | 3 +++
3 files changed, 15 insertions(+)

Detailed changes

server/config/config.go 🔗

@@ -51,6 +51,12 @@ type HTTPConfig struct {
 	// ListenAddr is the address on which the HTTP server will listen.
 	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 
+	// TLSKeyPath is the path to the TLS private key.
+	TLSKeyPath string `env:"TLS_KEY_PATH" yaml:"tls_key_path"`
+
+	// TLSCertPath is the path to the TLS certificate.
+	TLSCertPath string `env:"TLS_CERT_PATH" yaml:"tls_cert_path"`
+
 	// PublicURL is the public URL of the HTTP server.
 	PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
 }

server/config/file.go 🔗

@@ -54,6 +54,12 @@ http:
   # The address on which the HTTP server will listen.
   listen_addr: "{{ .HTTP.ListenAddr }}"
 
+  # The relative path to the TLS private key.
+  tls_key_path: "{{ .HTTP.TLSKeyPath }}"
+
+  # The relative path to the TLS certificate.
+  tls_cert_path: "{{ .HTTP.TLSCertPath }}"
+
   # The public URL of the HTTP server.
   # This is the address will be used to clone repositories.
   public_url: "{{ .HTTP.PublicURL }}"

server/http.go 🔗

@@ -105,6 +105,9 @@ func (s *HTTPServer) Close() error {
 
 // ListenAndServe starts the HTTP server.
 func (s *HTTPServer) ListenAndServe() error {
+	if s.cfg.HTTP.TLSKeyPath != "" && s.cfg.HTTP.TLSCertPath != "" {
+		return s.server.ListenAndServeTLS(s.cfg.HTTP.TLSCertPath, s.cfg.HTTP.TLSKeyPath)
+	}
 	return s.server.ListenAndServe()
 }