Skip to content

Commit ec6ad88

Browse files
authored
Merge pull request #7 from dev-community-de/prepare-production
Prepare production release
2 parents 5dfdaa5 + eb6da7a commit ec6ad88

11 files changed

Lines changed: 187 additions & 23 deletions

File tree

.docker/web/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ COPY .docker/web/vhost.conf /etc/apache2/sites-available/000-default.conf
55
WORKDIR /srv/app
66

77
RUN apt-get update -y && apt-get install -y clang-format
8+
9+
RUN mkdir -p /srv/app/storage/code
10+
RUN chmod -R ug+rwx /srv/app/storage/code && chown -R www-data:www-data /srv/app && a2enmod rewrite

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
storage
2+
.editorconfig
3+
.gitignore
4+
Dockerfile
5+
docker-compose.yml
6+
LICENSE
7+
README.md
8+
composer.json
9+
composer.lock

.gcloudignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
storage
2+
vendor
3+
.editorconfig
4+
.gitignore
5+
LICENSE
6+
README.md
7+
docker-compose.yml

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Dockerfile for production
2+
3+
FROM php:7.4-apache
4+
5+
COPY vhost.conf /etc/apache2/sites-available/000-default.conf
6+
COPY . /srv/app
7+
8+
WORKDIR /srv/app
9+
10+
RUN apt-get update -y && apt-get install -y clang-format
11+
12+
RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
13+
14+
RUN mkdir -p /srv/app/storage/code
15+
RUN chmod -R ug+rwx /srv/app/storage/code && chown -R www-data:www-data /srv/app && a2enmod rewrite
16+
17+
EXPOSE 8080

app/Api/Auth/ApiAuthorizer.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace DevCommunityDE\CodeFormatter\Api\Auth;
4+
5+
/**
6+
* Class ApiAuthorizer
7+
*
8+
* @package DevCommunityDE\CodeFormatter\Api\Auth
9+
*/
10+
class ApiAuthorizer
11+
{
12+
13+
/**
14+
* @var string
15+
*/
16+
protected $api_key;
17+
18+
/**
19+
*
20+
*/
21+
public function __construct()
22+
{
23+
$this->api_key = getenv('API_KEY') ?: '';
24+
}
25+
26+
/**
27+
*
28+
*/
29+
public function authorize()
30+
{
31+
if (!$this->checkRequestMethod()) {
32+
// set http status 405 method not allowed
33+
http_response_code(405);
34+
exit;
35+
}
36+
37+
if (
38+
!isset($_GET['api_key']) ||
39+
!$this->checkApiKey()
40+
) {
41+
// set http status 401 unauthorized
42+
http_response_code(401);
43+
exit;
44+
}
45+
}
46+
47+
/**
48+
* @return bool
49+
*/
50+
protected function checkRequestMethod() : bool
51+
{
52+
return strtoupper($_SERVER['REQUEST_METHOD']) === 'POST';
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
protected function checkApiKey() : bool
59+
{
60+
return hash_equals($this->api_key, $_GET['api_key']);
61+
}
62+
63+
}

bootstrap/app.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

33
use DevCommunityDE\CodeFormatter\CodeFormatterApp;
4+
use DevCommunityDE\CodeFormatter\Api\Auth\ApiAuthorizer;
45

56
require_once __DIR__ . '/../vendor/autoload.php';
67

8+
$auth = new ApiAuthorizer;
9+
$auth->authorize();
10+
711
$app = new CodeFormatterApp;
812
$app->run();

cloudbuild.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
steps:
2+
# install composer dependencies
3+
- name: 'gcr.io/$PROJECT_ID/composer'
4+
args: ['install', '--no-dev', '--optimize-autoloader']
5+
6+
# build the container image
7+
- name: 'gcr.io/cloud-builders/docker'
8+
args: ['build', '-t', 'gcr.io/$PROJECT_ID/code-formatter:$COMMIT_SHA', '.']
9+
10+
# push the container image to Container Registry
11+
- name: 'gcr.io/cloud-builders/docker'
12+
args: ['push', 'gcr.io/$PROJECT_ID/code-formatter:$COMMIT_SHA']
13+
14+
# Deploy container image to Cloud Run
15+
- name: 'gcr.io/cloud-builders/gcloud'
16+
args:
17+
- 'run'
18+
- 'deploy'
19+
- 'code-formatter'
20+
- '--image'
21+
- 'gcr.io/$PROJECT_ID/code-formatter:$COMMIT_SHA'
22+
- '--region'
23+
- 'europe-west4'
24+
- '--platform'
25+
- 'managed'
26+
27+
images:
28+
- 'gcr.io/$PROJECT_ID/code-formatter:$COMMIT_SHA'

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dev-community/code-formatter",
3-
"description": "A simple web app to format code",
3+
"description": "A simple web app to format code in XenForo forum posts",
44
"require": {
55
"php": ">=7.3"
66
},

composer.lock

Lines changed: 29 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
version: '3'
22

33
services:
4-
web:
5-
build:
6-
context: .
7-
dockerfile: .docker/web/Dockerfile
8-
image: dev-community-code-formatter_web
9-
ports:
10-
- 80:80
11-
volumes:
12-
- .:/srv/app
4+
web:
5+
build:
6+
context: .
7+
dockerfile: .docker/web/Dockerfile
8+
image: dev-community-code-formatter_web
9+
ports:
10+
- 8000:80
11+
volumes:
12+
- .:/srv/app
13+
environment:
14+
- API_KEY=abc

0 commit comments

Comments
 (0)