func initRepository(repo_cfg *RepositoryConfig) (adaptor.Repository, error) { options := make([]func(*repository.Repository), 0) if repo_cfg.Seeds_list != nil { seeds := repo_cfg.Seeds_list options = append(options, repository.SetSeedsList(seeds...)) } if repo_cfg.Seeds_port != 0 { port := repo_cfg.Seeds_port if port > 0 && port < 65535 { options = append(options, repository.SetSeedsPort(port)) } } if repo_cfg.Backup_path != "" { path := repo_cfg.Backup_path options = append(options, repository.SetBackupPath(path)) } if repo_cfg.Backup_rate != 0 { rate := time.Duration(repo_cfg.Backup_rate) * time.Second if rate > time.Minute*15 && rate < time.Hour*24 { options = append(options, repository.SetBackupRate(rate)) } } if repo_cfg.Node_limit != 0 { limit := repo_cfg.Node_limit if limit > 1000 && limit < 1000000 { options = append(options, repository.SetNodeLimit(limit)) } } return repository.New(options...) }
func New() (*Supervisor, error) { // load configuration file cfg := &Config{} err := gcfg.ReadFileInto(cfg, "pbtc.cfg") if err != nil { return nil, err } // initialize struct with maps supervisor := &Supervisor{ logr: make(map[string]adaptor.Logger), repo: make(map[string]adaptor.Repository), tkr: make(map[string]adaptor.Tracker), svr: make(map[string]adaptor.Server), pro: make(map[string]adaptor.Processor), mgr: make(map[string]adaptor.Manager), } if len(cfg.Logger) == 0 { logr, err := logger.New() if err != nil { return nil, err } supervisor.logr[""] = logr } else if cfg.Logger[""] == nil { for _, logr_cfg := range cfg.Logger { logr, err := initLogger(logr_cfg) if err != nil { return nil, err } supervisor.logr[""] = logr break } } else { logr_cfg := cfg.Logger[""] logr, err := initLogger(logr_cfg) if err != nil { return nil, err } supervisor.logr[""] = logr } level, err := logger.ParseLevel(cfg.Supervisor.Log_level) if err != nil { level = logging.CRITICAL } supervisor.log = supervisor.logr[""].GetLog("supervisor") supervisor.logr[""].SetLevel("supervisor", level) supervisor.log.Info("[SUP] Init: started") supervisor.log.Info("[SUP] Init: default logger initialized") supervisor.log.Info("[SUP] Init: initializing modules") // initialize remaining modules for name, logr_cfg := range cfg.Logger { if name == "" { continue } logr, err := initLogger(logr_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: logger init failed (%v)", err) continue } supervisor.logr[name] = logr } for name, repo_cfg := range cfg.Repository { repo, err := initRepository(repo_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: repo init failed (%v)", err) continue } supervisor.repo[name] = repo } for name, tkr_cfg := range cfg.Tracker { tkr, err := initTracker(tkr_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: tracker init failed (%v)", err) continue } supervisor.tkr[name] = tkr } for name, svr_cfg := range cfg.Server { svr, err := initServer(svr_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: server init failed (%v)", err) continue } supervisor.svr[name] = svr } for name, pro_cfg := range cfg.Processor { pro, err := initProcessor(pro_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: proc init failed (%v)", err) continue } supervisor.pro[name] = pro } for name, mgr_cfg := range cfg.Manager { mgr, err := initManager(mgr_cfg) if err != nil { supervisor.log.Warning("[SUP] Init: manager init failed (%v)", err) continue } supervisor.mgr[name] = mgr } supervisor.log.Info("[SUP] Init: checking module cardinality") // check remaining modules for missing values if len(supervisor.repo) == 0 { supervisor.log.Warning("[SUP] Init: missing repository module") repo, err := repository.New() if err != nil { return nil, err } supervisor.repo["default"] = repo } if len(supervisor.tkr) == 0 { supervisor.log.Warning("[SUP] Init: missing tracker module") tkr, err := tracker.New() if err != nil { return nil, err } supervisor.tkr["default"] = tkr } if len(supervisor.mgr) == 0 { supervisor.log.Warning("[SUP] Init: missing manager module") mgr, err := manager.New() if err != nil { return nil, err } supervisor.mgr["default"] = mgr } if len(supervisor.svr) == 0 { supervisor.log.Notice("[SUP] Init: no server module") } if len(supervisor.pro) == 0 { supervisor.log.Notice("[SUP] Init: no processor module") } supervisor.log.Info("[SUP] Init: injecting logging capabilities") // inject logging dependencies for key, logr := range supervisor.logr { logr_cfg, ok := cfg.Logger[key] if !ok { continue } level, err := logger.ParseLevel(logr_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "logr___" + key logr.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } for key, repo := range supervisor.repo { repo_cfg, ok := cfg.Repository[key] if !ok { continue } logr, ok := supervisor.logr[repo_cfg.Logger] if !ok { logr = supervisor.logr[""] } level, err := logger.ParseLevel(repo_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "repo___" + key repo.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } for key, tkr := range supervisor.tkr { tkr_cfg, ok := cfg.Tracker[key] if !ok { continue } logr, ok := supervisor.logr[tkr_cfg.Logger] if !ok { logr = supervisor.logr[""] } level, err := logger.ParseLevel(tkr_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "tkr___" + key tkr.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } for key, svr := range supervisor.svr { svr_cfg, ok := cfg.Server[key] if !ok { continue } logr, ok := supervisor.logr[svr_cfg.Logger] if !ok { logr = supervisor.logr[""] } level, err := logger.ParseLevel(svr_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "svr___" + key svr.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } for key, pro := range supervisor.pro { pro_cfg, ok := cfg.Processor[key] if !ok { continue } logr, ok := supervisor.logr[pro_cfg.Logger] if !ok { logr = supervisor.logr[""] } level, err := logger.ParseLevel(pro_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "pro___" + key pro.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } for key, mgr := range supervisor.mgr { mgr_cfg, ok := cfg.Manager[key] if !ok { continue } logr, ok := supervisor.logr[mgr_cfg.Logger] if !ok { logr = supervisor.logr[""] } level, err := logger.ParseLevel(mgr_cfg.Log_level) if err != nil { level = logging.CRITICAL } log := "mgr___" + key mgr.SetLog(logr.GetLog(log)) logr.SetLevel(log, level) } supervisor.log.Info("[SUP] Init: injecting module dependencies") // inject manager into server for key, svr := range supervisor.svr { svr_cfg, ok := cfg.Server[key] if !ok { continue } mgr, ok := supervisor.mgr[svr_cfg.Manager] if !ok { for _, def := range supervisor.mgr { mgr = def break } } svr.SetManager(mgr) } // inject repository into manager for key, mgr := range supervisor.mgr { mgr_cfg, ok := cfg.Manager[key] if !ok { continue } repo, ok := supervisor.repo[mgr_cfg.Repository] if !ok { for _, def := range supervisor.repo { repo = def break } } mgr.SetRepository(repo) } // inject tracker into manager for key, mgr := range supervisor.mgr { mgr_cfg, ok := cfg.Manager[key] if !ok { continue } tkr, ok := supervisor.tkr[mgr_cfg.Tracker] if !ok { for _, def := range supervisor.tkr { tkr = def break } } mgr.SetTracker(tkr) } // inject processors into managers for key, mgr := range supervisor.mgr { mgr_cfg, ok := cfg.Manager[key] if !ok { continue } for _, name := range mgr_cfg.Processor { pro, ok := supervisor.pro[name] if !ok { continue } mgr.AddProcessor(pro) } } // inject processors into processors for key, pro := range supervisor.pro { pro_cfg, ok := cfg.Processor[key] if !ok { continue } for _, name := range pro_cfg.Next { next, ok := supervisor.pro[name] if !ok { continue } pro.AddNext(next) } } supervisor.log.Info("[SUP] Init: completed") return supervisor, nil }