Skip to content

Commit

Permalink
dispatch added
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Jan 29, 2018
1 parent 76122b8 commit 822250a
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 5 deletions.
17 changes: 17 additions & 0 deletions dispatch/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.9.2

WORKDIR /opt/gorcv

ENV GOPATH=/opt/gorcv \
GOBIN=/opt/gorcv/bin

# install external components
RUN go get \
github.com/streadway/amqp

COPY src /opt/gorcv/

RUN go build && go install

CMD bin/gorcv

30 changes: 30 additions & 0 deletions dispatch/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3'
services:
rabbitmq:
image: rabbitmq:3.7-management-alpine
ports:
- "5672"
- "15672:15672"
networks:
- robot-shop
payment:
build:
context: ../payment
image: steveww/rs-payment
depends_on:
- rabbitmq
ports:
- "8080:8080"
networks:
- robot-shop
dispatch:
build:
context: .
image: steveww/rs-dispatch
depends_on:
- rabbitmq
networks:
- robot-shop

networks:
robot-shop:
106 changes: 106 additions & 0 deletions dispatch/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"fmt"
"log"
"time"

"github.com/streadway/amqp"
)

var amqpUri string = "amqp://guest:guest@rabbitmq:5672/"

var (
rabbitConn *amqp.Connection
rabbitChan *amqp.Channel
rabbitCloseError chan *amqp.Error
rabbitReady chan bool
)

func connectToRabbitMQ(uri string) *amqp.Connection {
for {
conn, err := amqp.Dial(uri)
if err == nil {
return conn
}

log.Println(err)
log.Printf("Reconnecting to %s\n", uri)
time.Sleep(1 * time.Second)
}
}

func rabbitConnector(uri string) {
var rabbitErr *amqp.Error

for {
rabbitErr = <-rabbitCloseError
if rabbitErr != nil {
log.Printf("Connecting to %s\n", amqpUri)
rabbitConn = connectToRabbitMQ(uri)
rabbitCloseError = make(chan *amqp.Error)
rabbitConn.NotifyClose(rabbitCloseError)

var err error

// create mappings here
rabbitChan, err = rabbitConn.Channel()
failOnError(err, "Failed to create channel")

// create exchange
err = rabbitChan.ExchangeDeclare("robot-shop", "direct", true, false, false, false, nil)
failOnError(err, "Failed to create exchange")

// create queue
queue, err := rabbitChan.QueueDeclare("orders", true, false, false, false, nil)
failOnError(err, "Failed to create queue")

// bind queue to exchange
err = rabbitChan.QueueBind(queue.Name, "orders", "robot-shop", false, nil)
failOnError(err, "Failed to bind queue")

// signal ready
rabbitReady <- true
}
}
}

func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("$s : %s", msg, err)
panic(fmt.Sprintf("%s : %s", msg, err))
}
}


func main() {
// MQ error channel
rabbitCloseError = make(chan *amqp.Error)

// MQ ready channel
rabbitReady = make(chan bool)

go rabbitConnector(amqpUri)

rabbitCloseError <- amqp.ErrClosed

go func() {
for {
// wait for rabbit to be ready
ready := <-rabbitReady
log.Printf("Rabbit MQ ready %v\n", ready)

// subscribe to bound queue
msgs, err := rabbitChan.Consume("orders", "", false, false, false, false, nil)
failOnError(err, "Failed to consume")

for d := range msgs {
log.Printf("Order %s\n", d.Body)
}
}
}()

log.Println("Waiting for messages")
forever := make(chan bool)
<-forever
}
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,20 @@ services:
image: steveww/rs-payment
depends_on:
- rabbitmq
environment:
- AUTOWRAPT_BOOTSTRAP=flask
ports:
- "8080"
networks:
- robot-shop
dispatch:
build:
context: dispatch
image: steveww/rs-dispatch
depends_on:
- rabbitmq
networks:
- robot-shop
web:
build:
context: web
Expand Down
3 changes: 2 additions & 1 deletion payment/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: '3'
services:
rabbitmq:
image: rabbitmq:3.7-alpine
image: rabbitmq:3.7-management-alpine
ports:
- "5672"
- "15672:15672"
payment:
build:
context: .
Expand Down
2 changes: 1 addition & 1 deletion payment/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _connect(self):
if not self._conn or self._conn.is_closed:
self._conn = pika.BlockingConnection(self._params)
self._channel = self._conn.channel()
self._channel.exchange_declare(exchange=self.EXCHANGE, exchange_type=self.TYPE)
self._channel.exchange_declare(exchange=self.EXCHANGE, exchange_type=self.TYPE, durable=True)
self._logger.info('connected to broker')

def _publish(self, msg):
Expand Down
Binary file modified shipping/database/scripts/10-dump.sql.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion web/static/cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<th>Sub Total</th>
</tr>
<tr ng-repeat="item in data.cart.items">
<td><input type="number" size="2" min="0" max="10" ng-model="item.qty" ng-change="change(item.sku, item.qty);"/></td>
<td>
<input type="number" size="2" min="0" max="10" ng-model="item.qty" ng-change="change(item.sku, item.qty);"/>
</td>
<td>{{ item.name }}</td>
<td class="currency">&euro;{{ item.subtotal.toFixed(2) }}</td>
</tr>
Expand Down
18 changes: 16 additions & 2 deletions web/static/js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,27 @@
url: '/api/cart/cart/' + id,
method: 'GET'
}).then((res) => {
$scope.data.cart = res.data;
var cart = res.data;
// remove shipping - last item in cart
if(cart.items[cart.items.length - 1].sku == 'SHIP') {
$http({
url: '/api/cart/update/' + id + '/SHIP/0',
method: 'GET'
}).then((res) => {
$scope.data.cart = res.data;
}).catch((e) => {
console.log('ERROR', e);
});
} else {
$scope.data.cart = cart;
}
}).catch((e) => {
console.log('ERROR', e);
});
}

loadCart($scope.data.uniqueid);
console.log('cart init');
});

robotshop.controller('shipform', function($scope, $http, $location, currentUser) {
Expand Down Expand Up @@ -330,7 +344,7 @@
data: $scope.data.cart
}).then((res) => {
console.log('order', res.data);
$scope.data.message = 'Order placed ' + res.data.orderid;
$scope.data.message = 'Order placed ' + res.data.order;
// clear down cart
$scope.data.cart = {
total: 0,
Expand Down

0 comments on commit 822250a

Please sign in to comment.