Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker support #34

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# App settings
APP_BASE=https://indielogin.com/
APP_NAME=IndieLogin.com
APP_USERAGENT=

# Github client settings
GITHUB_ID=
GITHUB_SECRET=

# Twitter client settings
TWITTER_ID=
TWITTER_SECRET=

# Mailgun settings
MAILGUN_KEY=key-
MAILGUN_DOMAIN=mail.indielogin.com
MAILGUN_FROM="indielogin.com" <[email protected]>

# Service settings
PGP_API=http://pgp:9009
REDIS_API=tcp://redis:6379

# Database settings
MYSQL_HOST=database
MYSQL_DATABASE=indielogin
MYSQL_USER=indielogin
MYSQL_PASSWORD=indielogin
MYSQL_RANDOM_ROOT_PASSWORD=yes
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ vendor/
lib/config.php
logs/
pgp/data/
data/
.env
3 changes: 3 additions & 0 deletions Dockerfile.database
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mariadb

COPY schema/schema.sql /docker-entrypoint-initdb.d
11 changes: 11 additions & 0 deletions Dockerfile.pgp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ruby:alpine

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apk add build-base

COPY pgp /usr/src/app
RUN bundle install

CMD rackup --host 0.0.0.0 -p 9009
25 changes: 25 additions & 0 deletions Dockerfile.web
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Install dependencies

FROM composer AS dependencies

RUN mkdir -p /var/www/html
WORKDIR /var/www/html

COPY . /var/www/html

RUN composer install

# Serve website

FROM php:apache

RUN mkdir -p /var/www/html
WORKDIR /var/www/html

COPY --from=dependencies /var/www/html /var/www/html

RUN docker-php-ext-install pdo_mysql mysqli
filips123 marked this conversation as resolved.
Show resolved Hide resolved
RUN a2enmod rewrite

RUN sed -i 's/DocumentRoot \/var\/www\/html/DocumentRoot \/var\/www\/html\/public/' /etc/apache2/sites-available/000-default.conf
RUN sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
55 changes: 55 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
version: "3"

services:
web:
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
volumes:
- .:/var/www/html
environment:
- APP_BASE
- APP_NAME
- GITHUB_ID
- GITHUB_SECRET
- TWITTER_ID
- TWITTER_SECRET
- MAILGUN_KEY
- MAILGUN_DOMAIN
- MAILGUN_FROM
- VERIFICATION_API
- MYSQL_DATABASE
- MYSQL_USER
- MYSQL_PASSWORD
depends_on:
- database
- pgp
- redis

database:
build:
context: .
dockerfile: Dockerfile.database
volumes:
- .:/var/www/html
- data:/var/lib/mysql
environment:
- MYSQL_DATABASE
- MYSQL_USER
- MYSQL_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD

pgp:
build:
context: .
dockerfile: Dockerfile.pgp
volumes:
- .:/var/www/html

redis:
image: redis:alpine

volumes:
data: {}
55 changes: 55 additions & 0 deletions lib/config.docker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this self-defeating to make the values globally available twice?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only easy option.

You can't set static properties dynamically with function call so I have to first save them into constants.

But it may also work with normal variables instead of constants.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only easy option.

I think putting the argument in the connection utility fits better with a heroku + docker (12-factor) deployment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think putting the argument in the connection utility fits better with a heroku + docker (12-factor) deployment.

Can you explain this a bit more?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So instead of having config be the source for all things, you could instead build an interface and have multiple implementations that fulfil that interface to provide details.

A Lighter weight option that does similar would be using null coalesce operator, leaving the config as null, and Config::$value ?? ENV['ENV_VAR1'] ?? ENV['ENV_VAR2'];

http://sandbox.onlinephpfunctions.com/code/22d382de6f0128a63d11ba37b79a4c7d3faad57a is a trivial example

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now setting properties directly from env vars. It doesn't need null coalesce operator, because env vars need to be set anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there has been confusion about my meaning.

Also blank string is not really the best option.

null specifically says "unknown"

class Config {
public static $base = '';
public static $name = '';
public static $useragent = '';

public static $githubClientID = '';
public static $githubClientSecret = '';

public static $twitterClientID = '';
public static $twitterClientSecret = '';

public static $mailgun = [
'key' => '',
'domain' => '',
'from' => ''
];

public static $pgpVerificationAPI = '';
public static $redisAPI = '';

public static $db = [
'host' => '',
'database' => '',
'username' => '',
'password' => ''
];
}

Config::$base = getenv('APP_BASE', true);
Config::$name = getenv('APP_NAME', true);
Config::$useragent = getenv('APP_USERAGENT', true);

Config::$githubClientID = getenv('GITHUB_ID', true);
Config::$githubClientSecret = getenv('GITHUB_SECRET', true);

Config::$twitterClientID = getenv('TWITTER_ID', true);
Config::$twitterClientSecret = getenv('TWITTER_SECRET', true);

Config::$mailgun = [
'key' => getenv('MAILGUN_KEY', true),
'domain' => getenv('MAILGUN_DOMAIN', true),
'from' => getenv('MAILGUN_FROM', true)
];

Config::$pgpVerificationAPI = getenv('PGP_API', true);
Config::$redisAPI = getenv('REDIS_API', true);

Config::$mailgun = [
'host' => getenv('MYSQL_HOST', true),
'database' => getenv('MYSQL_DATABASE', true),
'username' => getenv('MYSQL_USER', true),
'password' => getenv('MYSQL_PASSWORD', true)
];
2 changes: 2 additions & 0 deletions lib/config.template.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class Config {
public static $base = 'https://indielogin.com/';
public static $name = 'IndieLogin.com';
Expand All @@ -17,6 +18,7 @@ class Config {
];

public static $pgpVerificationAPI = 'http://127.0.0.1:9009';
public static $redisAPI = 'tcp://127.0.0.1:6379';
filips123 marked this conversation as resolved.
Show resolved Hide resolved

public static $db = [
'host' => '127.0.0.1',
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function random_user_code() {
function redis() {
static $client = false;
if(!$client)
$client = new Predis\Client('tcp://127.0.0.1:6379');
$client = new Predis\Client(Config::$redisAPI);
return $client;
}

Expand Down
7 changes: 7 additions & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</IfModule>