github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/app/options.go (about)

     1  // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/xzl8028/xenia-server/mlog"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/xzl8028/xenia-server/config"
    11  	"github.com/xzl8028/xenia-server/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 StartElasticsearch(s *Server) error {
    82  	s.startElasticsearch = 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  
   101  		a.Log = s.Log
   102  		a.NotificationsLog = s.NotificationsLog
   103  
   104  		a.AccountMigration = s.AccountMigration
   105  		a.Cluster = s.Cluster
   106  		a.Compliance = s.Compliance
   107  		a.DataRetention = s.DataRetention
   108  		a.Elasticsearch = s.Elasticsearch
   109  		a.Ldap = s.Ldap
   110  		a.MessageExport = s.MessageExport
   111  		a.Metrics = s.Metrics
   112  		a.Saml = s.Saml
   113  
   114  		a.HTTPService = s.HTTPService
   115  		a.ImageProxy = s.ImageProxy
   116  		a.Timezones = s.timezones
   117  	}
   118  }