Skip to content

Commit

Permalink
feat: dockerize php application
Browse files Browse the repository at this point in the history
  • Loading branch information
mashb1t committed Jan 26, 2022
1 parent 9ddc6f9 commit 55b52ca
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 543 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea
.DS_Store
.env
configuration.env
*.env
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ It uses a PHP Script for fetching data from the Spotify API and saves it to Infl

It is advised to use a PHP website serving tool of your choice, such as [Laravel Valet].

1. Copy ``configuration.env.dist`` to ``configuration.env`` and change the credentials
1. Copy ``php/.env.dist`` to ``php/env`` and change the credentials
2. Run ``docker-compose up -d`` to provision Grafana and InfluxDB
1. Copy ``config/*.env.dist`` to ``config/*.env`` and change the credentials
2. Run ``docker-compose up -d`` to provision Grafana, InfluxDB and PHP (via Nginx)
3. Log in to the [Spotify Developer] website
4. Set up a new Spotify App, save client id and client secret on your local machine
5. Set up static website serving by using ``cd php && valet link spotisights``
6. (optional) Make your connection secure by executing ``valet secure``
7. Edit your Spotify App and add the callback URL ``https://spotisights.test/callback.php`` (http when not securing the connection)
4. Set up a new Spotify App, add the callback URL ``http://localhost:8080/callback.php`` and add client id and client secret to ``php.env``

## Data Flow

1. Call https://spotisights.test/ and follow the displayed auth flow (technical reference: [Authorization Code Flow])
2. app.php will be called which collects recent tracks fron the logged in user, saves them to InfluxDB and prompts "done" when finished
1. Call http://localhost:8080/ and follow the displayed auth flow (technical reference: [Authorization Code Flow])
2. app.php is called and collects recent tracks fron the authorized user, saves them to InfluxDB and prompts "done" when finished
3. Open Grafana at http://localhost:3000/ and navigate to the Dashboard "spotisights" (http://localhost:3000/?orgId=1&search=open)

## Auth
Expand Down
5 changes: 0 additions & 5 deletions configuration.env.dist → config/grafana.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=admin
GF_INSTALL_PLUGINS=

# InfluxDB options
INFLUXDB_DB=influx
INFLUXDB_ADMIN_USER=admin
INFLUXDB_ADMIN_PASSWORD=admin
4 changes: 4 additions & 0 deletions config/influxdb.env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# InfluxDB options
INFLUXDB_DB=influx
INFLUXDB_ADMIN_USER=admin
INFLUXDB_ADMIN_PASSWORD=admin
13 changes: 13 additions & 0 deletions config/php.env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# InfluxDB
INFLUXDB_URL=http://influxdb:8086
INFLUXDB_TOKEN=secrettoken
INFLUXDB_BUCKET=influx
INFLUXDB_ORG=my-org

# Spotify
SPOTIFY_CLIENT_ID=123
SPOTIFY_CLIENT_SECRET=123
SPOTIFY_REDIRECT_URL=http://localhost:8080/callback.php

# MISC
USERNAME=testuser
28 changes: 26 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:

influxdb:
image: influxdb:1.8-alpine
env_file: configuration.env
env_file: config/influxdb.env
ports:
- '8086:8086'
volumes:
Expand All @@ -13,7 +13,7 @@ services:
image: grafana/grafana:latest
depends_on:
- influxdb
env_file: configuration.env
env_file: config/grafana.env
links:
- influxdb
ports:
Expand All @@ -23,6 +23,30 @@ services:
- ./grafana/provisioning/:/etc/grafana/provisioning/
- ./grafana/dashboards/:/var/lib/grafana/dashboards/

php:
build:
context: ./php
depends_on:
- influxdb
links:
- influxdb
env_file: config/php.env
ports:
- '8080:80'
# - '8443:443'
volumes:
- ./php/src:/app:rw,cached
environment:
- WEB_DOCUMENT_ROOT=/app/public
- PHP_DISPLAY_ERRORS=1
- PHP_MEMORY_LIMIT=2048M
# - PHP_MAX_EXECUTION_TIME=-1
# - XDEBUG_MODE=debug
# - XDEBUG_START_WITH_REQUEST=yes
# - XDEBUG_CLIENT_PORT=9000
# - XDEBUG_CLIENT_HOST=docker.local
# - XDEBUG_MAX_NESTING_LEVEL=1000

volumes:
grafana_data: { }
influxdb_data: { }
22 changes: 0 additions & 22 deletions php/.env.dist

This file was deleted.

13 changes: 13 additions & 0 deletions php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM webdevops/php-nginx-dev:8.0

RUN mkdir /app/sessions

COPY ./src/composer.json /app/composer.json
COPY ./src/composer.lock /app/composer.lock

RUN composer install -d /app

COPY ./src/app /app/app
COPY ./src/public /app/public

WORKDIR /app
14 changes: 0 additions & 14 deletions php/composer.json

This file was deleted.

16 changes: 16 additions & 0 deletions php/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server {
index index.php index.html;
server_name phpfpm.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /app/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Empty file removed php/sessions/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion php/.gitignore → php/src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.env
vendor/
sessions
8 changes: 4 additions & 4 deletions php/App/SessionHandler.php → php/src/app/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class SessionHandler
{
const BASE_FILEPATH = 'sessions';
const BASE_FILEPATH = '..' . DIRECTORY_SEPARATOR . 'sessions';

public static function getSession(): Session
{
return new Session(
$_ENV['SPOTIFY_CLIENT_ID'],
$_ENV['SPOTIFY_CLIENT_SECRET'],
$_ENV['SPOTIFY_REDIRECT_URL'],
getenv('SPOTIFY_CLIENT_ID'),
getenv('SPOTIFY_CLIENT_SECRET'),
getenv('SPOTIFY_REDIRECT_URL'),
);
}

Expand Down
21 changes: 21 additions & 0 deletions php/src/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "mashb1t/spotisights",
"description": "Spotify Insights",
"autoload": {
"psr-4": {
"App\\": "app"
}
},
"authors": [
{
"name": "Manuel Schmid",
"email": "[email protected]"
}
],
"require": {
"league/statsd": "^1.5",
"ext-pcntl": "*",
"influxdata/influxdb-client-php": "^2.5",
"jwilsson/spotify-web-api-php": "^5.0"
}
}
Loading

0 comments on commit 55b52ca

Please sign in to comment.