dispatch added

This commit is contained in:
Steve Waterworth
2018-01-29 17:40:12 +00:00
parent 76122b8917
commit 822250a94b
9 changed files with 185 additions and 5 deletions

17
dispatch/Dockerfile Normal file
View File

@@ -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

View File

@@ -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
dispatch/src/main.go Normal file
View File

@@ -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
}

View File

@@ -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

View File

@@ -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: .

View File

@@ -21,7 +21,7 @@ class Publisher:
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):

View File

@@ -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>

View File

@@ -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) {
@@ -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,