forked from linuxacademy/robot-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
466 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} | ||
?> |
Oops, something went wrong.