all: Add configurable defaults (fixes #4224, fixes #6086) (#7131)

This commit is contained in:
Simon Frei
2021-02-04 21:10:41 +01:00
committed by GitHub
parent 31119ed61a
commit ffc14a77c6
41 changed files with 1708 additions and 1046 deletions
+2
View File
@@ -302,6 +302,8 @@ func (s *service) Serve(ctx context.Context) error {
configBuilder.registerDevices("/rest/config/devices")
configBuilder.registerFolder("/rest/config/folders/:id")
configBuilder.registerDevice("/rest/config/devices/:id")
configBuilder.registerDefaultFolder("/rest/config/defaults/folder")
configBuilder.registerDefaultDevice("/rest/config/defaults/device")
configBuilder.registerOptions("/rest/config/options")
configBuilder.registerLDAP("/rest/config/ldap")
configBuilder.registerGUI("/rest/config/gui")
+45 -9
View File
@@ -73,7 +73,7 @@ func (c *configMuxBuilder) registerFolders(path string) {
})
c.HandlerFunc(http.MethodPost, path, func(w http.ResponseWriter, r *http.Request) {
c.adjustFolder(w, r, config.FolderConfiguration{})
c.adjustFolder(w, r, config.FolderConfiguration{}, false)
})
}
@@ -126,7 +126,7 @@ func (c *configMuxBuilder) registerFolder(path string) {
})
c.Handle(http.MethodPut, path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
c.adjustFolder(w, r, config.FolderConfiguration{})
c.adjustFolder(w, r, config.FolderConfiguration{}, false)
})
c.Handle(http.MethodPatch, path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -135,7 +135,7 @@ func (c *configMuxBuilder) registerFolder(path string) {
http.Error(w, "No folder with given ID", http.StatusNotFound)
return
}
c.adjustFolder(w, r, folder)
c.adjustFolder(w, r, folder, false)
})
c.Handle(http.MethodDelete, path, func(w http.ResponseWriter, _ *http.Request, p httprouter.Params) {
@@ -170,12 +170,12 @@ func (c *configMuxBuilder) registerDevice(path string) {
})
c.Handle(http.MethodPut, path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
c.adjustDevice(w, r, config.DeviceConfiguration{})
c.adjustDevice(w, r, config.DeviceConfiguration{}, false)
})
c.Handle(http.MethodPatch, path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if device, ok := deviceFromParams(w, p); ok {
c.adjustDevice(w, r, device)
c.adjustDevice(w, r, device, false)
}
})
@@ -190,6 +190,34 @@ func (c *configMuxBuilder) registerDevice(path string) {
})
}
func (c *configMuxBuilder) registerDefaultFolder(path string) {
c.HandlerFunc(http.MethodGet, path, func(w http.ResponseWriter, _ *http.Request) {
sendJSON(w, c.cfg.DefaultFolder())
})
c.HandlerFunc(http.MethodPut, path, func(w http.ResponseWriter, r *http.Request) {
c.adjustFolder(w, r, config.FolderConfiguration{}, true)
})
c.HandlerFunc(http.MethodPatch, path, func(w http.ResponseWriter, r *http.Request) {
c.adjustFolder(w, r, c.cfg.DefaultFolder(), true)
})
}
func (c *configMuxBuilder) registerDefaultDevice(path string) {
c.HandlerFunc(http.MethodGet, path, func(w http.ResponseWriter, _ *http.Request) {
sendJSON(w, c.cfg.DefaultDevice())
})
c.HandlerFunc(http.MethodPut, path, func(w http.ResponseWriter, r *http.Request) {
c.adjustDevice(w, r, config.DeviceConfiguration{}, true)
})
c.HandlerFunc(http.MethodPatch, path, func(w http.ResponseWriter, r *http.Request) {
c.adjustDevice(w, r, c.cfg.DefaultDevice(), true)
})
}
func (c *configMuxBuilder) registerOptions(path string) {
c.HandlerFunc(http.MethodGet, path, func(w http.ResponseWriter, _ *http.Request) {
sendJSON(w, c.cfg.Options())
@@ -260,13 +288,17 @@ func (c *configMuxBuilder) adjustConfig(w http.ResponseWriter, r *http.Request)
c.finish(w, waiter)
}
func (c *configMuxBuilder) adjustFolder(w http.ResponseWriter, r *http.Request, folder config.FolderConfiguration) {
func (c *configMuxBuilder) adjustFolder(w http.ResponseWriter, r *http.Request, folder config.FolderConfiguration, defaults bool) {
if err := unmarshalTo(r.Body, &folder); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
waiter, err := c.cfg.Modify(func(cfg *config.Configuration) {
cfg.SetFolder(folder)
if defaults {
cfg.Defaults.Folder = folder
} else {
cfg.SetFolder(folder)
}
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -275,13 +307,17 @@ func (c *configMuxBuilder) adjustFolder(w http.ResponseWriter, r *http.Request,
c.finish(w, waiter)
}
func (c *configMuxBuilder) adjustDevice(w http.ResponseWriter, r *http.Request, device config.DeviceConfiguration) {
func (c *configMuxBuilder) adjustDevice(w http.ResponseWriter, r *http.Request, device config.DeviceConfiguration, defaults bool) {
if err := unmarshalTo(r.Body, &device); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
waiter, err := c.cfg.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(device)
if defaults {
cfg.Defaults.Device = device
} else {
cfg.SetDevice(device)
}
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
+16 -1
View File
@@ -91,7 +91,6 @@ func (c *mockedConfig) RemoveFolder(id string) (config.Waiter, error) {
func (c *mockedConfig) FolderPasswords(device protocol.DeviceID) map[string]string {
return nil
}
func (c *mockedConfig) Device(id protocol.DeviceID) (config.DeviceConfiguration, bool) {
return config.DeviceConfiguration{}, false
}
@@ -112,6 +111,22 @@ func (c *mockedConfig) IgnoredFolder(device protocol.DeviceID, folder string) bo
return false
}
func (c *mockedConfig) DefaultFolder() config.FolderConfiguration {
return config.FolderConfiguration{}
}
func (c *mockedConfig) SetDefaultFolder(config.FolderConfiguration) (config.Waiter, error) {
return noopWaiter{}, nil
}
func (c *mockedConfig) DefaultDevice() config.DeviceConfiguration {
return config.DeviceConfiguration{}
}
func (c *mockedConfig) SetDefaultDevice(config.DeviceConfiguration) (config.Waiter, error) {
return noopWaiter{}, nil
}
func (c *mockedConfig) GlobalDiscoveryServers() []string {
return nil
}