Skip to content

Commit

Permalink
Merge branch 'php'
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Aug 21, 2018
2 parents 61b7e02 + d1863f3 commit bb2cbd5
Show file tree
Hide file tree
Showing 28 changed files with 466 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# environment file for docker-compose
REPO=robotshop
TAG=0.2.9
TAG=0.3.0
2 changes: 1 addition & 1 deletion K8s/descriptors/mysql-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
io.kompose.service: mysql
spec:
containers:
- image: robotshop/rs-shipping-db:latest
- image: robotshop/rs-mysql-db:latest
name: mysql
ports:
- containerPort: 3306
Expand Down
33 changes: 33 additions & 0 deletions K8s/descriptors/ratings-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose -f ../docker-compose.yaml convert
kompose.version: 1.10.0 (8bb0907)
creationTimestamp: null
labels:
io.kompose.service: ratings
name: ratings
spec:
replicas: 1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: ratings
spec:
containers:
- image: robotshop/rs-ratings:latest
name: mysql
ports:
- containerPort: 3306
resources:
limits:
cpu: 200m
memory: 100Mi
requests:
cpu: 100m
memory: 50Mi
restartPolicy: Always
status: {}
19 changes: 19 additions & 0 deletions K8s/descriptors/ratings-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose -f ../docker-compose.yaml convert
kompose.version: 1.10.0 (8bb0907)
creationTimestamp: null
labels:
io.kompose.service: ratings
name: ratings
spec:
ports:
- name: "3306"
port: 3306
targetPort: 3306
selector:
io.kompose.service: ratings
status:
loadBalancer: {}
18 changes: 15 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ services:
condition: on-failure
mysql:
build:
context: shipping/database
image: ${REPO}/rs-shipping-db:${TAG}
context: mysql
image: ${REPO}/rs-mysql-db:${TAG}
networks:
- robot-shop
deploy:
Expand All @@ -75,7 +75,7 @@ services:
condition: on-failure
shipping:
build:
context: shipping/service
context: shipping
image: ${REPO}/rs-shipping:${TAG}
depends_on:
- mysql
Expand All @@ -85,6 +85,18 @@ services:
replicas: 1
restart_policy:
condition: on-failure
ratings:
build:
context: ratings
image: ${REPO}/rs-ratings:${TAG}
networks:
- robot-shop
depends_on:
- mysql
deploy:
replicas: 1
restart_policy:
condition: on-failure
payment:
build:
context: payment
Expand Down
6 changes: 6 additions & 0 deletions load-gen/robot-shop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from locust import HttpLocust, TaskSet, task
from random import choice
from random import randint

class UserBehavior(TaskSet):
def on_start(self):
Expand Down Expand Up @@ -33,7 +34,12 @@ def load(self):
if item['instock'] != 0:
break

# vote for item
if randint(1, 10) <= 3:
self.client.put('/api/ratings/api/rate/{}/{}'.format(item['sku'], randint(1, 5)))

self.client.get('/api/catalogue/product/{}'.format(item['sku']))
self.client.get('/api/ratings/api/fetch/{}'.format(item['sku']))
self.client.get('/api/cart/add/{}/{}/1'.format(uniqueid, item['sku']))

cart = self.client.get('/api/cart/cart/{}'.format(uniqueid)).json()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions mysql/scripts/20-ratings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE DATABASE ratings
DEFAULT CHARACTER SET 'utf8';

USE ratings;

CREATE TABLE ratings (
sku varchar(80) NOT NULL,
avg_rating DECIMAL(3, 2) NOT NULL,
rating_count INT NOT NULL,
PRIMARY KEY (sku)
) ENGINE=InnoDB;


GRANT ALL ON ratings.* TO 'ratings'@'%'
IDENTIFIED BY 'iloveit';

File renamed without changes.
14 changes: 14 additions & 0 deletions ratings/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM php:7.2-apache

RUN docker-php-ext-install pdo_mysql

# relax permissions on status
COPY status.conf /etc/apache2/mods-available/status.conf
# Enable Apache mod_rewrite and status
RUN a2enmod rewrite && a2enmod status


WORKDIR /var/www/html

COPY html/ /var/www/html

6 changes: 6 additions & 0 deletions ratings/html/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/(.*)$ api.php?request=$1 [QSA,NC,L]
</IfModule>
94 changes: 94 additions & 0 deletions ratings/html/API.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
abstract class API {
protected $method = '';

protected $endpoint = '';

protected $verb = '';

protected $args = array();

protected $file = Null;

public function __construct($request) {
// CORS
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Content-Type: application/json');

$this->args = explode('/', rtrim($request, '/'));
$this->endpoint = array_shift($this->args);

if(array_key_exists(0, $this->args) && !is_numeric($this->args[0])) {
$this->verb = array_shift($this->args);
}

$this->method = $_SERVER['REQUEST_METHOD'];
if($this->method == 'POST' && array_key_exists('HTTP_X_METHOD', $_SERVER)) {
if($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$this->method = 'DELETE';
} else if($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$this->method = 'PUT';
} else {
throw new Exception('Unexpected header');
}
}

switch($this->method) {
case 'DELETE':
case 'POST':
$this->request = $this->_cleanInputs($_POST);
break;
case 'GET':
$this->request = $this->_cleanInputs($_GET);
break;
case 'PUT':
$this->request = $this->_cleanInputs($_GET);
$this->file = file_get_contents('php://input');
break;
}
}

public function processAPI() {
if(method_exists($this, $this->endpoint)) {
try {
$result = $this->{$this->endpoint}();
return $this->_response($result, 200);
} catch (Exception $e) {
return $this->_response($e->getMessage(), $e->getCode());
}
}
return $this->_response("No endpoint: $this->endpoint", 404);
}

private function _response($data, $status = 200) {
header('HTTP/1.1 ' . $status . ' ' . $this->_requestStatus($status));
return json_encode($data);
}

private function _cleanInputs($data) {
$clean_input = array();

if(is_array($data)) {
foreach($data as $k => $v) {
$clean_input[$k] = $this->_cleanInputs($v);
}
} else {
$clean_input = trim(strip_tags($data));
}

return $clean_input;
}

private function _requestStatus($code) {
$status = array(
200 => 'OK',
400 => 'Bad Request',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error');

return (array_key_exists("$code", $status) ? $status["$code"] : $status['500']);
}
}
?>
Loading

0 comments on commit bb2cbd5

Please sign in to comment.