github.com/go-kivik/kivik/v4@v4.3.2/x/server/config/config.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 // Package config manages server configuration. 14 package config 15 16 import ( 17 "context" 18 "net/http" 19 20 internal "github.com/go-kivik/kivik/v4/int/errors" 21 ) 22 23 // Config provides access to read server configuration. Configuration 24 // backends that allow modifying configuration will also implement 25 // [ConfigWriter]. 26 type Config interface { 27 All(context.Context) (map[string]map[string]string, error) 28 Section(context.Context, string) (map[string]string, error) 29 Key(context.Context, string, string) (string, error) 30 Reload(context.Context) error 31 } 32 33 // Writer allows setting server configuration. 34 type Writer interface { 35 // SetKey sets a new config value, and returns the old value. 36 SetKey(context.Context, string, string, string) (string, error) 37 Delete(context.Context, string, string) (string, error) 38 } 39 40 type defaultConfig struct { 41 conf map[string]map[string]string 42 } 43 44 var ( 45 _ Config = (*defaultConfig)(nil) 46 _ Writer = (*defaultConfig)(nil) 47 ) 48 49 // Default returns a Config implementation which returns default values for all 50 // configuration options, and preserves changed settings only until restart. 51 func Default() Config { 52 return &defaultConfig{ 53 conf: map[string]map[string]string{ 54 "couchdb": { 55 "users_db_suffix": "_users", 56 }, 57 }, 58 } 59 } 60 61 // Map returns a Config implementation which returns the provided configuration. 62 func Map(conf map[string]map[string]string) Config { 63 return &defaultConfig{conf: conf} 64 } 65 66 func (c *defaultConfig) All(context.Context) (map[string]map[string]string, error) { 67 return c.conf, nil 68 } 69 70 func (c *defaultConfig) Section(_ context.Context, section string) (map[string]string, error) { 71 return c.conf[section], nil 72 } 73 74 func (c *defaultConfig) Key(_ context.Context, section, key string) (string, error) { 75 if v, ok := c.conf[section][key]; ok { 76 return v, nil 77 } 78 return "", &internal.Error{Status: http.StatusNotFound, Message: "unknown_config_value"} 79 } 80 81 func (c *defaultConfig) Reload(context.Context) error { 82 return nil 83 } 84 85 func (c *defaultConfig) SetKey(_ context.Context, section, key string, value string) (string, error) { 86 s, ok := c.conf[section] 87 if !ok { 88 s = map[string]string{} 89 c.conf[section] = s 90 } 91 old := s[key] 92 s[key] = value 93 return old, nil 94 } 95 96 func (c *defaultConfig) Delete(_ context.Context, section, key string) (string, error) { 97 if v, ok := c.conf[section][key]; ok { 98 delete(c.conf[section], key) 99 return v, nil 100 } 101 return "", &internal.Error{Status: http.StatusNotFound, Message: "unknown_config_value"} 102 }