Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Wedrix committed Mar 30, 2024
0 parents commit 8b5fa54
Show file tree
Hide file tree
Showing 97 changed files with 8,530 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
**/*.log
**/*.md
**/*.php~
**/*.dist.php
**/*.dist
**/*.cache
**/._*
**/.dockerignore
**/.DS_Store
**/.git/
**/.gitattributes
**/.gitignore
**/.gitmodules
**/compose.*.yaml
**/compose.*.yml
**/compose.yaml
**/compose.yml
**/docker-compose.*.yaml
**/docker-compose.*.yml
**/docker-compose.yaml
**/docker-compose.yml
**/Dockerfile
**/Thumbs.db
.github/
docs/
public/bundles/
tests/
var/
vendor/
.editorconfig
.env.*.local
.env.local
.env.local.php
.env.test
58 changes: 58 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{js,html,ts,tsx}]
indent_size = 2

[*.json]
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.sh]
indent_style = tab

[*.xml{,.dist}]
indent_style = space
indent_size = 4

[*.{yaml,yml}]
trim_trailing_whitespace = false

[.github/workflows/*.yml]
indent_size = 2

[.gitmodules]
indent_style = tab

[.php_cs{,.dist}]
indent_style = space
indent_size = 4

[composer.json]
indent_size = 4

[{,docker-}compose{,.*}.{yaml,yml}]
indent_style = space
indent_size = 2

[{,*.*}Dockerfile]
indent_style = tab

[{,*.*}Caddyfile]
indent_style = tab
36 changes: 36 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
# https://symfony.com/doc/current/configuration/secrets.html
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=17df7ebe0411c0d99a58c75a079074eb
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8&charset=utf8mb4"
DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=16&charset=utf8"
###< doctrine/doctrine-bundle ###

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
###< nelmio/cors-bundle ###

APP_DOMAIN=https://localhost
AUTH_TOKEN_HEADER=X-AUTH-TOKEN
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
* text=auto eol=lf

*.conf text eol=lf
*.html text eol=lf
*.ini text eol=lf
*.js text eol=lf
*.json text eol=lf
*.md text eol=lf
*.php text eol=lf
*.sh text eol=lf
*.yaml text eol=lf
*.yml text eol=lf
bin/console text eol=lf
composer.lock text eol=lf merge=ours

*.ico binary
*.png binary
72 changes: 72 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: CI

on:
push:
branches:
- main
pull_request: ~
workflow_dispatch: ~

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
tests:
name: Tests
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build Docker images
uses: docker/bake-action@v4
with:
pull: true
load: true
files: |
compose.yaml
compose.override.yaml
set: |
*.cache-from=type=gha,scope=${{github.ref}}
*.cache-from=type=gha,scope=refs/heads/main
*.cache-to=type=gha,scope=${{github.ref}},mode=max
-
name: Start services
run: docker compose up --wait --no-build
-
name: Check HTTP reachability
run: curl -v -o /dev/null http://localhost
-
name: Check HTTPS reachability
run: curl -vk -o /dev/null https://localhost
-
name: Create test database
if: false # Remove this line if Doctrine ORM is installed
run: docker compose exec -T php bin/console -e test doctrine:database:create
-
name: Run migrations
if: false # Remove this line if Doctrine Migrations is installed
run: docker compose exec -T php bin/console -e test doctrine:migrations:migrate --no-interaction
-
name: Run PHPUnit
if: false # Remove this line if PHPUnit is installed
run: docker compose exec -T php bin/phpunit
-
name: Doctrine Schema Validator
if: false # Remove this line if Doctrine ORM is installed
run: docker compose exec -T php bin/console -e test doctrine:schema:validate
lint:
name: Docker Lint
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Lint Dockerfile
uses: hadolint/[email protected]
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

###> symfony/framework-bundle ###
/.env.local
/.env.local.php
/.env.*.local
/config/secrets/prod/prod.decrypt.private.php
/public/bundles/
/var/
/vendor/
###< symfony/framework-bundle ###
.DS_Store
97 changes: 97 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#syntax=docker/dockerfile:1.4

# Versions
FROM dunglas/frankenphp:latest-php8.2-alpine AS frankenphp_upstream

# The different stages of this Dockerfile are meant to be built into separate images
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
# https://docs.docker.com/compose/compose-file/#target


# Base FrankenPHP image
FROM frankenphp_upstream AS frankenphp_base

WORKDIR /app

# persistent / runtime deps
# hadolint ignore=DL3018
RUN apk add --no-cache \
acl \
file \
gettext \
git \
;

RUN set -eux; \
install-php-extensions \
@composer \
apcu \
intl \
opcache \
zip \
;

# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1

###> recipes ###
###> doctrine/doctrine-bundle ###
RUN apk add --no-cache --virtual .pgsql-deps postgresql-dev; \

Check failure on line 39 in Dockerfile

View workflow job for this annotation

GitHub Actions / Docker Lint

DL3018 warning: Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`

Check failure on line 39 in Dockerfile

View workflow job for this annotation

GitHub Actions / Docker Lint

SC2046 warning: Quote this to prevent word splitting.
docker-php-ext-install -j$(nproc) pdo_pgsql; \
apk add --no-cache --virtual .pgsql-rundeps so:libpq.so.5; \
apk del .pgsql-deps
###< doctrine/doctrine-bundle ###
###< recipes ###

COPY --link frankenphp/conf.d/app.ini $PHP_INI_DIR/conf.d/
COPY --link --chmod=755 frankenphp/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
COPY --link frankenphp/Caddyfile /etc/caddy/Caddyfile

ENTRYPOINT ["docker-entrypoint"]

HEALTHCHECK --start-period=60s CMD curl -f http://localhost:2019/metrics || exit 1
CMD [ "frankenphp", "run", "--config", "/etc/caddy/Caddyfile" ]

# Dev FrankenPHP image
FROM frankenphp_base AS frankenphp_dev

ENV APP_ENV=dev XDEBUG_MODE=off
VOLUME /app/var/

RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

RUN set -eux; \
install-php-extensions \
xdebug \
;

COPY --link frankenphp/conf.d/app.dev.ini $PHP_INI_DIR/conf.d/

CMD [ "frankenphp", "run", "--config", "/etc/caddy/Caddyfile", "--watch" ]

# Prod FrankenPHP image
FROM frankenphp_base AS frankenphp_prod

ENV APP_ENV=prod
ENV FRANKENPHP_CONFIG="import worker.Caddyfile"

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

COPY --link frankenphp/conf.d/app.prod.ini $PHP_INI_DIR/conf.d/
COPY --link frankenphp/worker.Caddyfile /etc/caddy/worker.Caddyfile

# prevent the reinstallation of vendors at every changes in the source code
COPY --link composer.* symfony.* ./
RUN set -eux; \
composer install --no-cache --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress

# copy sources
COPY --link . ./
RUN rm -Rf frankenphp/

RUN set -eux; \
mkdir -p var/cache var/log; \
composer dump-autoload --classmap-authoritative --no-dev; \
composer dump-env prod; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync;
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Simple Order Service

**Author:** James Wedam Anewenah

**Stack:** [Symfony](https://symfony.com/) / [GraphQL](https://github.com/Wedrix/watchtower-symfony-bundle)

**Templates:** [Symfony Docker](https://github.com/dunglas/symfony-docker)

## Project Setup

1. If not already done, [install Docker Compose](https://docs.docker.com/compose/install/) (v2.10+)

2. Run `docker compose build --no-cache` to build fresh images

3. Run `SYMFONY_VERSION=6.1.* docker compose up --pull always -d --wait` to start the project

4. Run `docker exec -d replace-with-container-name bin/console doctrine:schema:create` to migrate the database. Remember to replace the actual container name.

5. Open `https://localhost` in Chrome and [accept the auto-generated TLS certificate](https://youtu.be/7J3vSN3pCjI?si=kOxw6vNHPmyptSto).

6. Interact with the API using an appropriate browser-based GraphQL client like [GraphiQL](https://chromewebstore.google.com/detail/graphiql-extension/jhbedfdjpmemmbghfecnaeeiokonjclb).

7. The API endpoint is `https://localhost/graphql.json`

8. Authentication is done using tokens. The response after signing up/signing in contains an `X-Auth-Token` header with the authentication token. You can view the response header by [accessing chrome's dev-tools](https://www.youtube.com/watch?v=hqQR1O2H_ck). Pass the token header with subsequent requests to authenticate them. The image below demonstrates how to do this with the aforementioned [GraphiQL](https://chromewebstore.google.com/detail/graphiql-extension/jhbedfdjpmemmbghfecnaeeiokonjclb) client.

9. Run `docker compose down --remove-orphans` to stop the Docker containers.

10. It is recommended to [install Docker Desktop](https://www.docker.com/products/docker-desktop/) for easy viewing/management of containers.

![Demo Image](https://github.com/wedrix/watchtower_order_service/blob/main/demo.png?raw=true)

## API Description

### Scalars

- Limit - Integer from **1** to **100**
- Page - Integer greater than or equal to **1**
- EmailAddress - Email address
- Name - String consisting of only chars or spaces
- Password - String of length greater than or equal to **8**
- Price - Decimal String with two-point precision in range **0.00 - 1000000000.00**
- ProductName - String consisting of only chars, digits, or spaces
- Role - Either **ROLE_USER** or **ROLE_ADMIN**
- Stock - Integer greater than or equal to **0** but less than **1000000**
17 changes: 17 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env php
<?php

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

return new Application($kernel);
};
Loading

0 comments on commit 8b5fa54

Please sign in to comment.