@@ -3,6 +3,8 @@ package configuration
33
44import (
55 "github.com/jinzhu/configor"
6+ "github.com/pushbits/server/internal/log"
7+ "github.com/pushbits/server/internal/pberrors"
68)
79
810// testMode indicates if the package is run in test mode
@@ -53,6 +55,8 @@ type Configuration struct {
5355 ListenAddress string `default:""`
5456 Port int `default:"8080"`
5557 TrustedProxies []string `default:"[]"`
58+ CertFile string `default:""`
59+ KeyFile string `default:""`
5660 }
5761 Database struct {
5862 Dialect string `default:"sqlite3"`
@@ -80,6 +84,21 @@ func configFiles() []string {
8084 return []string {"config.yml" }
8185}
8286
87+ func validateHTTPConfiguration (c * Configuration ) error {
88+ certAndKeyEmpty := (c .HTTP .CertFile == "" && c .HTTP .KeyFile == "" )
89+ certAndKeyPopulated := (c .HTTP .CertFile != "" && c .HTTP .KeyFile != "" )
90+
91+ if ! certAndKeyEmpty && ! certAndKeyPopulated {
92+ return pberrors .ErrConfigTLSFilesInconsistent
93+ }
94+
95+ return nil
96+ }
97+
98+ func validateConfiguration (c * Configuration ) error {
99+ return validateHTTPConfiguration (c )
100+ }
101+
83102// Get returns the configuration extracted from env variables or config file.
84103func Get () * Configuration {
85104 config := & Configuration {}
@@ -93,5 +112,9 @@ func Get() *Configuration {
93112 panic (err )
94113 }
95114
115+ if err := validateConfiguration (config ); err != nil {
116+ log .L .Fatal (err )
117+ }
118+
96119 return config
97120}
0 commit comments