Пример #1
0
func main() {
	start := false
	logDir := flag.String("log", "./logs", "path to log directory")
	confPath := flag.String("config", "./config.json", "path to cofig file")
	// docker := flag.Bool("docker", false, "for docker")
	flag.Parse()

	// Create log directory
	if err := os.Mkdir(*logDir, 0777); err != nil && os.IsNotExist(err) {
		log.Fatal(err)
	}
	// Write log in file:
	// Change file every 24 hours
	go func() {
		Log = llog.New()
		t := time.Now()
		for {
			// Day_Month_Year
			name := fmt.Sprintf("%v.%v.%v", t.Month(), t.Day(), t.Year())
			h, f, err := MyFileHandler(path.Join(*logDir, name), llog.LogfmtFormat())
			if err != nil {
				log.Println(err)
				return
			}
			Log.SetHandler(llog.SyncHandler(h))
			if !start {
				start = true
			}
			t = <-time.After(24 * time.Second)
			Log.SetHandler(llog.DiscardHandler())
			f.Close()
		}
	}()
	// Wait for log setup
	for {
		if start {
			break
		}
		time.Sleep(time.Second * 5)
	}

	file, err := ioutil.ReadFile(*confPath)
	if err != nil {
		fmt.Println("No config.json provided, using default values!")
		os.Exit(1)
	}

	var conf proxy.Config
	if err := json.Unmarshal(file, &conf); err != nil {
		os.Exit(1)
	}
	// creates new server instance using config.json
	s := proxy.NewServer(Log, conf)

	if err := s.Listen(); err != nil {
		Log.Error("Listen", "error", err)
		os.Exit(1)
	}

}
Пример #2
0
func TestMain(m *testing.M) {
	Log := llog.New()
	h, _ := llog.FileHandler("./test.txt", llog.LogfmtFormat())
	Log.SetHandler(h)
	server = s.NewServer(Log, ":81234")
	n, err := nats.Connect("nats://localhost:4222")
	if err != nil {
		llog.Error("Can't connect to NATS", "error", err)
		return
	}
	nc, _ := nats.NewEncodedConn(n, nats.JSON_ENCODER)
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		nc.Close()
		llog.Error("Can't listen on provided port", "error", err)
		return
	}
	server.BC = l
	server.NC = nc
	server.Loc, _ = time.LoadLocation(s.BraceletLocation)
	log.Println("Connections established")
	os.Exit(m.Run())
}