func initConfig() { // Config parsing. if flag.NFlag() == 0 || (flag.NFlag() == 1 && *debug) { // Case ./program [-d] log.Warnf("no config specified, using default..") } else { // Case ./program -c filename err := cfg.UpdateWithYamlFile(*fileName) if err != nil { log.Fatalf("failed to load %s, %s", *fileName, err) } } // Config validation. err := cfg.Validate() if err != nil { log.Fatalf("config: %s", err) } }
func initDB() { // Rely on config. if cfg == nil { panic(errors.New("db require config")) } path := cfg.Storage.Path opts := &storage.Options{ Period: cfg.Period, Expiration: cfg.Expiration, } var err error db, err = storage.Open(path, opts) if err != nil { log.Fatalf("failed to open %s: %v", path, err) } }
// Start the tcp server. func (d *Detector) Start() { addr := fmt.Sprintf("0.0.0.0:%d", d.cfg.Detector.Port) ln, err := net.Listen("tcp", addr) if err != nil { log.Fatalf("listen: %v", err) } log.Infof("detector is listening on %s", addr) go d.startIdleTracking() for { conn, err := ln.Accept() if err != nil { log.Errorf("cannot accept conn: %v, skipping..", err) continue } go d.handle(conn) } }