github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/options.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/vnforks/kid/v5/mlog"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/vnforks/kid/v5/config"
    11  	"github.com/vnforks/kid/v5/store"
    12  )
    13  
    14  type Option func(s *Server) error
    15  
    16  // By default, the app will use the store specified by the configuration. This allows you to
    17  // construct an app with a different store.
    18  //
    19  // The override parameter must be either a store.Store or func(App) store.Store().
    20  func StoreOverride(override interface{}) Option {
    21  	return func(s *Server) error {
    22  		switch o := override.(type) {
    23  		case store.Store:
    24  			s.newStore = func() store.Store {
    25  				return o
    26  			}
    27  			return nil
    28  
    29  		case func(*Server) store.Store:
    30  			s.newStore = func() store.Store {
    31  				return o(s)
    32  			}
    33  			return nil
    34  
    35  		default:
    36  			return errors.New("invalid StoreOverride")
    37  		}
    38  	}
    39  }
    40  
    41  // Config applies the given config dsn, whether a path to config.json or a database connection string.
    42  func Config(dsn string, watch bool) Option {
    43  	return func(s *Server) error {
    44  		configStore, err := config.NewStore(dsn, watch)
    45  		if err != nil {
    46  			return errors.Wrap(err, "failed to apply Config option")
    47  		}
    48  
    49  		s.configStore = configStore
    50  		return nil
    51  	}
    52  }
    53  
    54  // ConfigStore applies the given config store, typically to replace the traditional sources with a memory store for testing.
    55  func ConfigStore(configStore config.Store) Option {
    56  	return func(s *Server) error {
    57  		s.configStore = configStore
    58  
    59  		return nil
    60  	}
    61  }
    62  
    63  func RunJobs(s *Server) error {
    64  	s.runjobs = true
    65  
    66  	return nil
    67  }
    68  
    69  func JoinCluster(s *Server) error {
    70  	s.joinCluster = true
    71  
    72  	return nil
    73  }
    74  
    75  func StartMetrics(s *Server) error {
    76  	s.startMetrics = true
    77  
    78  	return nil
    79  }
    80  
    81  func StartSearchEngine(s *Server) error {
    82  	s.startSearchEngine = true
    83  
    84  	return nil
    85  }
    86  
    87  func SetLogger(logger *mlog.Logger) Option {
    88  	return func(s *Server) error {
    89  		s.Log = logger
    90  		return nil
    91  	}
    92  }
    93  
    94  type AppOption func(a *App)
    95  type AppOptionCreator func() []AppOption
    96  
    97  func ServerConnector(s *Server) AppOption {
    98  	return func(a *App) {
    99  		a.srv = s
   100  		a.log = s.Log
   101  		a.notificationsLog = s.NotificationsLog
   102  
   103  		a.accountMigration = s.AccountMigration
   104  		a.cluster = s.Cluster
   105  		a.compliance = s.Compliance
   106  		a.dataRetention = s.DataRetention
   107  		a.searchEngine = s.SearchEngine
   108  		a.ldap = s.Ldap
   109  		a.messageExport = s.MessageExport
   110  		a.metrics = s.Metrics
   111  		a.notification = s.Notification
   112  		a.saml = s.Saml
   113  
   114  		a.httpService = s.HTTPService
   115  		a.imageProxy = s.ImageProxy
   116  		a.timezones = s.timezones
   117  	}
   118  }