Пример #1
0
func (rs *RedisSubscriber) Subscribe(channel string, msgChan chan<- *message.Message) {
	pubsub, err := rs.redisClient.Subscribe(channel)

	if err != nil {
		panic(err)
	}
	defer pubsub.Close()

	for {
		redisMsg, err := pubsub.ReceiveMessage()

		if err != nil {
			log.Println("Error on receiving msg from redis: %v", err)
		}

		msg, err := message.NewMessageFromJSON(redisMsg.Payload)
		if err != nil {
			log.Println("Error on parsing msg: %v", err)
		}

		log.Println(msg.ToJSON())
		msgChan <- msg
	}
}
func main() {
	configPath := kingpin.Flag("config", "Configuration path").Short('c').Required().String()
	kingpin.Parse()

	eventStreamConfig := config.NewEventStreamConfigFromJSONFile(configPath)

	rs := common.NewRedisSubscriber(eventStreamConfig.RedisConfig)
	go rs.Subscribe(eventStreamConfig.RedisConfig.PubSubChannel, msgChannel)

	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	e.Index(eventStreamConfig.StaticPath + "/index.html")
	e.Static("/", eventStreamConfig.StaticPath)
	e.Get("/api", apiHandler)

	log.Println("Event stream is running on port: %s", eventStreamConfig.Port)
	e.Run(":" + eventStreamConfig.Port)
}
Пример #3
0
// New creates an instance of Echo.
func New() (e *Echo) {
	e = &Echo{maxParam: new(int)}
	e.pool.New = func() interface{} {
		return NewContext(nil, new(Response), e)
	}
	e.router = NewRouter(e)

	//----------
	// Defaults
	//----------

	e.HTTP2(true)
	e.defaultHTTPErrorHandler = func(err error, c *Context) {
		code := http.StatusInternalServerError
		msg := http.StatusText(code)
		if he, ok := err.(*HTTPError); ok {
			code = he.code
			msg = he.message
		}
		if e.debug {
			msg = err.Error()
		}
		if !c.response.committed {
			http.Error(c.response, msg, code)
		}
		if e.debug {
			log.Println(err)
		}
	}
	e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
	e.SetBinder(&binder{})

	// Logger
	e.logger = log.New("echo")
	e.logger.SetLevel(log.INFO)

	return
}