apiService should not reference global variable 'locations' (hinders testing)

This commit is contained in:
Jakob Borg 2016-03-13 11:03:00 +01:00
parent 032365d57c
commit ba5231dc89
2 changed files with 8 additions and 4 deletions

View File

@ -51,6 +51,8 @@ var (
type apiService struct { type apiService struct {
id protocol.DeviceID id protocol.DeviceID
cfg *config.Wrapper cfg *config.Wrapper
httpsCertFile string
httpsKeyFile string
assetDir string assetDir string
themes []string themes []string
model *model.Model model *model.Model
@ -70,10 +72,12 @@ type apiService struct {
systemLog *logger.Recorder systemLog *logger.Recorder
} }
func newAPIService(id protocol.DeviceID, cfg *config.Wrapper, assetDir string, m *model.Model, eventSub *events.BufferedSubscription, discoverer *discover.CachingMux, relayService *relay.Service, errors, systemLog *logger.Recorder) (*apiService, error) { func newAPIService(id protocol.DeviceID, cfg *config.Wrapper, httpsCertFile, httpsKeyFile, assetDir string, m *model.Model, eventSub *events.BufferedSubscription, discoverer *discover.CachingMux, relayService *relay.Service, errors, systemLog *logger.Recorder) (*apiService, error) {
service := &apiService{ service := &apiService{
id: id, id: id,
cfg: cfg, cfg: cfg,
httpsCertFile: httpsCertFile,
httpsKeyFile: httpsKeyFile,
assetDir: assetDir, assetDir: assetDir,
model: m, model: m,
eventSub: eventSub, eventSub: eventSub,
@ -112,7 +116,7 @@ func newAPIService(id protocol.DeviceID, cfg *config.Wrapper, assetDir string, m
} }
func (s *apiService) getListener(guiCfg config.GUIConfiguration) (net.Listener, error) { func (s *apiService) getListener(guiCfg config.GUIConfiguration) (net.Listener, error) {
cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile]) cert, err := tls.LoadX509KeyPair(s.httpsCertFile, s.httpsKeyFile)
if err != nil { if err != nil {
l.Infoln("Loading HTTPS certificate:", err) l.Infoln("Loading HTTPS certificate:", err)
l.Infoln("Creating new HTTPS certificate") l.Infoln("Creating new HTTPS certificate")
@ -125,7 +129,7 @@ func (s *apiService) getListener(guiCfg config.GUIConfiguration) (net.Listener,
name = tlsDefaultCommonName name = tlsDefaultCommonName
} }
cert, err = tlsutil.NewCertificate(locations[locHTTPSCertFile], locations[locHTTPSKeyFile], name, httpsRSABits) cert, err = tlsutil.NewCertificate(s.httpsCertFile, s.httpsKeyFile, name, httpsRSABits)
} }
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -983,7 +983,7 @@ func setupGUI(mainService *suture.Supervisor, cfg *config.Wrapper, m *model.Mode
l.Warnln("Insecure admin access is enabled.") l.Warnln("Insecure admin access is enabled.")
} }
api, err := newAPIService(myID, cfg, runtimeOptions.assetDir, m, apiSub, discoverer, relayService, errors, systemLog) api, err := newAPIService(myID, cfg, locations[locHTTPSCertFile], locations[locHTTPSKeyFile], runtimeOptions.assetDir, m, apiSub, discoverer, relayService, errors, systemLog)
if err != nil { if err != nil {
l.Fatalln("Cannot start GUI:", err) l.Fatalln("Cannot start GUI:", err)
} }