예제 #1
0
func main() {

	// create kafka producer
	var producer = kafka.NewProducer(brokers)

	// create http renderer
	var renderer = render.New(render.Options{
		IndentJSON: true,
	})

	// create mongodb session
	session, err := mgo.Dial(os.Getenv("MONGODB_CS"))
	if err != nil {
		log.Fatal("Cannot connect to mongo server: %s", err)
	}

	mongo := session.DB(os.Getenv("MONGODB_DB"))

	// create controller
	controller := Controller{
		mongo:    mongo,
		producer: producer,
		renderer: renderer,
	}

	log.Println("Initializing consumers...")

	// listen to the creation of new users
	// usersConsumer := kafka.NewConsumer(brokers, usersTopic)
	// usersConsumer.Consume(controller.ConsumeUsers )

	// listen to the creation of new discussions
	discussionsConsumer := kafka.NewConsumer(brokers, discussionsTopic)
	discussionsConsumer.Consume(controller.ConsumeDiscussions)

	answersConsumer := kafka.NewConsumer(brokers, answersTopic)
	answersConsumer.Consume(controller.ConsumeAnswers)

	log.Println("Initializing routes...")

	// create routes
	router := httprouter.New()
	router.GET("/api/v1/discussions", controller.ListDiscussions)
	router.GET("/api/v1/discussions/:id", controller.ShowDiscussion)

	log.Println("Start listening...")

	log.Fatal(http.ListenAndServe(":8080", router))

	defer func() {
		producer.Close()
		session.Close()
		// usersConsumer.Close()
		discussionsConsumer.Close()
	}()
}
예제 #2
0
func main() {

	brokers := strings.Split(os.Getenv("KAFKA_BROKERS"), ",")

	// create kafka producer
	var producer = kafka.NewProducer(brokers)

	// create http renderer
	var renderer = render.New(render.Options{
		IndentJSON: true,
	})

	// create mongodb session
	session, err := mgo.Dial(os.Getenv("MONGODB_CS"))
	if err != nil {
		log.Fatal("Cannot connect to mongo server: %s", err)
	}

	mongo := session.DB(os.Getenv("MONGODB_DB"))

	// create controller
	controller := Controller{
		mongo:    mongo,
		producer: producer,
		renderer: renderer,
	}

	log.Println("Initializing consumers...")

	// listen to the creation of new users
	usersConsumer := kafka.NewConsumer(brokers, usersTopic)
	usersConsumer.Consume(controller.ConsumeUsers)

	// listen to the creation of new users
	discussionsConsumer := kafka.NewConsumer(brokers, discussionsTopic)
	discussionsConsumer.Consume(controller.ConsumeDiscussions)

	log.Println("Initializing routes...")

	// create routes
	router := httprouter.New()
	router.POST("/api/v1/commands/postAnswer", controller.PostAnswer)

	log.Println("Start listening...")

	log.Fatal(http.ListenAndServe(":8080", router))

	defer func() {
		producer.Close()
		session.Close()
		usersConsumer.Close()
	}()
}