github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/config/store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package config
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/crspeller/mattermost-server/model"
    10  )
    11  
    12  // Listener is a callback function invoked when the configuration changes.
    13  type Listener func(oldConfig *model.Config, newConfig *model.Config)
    14  
    15  // Store abstracts the act of getting and setting the configuration.
    16  type Store interface {
    17  	// Get fetches the current, cached configuration.
    18  	Get() *model.Config
    19  
    20  	// GetEnvironmentOverrides fetches the configuration fields overridden by environment variables.
    21  	GetEnvironmentOverrides() map[string]interface{}
    22  
    23  	// Set replaces the current configuration in its entirety and updates the backing store.
    24  	Set(*model.Config) (*model.Config, error)
    25  
    26  	// Load updates the current configuration from the backing store, possibly initializing.
    27  	Load() (err error)
    28  
    29  	// AddListener adds a callback function to invoke when the configuration is modified.
    30  	AddListener(listener Listener) string
    31  
    32  	// RemoveListener removes a callback function using an id returned from AddListener.
    33  	RemoveListener(id string)
    34  
    35  	// GetFile fetches the contents of a previously persisted configuration file.
    36  	// If no such file exists, an empty byte array will be returned without error.
    37  	GetFile(name string) ([]byte, error)
    38  
    39  	// SetFile sets or replaces the contents of a configuration file.
    40  	SetFile(name string, data []byte) error
    41  
    42  	// HasFile returns true if the given file was previously persisted.
    43  	HasFile(name string) (bool, error)
    44  
    45  	// RemoveFile removes a previously persisted configuration file.
    46  	RemoveFile(name string) error
    47  
    48  	// String describes the backing store for the config.
    49  	String() string
    50  
    51  	// Close cleans up resources associated with the store.
    52  	Close() error
    53  }
    54  
    55  // NewStore creates a database or file store given a data source name by which to connect.
    56  func NewStore(dsn string, watch bool) (Store, error) {
    57  	if strings.HasPrefix(dsn, "mysql://") || strings.HasPrefix(dsn, "postgres://") {
    58  		return NewDatabaseStore(dsn)
    59  	}
    60  
    61  	return NewFileStore(dsn, watch)
    62  }