Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# Customer Information Portal

# Requirements
- you need to install docker-compose

# Running the app
- $ docker-compose up --build db customerapi web
- load some data into the db (c.f. command below)

# Loading Data
- $ docker exec -it database bash ./load_data

# Testing the API
- Get the app running
- Load data with the command above
- $ docker-compose up --build apitest

# Seeing the data in a browser
- Get the app running
- Load data with the command above (if you've tested you need to do
nothing else).
- Visit localhost:8000



# PHP JavaScript Developer Test

A simple test for PHP / JavaScript Developers
Expand Down
18 changes: 18 additions & 0 deletions apitest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This will create an image for frisby API tests.

FROM node
MAINTAINER Robert Higgins <[email protected]>

RUN npm install -g frisby
RUN npm install -g jasmine-node

WORKDIR /opt/frisby/

ENV NODE_PATH /usr/local/lib/node_modules/

ADD src/ /opt/frisby/api_tests

CMD jasmine-node \
--config CUSTOMERAPI_PORT_80_TCP_ADDR $CUSTOMERAPI_PORT_80_TCP_ADDR \
--config CUSTOMERAPI_PORT_80_TCP_PORT $CUSTOMERAPI_PORT_80_TCP_PORT \
api_tests
42 changes: 42 additions & 0 deletions apitest/src/basic_connection_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
describe('Check the customer json', function() {
var frisby = require('frisby');
var URL = 'http://' + process.env['CUSTOMERAPI_PORT_80_TCP_ADDR'] + ':'
+ process.env['CUSTOMERAPI_PORT_80_TCP_PORT'];

frisby.create('GET Method')
.get(URL + '/customers.php')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSONTypes('*', Array)
.expectJSONLength(1000)
.expectJSONTypes('0', {
id: String,
first_name: String,
last_name: String,
email: String,
gender: String,
ip_address: String,
company: String,
city: String,
title: String,
website: String
})
.expectJSON('0', {
id: "1",
first_name: "Laura",
last_name: "Richards",
email: "[email protected]",
gender: "Female",
ip_address: "81.192.7.99",
company: "Meezzy",
city: "Kallith\u00e9a",
title: "Biostatistician III",
website: "https:\/\/intel.com\/aliquam\/lacus\/morbi\/quis.png?ante=in&vivamus=sapien&tortor=iaculis&duis=congue&mattis=vivamus&egestas=metus&metus=arcu&aenean=adipiscing&fermentum=molestie&donec=hendrerit&ut=at&mauris=vulputate&eget=vitae&massa=nisl&tempor=aenean&convallis=lectus&nulla=pellentesque&neque=eget&libero=nunc&convallis=donec&eget=quis&eleifend=orci&luctus=eget&ultricies=orci&eu=vehicula&nibh=condimentum"
})
.toss();

frisby.create('POST Method')
.post(URL + '/customers.php', {})
.expectStatus(400)
.toss();
});
9 changes: 9 additions & 0 deletions customerapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM php:5.6-apache
MAINTAINER Robert Higgins <[email protected]>

RUN apt-get update && apt-get install -y \
libpq-dev \
postgresql-client
RUN docker-php-ext-install pdo pgsql pdo_pgsql

COPY src/ /var/www/html/
25 changes: 25 additions & 0 deletions customerapi/src/customers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This will just extract a list of all customers and return it as json
*/

$method = $_SERVER["REQUEST_METHOD"];
if ($method != 'GET') {
http_response_code(400);
die("Only GET is permitted.");
}

$dbname = getenv("POSTGRES_DB");
$password = getenv("DATABASE_PASSWORD");
$user = getenv("POSTGRES_USER");
$dbconn = pg_connect("host=db dbname=$dbname user=$user password=$password");

$result = pg_query($dbconn, "SELECT * FROM customer");


header('Content-Type: application/json');
http_response_code(200);
echo json_encode(pg_fetch_all($result));

?>
8 changes: 8 additions & 0 deletions database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM postgres:9.3
MAINTAINER Robert Higgins <[email protected]>

ADD schema/ /docker-entrypoint-initdb.d/
ADD data/ data/
ADD load_data load_data

VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
1,001 changes: 1,001 additions & 0 deletions database/data/customers.csv

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions database/load_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/bash

# Remove the first line from the csv file
FILE=data/customers.csv
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"

#copy into table from file
psql -d "$POSTGRES_DB" -U "$POSTGRES_USER" \
-c "COPY customer FROM '/data/customers.csv' DELIMITER ',' CSV;"

exit 0
12 changes: 12 additions & 0 deletions database/schema/001_customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE customer (
id SERIAL PRIMARY KEY,
first_name VARCHAR,
last_name VARCHAR,
email VARCHAR,
gender VARCHAR,
ip_address VARCHAR,
company VARCHAR,
city VARCHAR,
title VARCHAR,
website VARCHAR
);
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
customerapi:
build: customerapi
container_name: customerapi
ports:
- "8001:80"
links:
- db
environment:
POSTGRES_USER: some-user
DATABASE_PASSWORD: a-secret-password-for-this-environment
POSTGRES_DB: dev-test-db
web:
build: web
container_name: webserver
ports:
- "8000:80"
links:
- customerapi
db:
build: database
container_name: database
environment:
POSTGRES_USER: some-user
DATABASE_PASSWORD: a-secret-password-for-this-environment
POSTGRES_DB: dev-test-db
apitest:
build: apitest
container_name: frisby_tests
links:
- customerapi
4 changes: 4 additions & 0 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM php:5.6-apache
MAINTAINER Robert Higgins <[email protected]>

COPY src/ /var/www/html/
45 changes: 45 additions & 0 deletions web/src/customers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* Front page returns customer data in an ugly table.
*/

// Get the list of customers
$curl = curl_init();
$customer_url = "http://".getenv("CUSTOMERAPI_PORT_80_TCP_ADDR").":".
getenv("CUSTOMERAPI_PORT_80_TCP_PORT")."/customers.php";

curl_setopt($curl, CURLOPT_URL, $customer_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);


$customers = json_decode(curl_exec($curl), true);
curl_close($curl);


// Present the list to the user
echo "<h1>List of Customers</h1>";

echo "<table border=\"1\">";
echo "<thead><tr>";
foreach(array_keys($customers[0]) as $column) {
echo "<th>".$column."</th>";
}
echo "</tr></thead>";

echo "<tbody>";
foreach($customers as $customer) {
echo "<tr>";
foreach($customer as $colum_name => $field) {
if ($colum_name == "website") {
echo "<td><a href=\"".$field."\">Website</a></td>";
} else {
echo "<td>".$field."</td>";
}
}
echo "</tr>";
}
echo "</tbody>";

echo "</table>";
?>
8 changes: 8 additions & 0 deletions web/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<h1>List of Customers</h1>
<form action="/customers.php">
<input type="submit" value="Click to see a list of customers">
</form>
</body>
</html>