Skip to content

Commit

Permalink
Removes rabbitmq from api
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptnull committed Aug 17, 2017
1 parent 215887d commit c303bda
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 71 deletions.
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,11 @@ init: init-samples
clean: clean-samples

api-run:
export RABBIT_USERNAME="user" && \
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
export RABBIT_USERNAME="user" && \
export RABBIT_PASSWORD="password" && \
export RABBIT_HOSTNAME="localhost" && \
export RABBIT_PORT="5672" && \
export CLONE_DIR="`pwd`/worker/storage" && \
export REDIS_HOSTNAME="localhost" && \
export REDIS_PORT="6379" && \
Expand Down
66 changes: 3 additions & 63 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/garyburd/redigo/redis"

"github.com/gin-gonic/gin"
"github.com/streadway/amqp"
)

const (
Expand All @@ -20,18 +19,8 @@ const (
)

func main() {

log.Println("Booting Badgeit API server")

log.Println("Initializing Message Queue")
amqpConnection, workerQueue, workerChannel, err := initMessageQueue()
if err != nil {
log.Fatalln("Failed to initialize message queue", err)
}
defer amqpConnection.Close()
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 {
Expand All @@ -41,41 +30,10 @@ func main() {
log.Println("Initialized Redis Message Queue successfully")

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

func initMessageQueue() (*amqp.Connection, *amqp.Queue, *amqp.Channel, error) {
username := os.Getenv("RABBIT_USERNAME")
password := os.Getenv("RABBIT_PASSWORD")
hostname := os.Getenv("RABBIT_HOSTNAME")
port := os.Getenv("RABBIT_PORT")
conStr := fmt.Sprintf("amqp://%s:%s@%s:%s/", username, password, hostname, port)
conn, err := amqp.Dial(conStr)
if err != nil {
return nil, nil, nil, err
}

ch, err := conn.Channel()
if err != nil {
return nil, nil, nil, err
}

q, err := ch.QueueDeclare(
"badgeit.worker", // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
return nil, nil, nil, err
}

return conn, &q, ch, nil
initAPIServer(conn)
}

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

r.POST("/test/callback", func(c *gin.Context) {
Expand Down Expand Up @@ -130,25 +88,7 @@ func initAPIServer(workerQueue *amqp.Queue, workerChannel *amqp.Channel, redisCo

// queue a task for the worker
jsonPayload, _ := json.Marshal(payload)
err := workerChannel.Publish(
"", // exchange
workerQueue.Name, // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(jsonPayload),
})
if err != nil {
log.Println("Unable to queue request", err)
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Unable to queue request",
})
return
}

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

0 comments on commit c303bda

Please sign in to comment.