Skip to content

Commit

Permalink
queues badge requests to redis
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptnull committed Aug 17, 2017
1 parent 5ebf72d commit 1ab8f19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ api-run:
export RABBIT_PASSWORD="password" && \
export RABBIT_HOSTNAME="localhost" && \
export RABBIT_PORT="5672" && \
export REDIS_HOSTNAME="localhost" && \
export REDIS_PORT="6379" && \
go run ./api/main.go

worker-run: build
Expand All @@ -88,4 +90,7 @@ worker-run: build
go run ./worker/main.go

queue-run:
docker run -d --hostname my-rabbit --name badgeit-rabbit -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management
docker run -d --hostname my-rabbit --name badgeit-rabbit -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management

redis-run:
docker run --name badgeit-redis -p 6379:6379 -d redis
23 changes: 21 additions & 2 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"os"

"github.com/garyburd/redigo/redis"

"github.com/gin-gonic/gin"
"github.com/streadway/amqp"
)
Expand All @@ -30,8 +32,16 @@ func main() {
defer workerChannel.Close()
log.Println("Initialized Message Queue successfully")

log.Println("Initializing Redis Message Queue")
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOSTNAME"), os.Getenv("REDIS_PORT")))
if err != nil {
log.Fatalln("Failed to initialize redis message queue", err)
}
defer conn.Close()
log.Println("Initialized Redis Message Queue successfully")

log.Println("Initializing API Server")
initAPIServer(workerQueue, workerChannel)
initAPIServer(workerQueue, workerChannel, conn)
}

func initMessageQueue() (*amqp.Connection, *amqp.Queue, *amqp.Channel, error) {
Expand Down Expand Up @@ -65,7 +75,7 @@ func initMessageQueue() (*amqp.Connection, *amqp.Queue, *amqp.Channel, error) {
return conn, &q, ch, nil
}

func initAPIServer(workerQueue *amqp.Queue, workerChannel *amqp.Channel) {
func initAPIServer(workerQueue *amqp.Queue, workerChannel *amqp.Channel, redisConn redis.Conn) {
r := gin.Default()

r.POST("/test/callback", func(c *gin.Context) {
Expand Down Expand Up @@ -138,6 +148,15 @@ func initAPIServer(workerQueue *amqp.Queue, workerChannel *amqp.Channel) {
return
}

_, err = redisConn.Do("LPUSH", "badge:worker", []byte(jsonPayload))
if err != nil {
log.Println("Unable to queue request", err)
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Unable to queue request",
})
return
}

c.JSON(http.StatusAccepted, payload)
return
})
Expand Down

0 comments on commit 1ab8f19

Please sign in to comment.