2
Hockeypuck - OpenPGP key server
3
Copyright (C) 2012-2014 Casey Marshall
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Affero General Public License as published by
7
the Free Software Foundation, version 3.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Affero General Public License for more details.
14
You should have received a copy of the GNU Affero General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
// Package hockeypuck provides common configuration, logging and
19
// static content for the keyserver.
28
"github.com/pelletier/go-toml"
33
// Config returns the global Settings for an application built with Hockeypuck.
34
func Config() *Settings {
38
// Settings stores configuration options for Hockeypuck.
39
type Settings struct {
43
// GetString returns the string value for the configuration key if set,
44
// otherwise the empty string.
45
func (s *Settings) GetString(key string) string {
46
return s.GetStringDefault(key, "")
49
// GetStringDefault returns the string value for the configuration key if set,
50
// otherwise the default value.
51
func (s *Settings) GetStringDefault(key string, defaultValue string) string {
52
if s, is := s.Get(key).(string); is {
58
// MustGetInt returns the int value for the configuration key if set and valid,
60
func (s *Settings) MustGetInt(key string) int {
61
if v, err := s.getInt(key); err == nil {
68
// GetIntDefault returns the int value for the configuration key if set and valid,
69
// otherwise the default value.
70
func (s *Settings) GetIntDefault(key string, defaultValue int) int {
71
if v, err := s.getInt(key); err == nil {
78
func (s *Settings) getInt(key string) (int, error) {
79
switch v := s.Get(key).(type) {
85
if i, err := strconv.Atoi(fmt.Sprintf("%v", v)); err != nil {
95
// GetBool returns the boolean value for the configuration key if set,
97
func (s *Settings) GetBool(key string) bool {
99
switch v := s.Get(key).(type) {
105
b, err := strconv.ParseBool(v)
106
result = err == nil && b
114
// GetStrings returns a []string slice for the configuration key if set,
115
// otherwise an empty slice.
116
func (s *Settings) GetStrings(key string) (value []string) {
117
if strs, is := s.Get(key).([]interface{}); is {
118
for _, v := range strs {
119
if str, is := v.(string); is {
120
value = append(value, str)
127
// SetConfig sets the global configuration to the TOML-formatted string contents.
128
func SetConfig(contents string) (err error) {
129
var tree *toml.TomlTree
130
if tree, err = toml.Load(contents); err != nil {
133
config = &Settings{tree}
137
// LoadConfig sets the global configuration to the TOML-formatted reader contents.
138
func LoadConfig(r io.Reader) (err error) {
139
buf := bytes.NewBuffer(nil)
140
_, err = io.Copy(buf, r)
144
var tree *toml.TomlTree
145
if tree, err = toml.Load(buf.String()); err != nil {
148
config = &Settings{tree}
152
// LoadConfigFile sets the global configuration to the contents from the TOML file path.
153
func LoadConfigFile(path string) (err error) {
154
var tree *toml.TomlTree
155
if tree, err = toml.LoadFile(path); err != nil {
158
config = &Settings{tree}