Skip to content

Commit

Permalink
Merge e3c71b0 into f47f8dd
Browse files Browse the repository at this point in the history
  • Loading branch information
meadsteve committed Apr 2, 2019
2 parents f47f8dd + e3c71b0 commit 391cdb3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Dockerfile
@@ -0,0 +1,56 @@
FROM php:7.3-fpm AS base

RUN apt-get update -y \
&& apt-get install -y nginx

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"

RUN docker-php-ext-install opcache

# Has all the files AND composer installed constructs everything needed for the app
FROM base AS builder
WORKDIR /app

# Composer needs git and some zip deps
RUN apt-get install -y git libzip-dev
RUN docker-php-ext-install zip
COPY ./docker/install_composer.sh /tmp/install_composer.sh
RUN /tmp/install_composer.sh && rm /tmp/install_composer.sh

# Install the dependencies
COPY ./composer.* /app/
RUN php composer.phar install \
&& rm -rf /home/root/.composer/cache

# Copy over our app code
COPY ./www /app/www/
COPY ./src /app/src/

# Now all the app code is over we can build the final autoloader and
# remove composer.
RUN php composer.phar dump-autoload --no-dev \
&& rm composer.phar

# This is the final image that we'll serve from
FROM base AS final
WORKDIR /app
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/php-opocache-cfg.ini


COPY ./docker/nginx-site.conf /etc/nginx/sites-enabled/default
COPY ./docker/run_app.sh /etc/run_app.sh

# The builder has already pulled all composer deps & built the autoloader
COPY --from=builder /app /app

EXPOSE 80 443
CMD ["/etc/run_app.sh"]
HEALTHCHECK CMD curl http://localhost:80/health-check
9 changes: 9 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,9 @@
version: "3"

services:
dice-api:
build:
context: ./
dockerfile: Dockerfile
ports:
- "8089:80"
17 changes: 17 additions & 0 deletions docker/install_composer.sh
@@ -0,0 +1,17 @@
#!/bin/sh
set -ex

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIGNATURE="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
EXPECTED_SIGNATURE="48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5"

if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]
then
>&2 echo 'ERROR: Invalid installer signature'
rm composer-setup.php
exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
22 changes: 22 additions & 0 deletions docker/nginx-site.conf
@@ -0,0 +1,22 @@
server {
root /app/www;

include /etc/nginx/default.d/*.conf;

index app.php index.php index.html index.htm;

client_max_body_size 30m;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index app.php;
include fastcgi.conf;
}
}
3 changes: 3 additions & 0 deletions docker/run_app.sh
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
service nginx start
php-fpm
5 changes: 5 additions & 0 deletions src/DiceApp.php
Expand Up @@ -49,12 +49,17 @@ public function diceStats(Request $request, Response $response)
->withHeader("Content-Type", "application/json");
}

public function healthCheck(Request $request, Response $response) {
return $response->write("ok");
}

private function setupRoutes()
{
$diceRequestHandler = $this->diceRequestHandler;

$this->get("/", [$this, 'index']);
$this->get("/dice-stats", [$this, 'diceStats']);
$this->get("/health-check", [$this, 'healthCheck']);
$this->get(self::DICE_PATH_REGEX, [$diceRequestHandler, 'getDice']);

foreach ($this->diceRequestHandler->contentTypesForPaths() as $path => $contentType) {
Expand Down

0 comments on commit 391cdb3

Please sign in to comment.