예제 #1
0
func main() {

	var (
		redisAddress string
		router       *mux.Router
		err          error
		port         int
	)

	flag.IntVar(&port, "port", 4242, "Local port to listen on")
	flag.StringVar(&redisAddress, "redis-address", "localhost:6379", "Redis server address (host:port)")
	flag.Parse()

	if redisConn, err = redis.NewRedis(redisAddress); err != nil {
		log.Fatalf("Unable to connect to Redis server at %v\n", redisAddress)
	}
	log.Printf("Connected to Redis server at %v\n", redisAddress)

	router = mux.NewRouter()
	router.HandleFunc("/entries/{category}", GetEntries).Methods("GET")
	router.HandleFunc("/add/{category}", AddEntry).Methods("POST")
	http.Handle("/", router)

	log.Printf("Listening on http://localhost:%v\n", port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
예제 #2
0
func NewContext() *Context {
	context := new(Context)
	cli := redis.NewRedis(setting.Redis.Host, setting.Redis.Password, setting.Redis.Db)
	context.redis = cli
	context.quit_chan = make(chan bool)
	return context
}
예제 #3
0
func run() int {
	host, _ := config.GetString("redis", "host")
	password, _ := config.GetString("redis", "password")
	db, _ := config.GetInt("redis", "db")

	events, _ := config.GetString("redis", "events")
	channel, _ := config.GetString("redis", "channel")

	cli := redis.NewRedis(host, password, db)
	err := cli.Connect()
	if err != nil {
		log.Print("connect to redis failed")
		return 1
	}

	_, err = cli.Exec("config", "set", "notify-keyspace-events", events)
	if err != nil {
		log.Printf("config redis failed:%v", err)
		return 1
	}

	_, err = cli.Exec("subscribe", channel)
	if err != nil {
		log.Printf("subscribe failed:%s", err)
		return 1
	}

	for {
		if quit {
			break
		}
		resp, err := cli.ReadResponse()
		if err != nil {
			log.Printf("read publish message failed:%v", err)
			return 2
		}
		if data, ok := resp.([]string); ok {
			if len(data) != 3 || data[0] != "message" {
				log.Printf("receive unexpected message, %v", data)
			} else {
				event := data[1]
				key := data[2]
				log.Printf("receive [%s], value[%s]", event, key)
			}
		} else {
			log.Printf("receive unexpected message, %v", resp)
		}
	}
	return 0
}
예제 #4
0
func NewMonitor() *Monitor {
	cli := redis.NewRedis(setting.Redis.Host, setting.Redis.Password, setting.Redis.Db)
	notification_config := "gE"
	event := fmt.Sprintf("__keyevent@%d__:%s", setting.Redis.Db, setting.Redis.Event)
	return &Monitor{cli, notification_config, event, 0, false, make(chan int)}
}
예제 #5
0
func GetRedisConnection() (cli *redis.Redis, err error) {
	cli = redis.NewRedis(setting.Redis.Host, setting.Redis.Password, setting.Redis.Db)
	err = cli.Connect()
	return
}
예제 #6
0
func NewStorer(db *Leveldb) *Storer {
	cli := redis.NewRedis(setting.Redis.Host, setting.Redis.Password, setting.Redis.Db)
	return &Storer{cli, db}
}
예제 #7
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("config file is missing.")
		os.Exit(1)
	}

	config, err := conf.ReadConfigFile(args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "read config file failed:%s", err)
		os.Exit(1)
	}

	logfile, _ := config.GetString("log", "file")
	fp, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Fprintf(os.Stderr, "open log file failed:%s", err)
		os.Exit(1)
	}
	defer fp.Close()
	log.SetOutput(io.MultiWriter(fp, os.Stderr))

	host, _ := config.GetString("redis", "host")
	password, _ := config.GetString("redis", "password")
	db, _ := config.GetInt("redis", "db")
	events, _ := config.GetString("redis", "events")
	channel, _ := config.GetString("redis", "channel")

	queue := make(chan string, 1024)

	cli1 := redis.NewRedis(host, password, db)
	m := NewMonitor(cli1, events, channel)

	dbname, err := config.GetString("leveldb", "dbname")
	if err != nil {
		log.Fatalf("get leveldb config failed:%v", err)
	}

	database := NewLeveldb()
	err = database.Open(dbname)
	if err != nil {
		log.Panicf("open db failed, err:%v", err)
	} else {
		log.Printf("open db succeed, dbname:%v", dbname)
	}

	defer database.Close()

	cli2 := redis.NewRedis(host, password, db)
	s := NewStorer(cli2, database)

	addr, _ := config.GetString("manager", "addr")
	c := NewCmdService(addr)

	cli3 := redis.NewRedis(host, password, db)
	context := &Context{database, cli3, m, s, c, make(chan os.Signal)}
	context.Register(c)

	signal.Notify(context.quit_chan, syscall.SIGINT, syscall.SIGTERM)

	go m.Start(queue)
	go s.Start(queue)
	go c.Start()

	log.Println("start succeed")
	log.Printf("catch signal %v, program will exit", <-context.quit_chan)
}
예제 #8
0
func Init() (err error) {
	defaultQ, err = redis.NewRedis("127.0.0.1:6379")
	return
}
예제 #9
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("config file is missing.")
		os.Exit(1)
	}

	var config Config
	content, err := ioutil.ReadFile(args[1])
	if err != nil {
		panic(err)
	}
	if err = json.Unmarshal([]byte(content), &config); err != nil {
		panic(err)
	}
	logfile := config.Log.File
	host := config.Redis.Host
	password := config.Redis.Password
	tdb, _ := strconv.ParseInt(config.Redis.Db, 0, 0)
	db := int(tdb)
	events := config.Redis.Events
	channel := config.Redis.Channel
	dbname := config.Leveldb.Dbname
	manager_addr := config.Manager.Addr
	zinc_addr := config.Zinc.Addr

	fp, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Fprintf(os.Stderr, "open log file failed:%s", err)
		os.Exit(1)
	}
	defer fp.Close()
	log.SetOutput(io.MultiWriter(fp, os.Stderr))

	queue := make(chan string, 1024)

	cli1 := redis.NewRedis(host, password, db)
	m := NewMonitor(cli1, events, channel)

	database := NewLeveldb()
	err = database.Open(dbname)
	if err != nil {
		log.Panicf("open db failed, err:%v", err)
	} else {
		log.Printf("open db succeed, dbname:%v", dbname)
	}
	defer database.Close()

	cli2 := redis.NewRedis(host, password, db)
	s := NewStorer(cli2, database)

	c := NewCmdService(manager_addr)

	cli3 := redis.NewRedis(host, password, db)
	context := &Context{database, cli3, m, s, c, make(chan os.Signal)}
	context.Register(c)

	zinc_agent := NewZincAgent(zinc_addr, database)

	signal.Notify(context.quit_chan, syscall.SIGINT, syscall.SIGTERM)

	go m.Start(queue)
	go s.Start(queue)
	go c.Start()
	go StartZincAgent(zinc_agent)

	log.Println("start succeed")
	log.Printf("catch signal %v, program will exit", <-context.quit_chan)
}