From 023ea06b49cc37f5778c62e17be2625b99c1a10c Mon Sep 17 00:00:00 2001 From: Rafael White Date: Thu, 1 Aug 2024 16:48:55 -0400 Subject: [PATCH 1/7] Remove frankenphp dockerfile and docker compose --- docker-compose.franken.yml | 132 ------------------------------------- franken.Dockerfile | 42 ------------ 2 files changed, 174 deletions(-) delete mode 100644 docker-compose.franken.yml delete mode 100644 franken.Dockerfile diff --git a/docker-compose.franken.yml b/docker-compose.franken.yml deleted file mode 100644 index 44271e4bf..000000000 --- a/docker-compose.franken.yml +++ /dev/null @@ -1,132 +0,0 @@ -services: - php: - container_name: php${APP_CONTAINER_NAME} - ports: - - "80:80" - build: - context: . - dockerfile: franken.Dockerfile - extra_hosts: - - 'host.docker.internal:host-gateway' - environment: - WWWUSER: '${WWWUSER}' - LARAVEL_SAIL: 1 - XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}' - XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}' - volumes: - - ./:/app - - caddy_data:/data - - caddy_config:/config - networks: - - sail - depends_on: - - mysql - - redis - mysql: - container_name: mysql${APP_CONTAINER_NAME} - image: 'mysql:8.3.0' - command: --innodb_strict_mode=0 --innodb_log_file_size=512M - ports: - - '${FORWARD_DB_PORT:-3306}:3306' - environment: - MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' - MYSQL_ROOT_HOST: "%" - MYSQL_DATABASE: '${DB_DATABASE}' - MYSQL_USER: '${DB_USERNAME}' - MYSQL_PASSWORD: '${DB_PASSWORD}' - MYSQL_ALLOW_EMPTY_PASSWORD: 1 - volumes: - - 'sail-mysql:/var/lib/mysql' - networks: - - sail - healthcheck: - test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ] - retries: 3 - timeout: 5s - redis: - container_name: redis${APP_CONTAINER_NAME} - image: 'redis:alpine' - ports: - - '${FORWARD_REDIS_PORT:-6379}:6379' - volumes: - - 'sail-redis:/data' - networks: - - sail - healthcheck: - test: [ "CMD", "redis-cli", "ping" ] - retries: 3 - timeout: 5s - - rabbitmq: - container_name: rabbitmq${APP_CONTAINER_NAME} - image: rabbitmq:3.9.21-management-alpine - hostname: "rabbit" - labels: - NAME: "rabbitmq1" - volumes: - - "sail-rabbitmq:/var/lib/rabbitmq" - healthcheck: - test: rabbitmq-diagnostics -q ping - interval: 30s - timeout: 30s - retries: 3 - ports: - - "15672:15672" - - "5672:5672" - networks: - - sail - mailpit: - image: 'axllent/mailpit:latest' - ports: - - '${FORWARD_MAILPIT_PORT:-1025}:1025' - - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' - networks: - - sail - minio1: - image: minio/minio - container_name: minio${APP_CONTAINER_NAME} - restart: always - environment: - MINIO_ACCESS_KEY: minio - MINIO_SECRET_KEY: ${AWS_SECRET_ACCESS_KEY} - command: server /data --console-address ":9001" - ports: - - "9002:9000" - - "9001:9001" - volumes: - - s3data:/data - networks: - - sail - meilisearch: - container_name: meilisearch - image: getmeili/meilisearch:v1.1 - environment: - - http_proxy - - https_proxy - - MEILI_MASTER_KEY=${MEILI_MASTER_KEY:-masterKey} - - MEILI_NO_ANALYTICS=${MEILI_NO_ANALYTICS:-true} - - MEILI_ENV=${MEILI_ENV:-development} - - MEILI_LOG_LEVEL - - MEILI_DB_PATH=${MEILI_DB_PATH:-/data.ms} - ports: - - ${MEILI_PORT:-7700}:7700 - networks: - - sail - volumes: - - meilisearch:/data.ms - restart: unless-stopped -networks: - sail: - driver: bridge -volumes: - pma: - sail-mysql: - driver: local - sail-redis: - driver: local - sail-rabbitmq: - driver: local - s3data: - meilisearch: - caddy_data: - caddy_config: diff --git a/franken.Dockerfile b/franken.Dockerfile deleted file mode 100644 index eb468c242..000000000 --- a/franken.Dockerfile +++ /dev/null @@ -1,42 +0,0 @@ -FROM dunglas/frankenphp - -ENV SERVER_NAME="http://" - -RUN apt-get update \ - && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ - build-essential \ - libpng-dev \ - libjpeg62-turbo-dev \ - libfreetype6-dev \ - locales \ - zip \ - jpegoptim optipng pngquant gifsicle \ - unzip \ - git \ - curl \ - lua-zlib-dev \ - libmemcached-dev \ - redis \ - vim - -RUN chmod +x /usr/local/bin/install-php-extensions && sync && \ - install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached redis swoole opcache curl readline sqlite3 msgpack igbinary pcov sockets - -COPY --from=composer:2 /usr/bin/composer /usr/bin/composer - -COPY Caddyfile /etc/caddy/Caddyfile - -COPY . /app - -WORKDIR /app - -RUN composer install --optimize-autoloader - -# COPY docker/weroad-php.ini $PHP_INI_DIR/conf.d/ -COPY docker/docker-php-ext-opcache.ini $PHP_INI_DIR/conf.d/ - -RUN cp docker/php.ini $PHP_INI_DIR/php.ini; \ - sed -i 's/variables_order = "GPCS"/variables_order = "EGPCS"/' $PHP_INI_DIR/php.ini; - -CMD /bin/bash -c "php artisan cache:clear \ - && frankenphp run --config /etc/caddy/Caddyfile --adapter caddyfile" \ No newline at end of file From 8c5f72db9132e82cc2ad131bbc2b472aafc77292 Mon Sep 17 00:00:00 2001 From: Rafael White Date: Thu, 1 Aug 2024 16:49:42 -0400 Subject: [PATCH 2/7] Add swoole package --- composer.json | 1 + composer.lock | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d11339f85..c1eecf08a 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,7 @@ "spatie/laravel-queueable-action": "^2.15", "spatie/laravel-webhook-server": "^3.8", "stripe/stripe-php": "^15.0", + "swoole/ide-helper": "*", "symfony/expression-language": "^7.0", "symfony/http-client": "^7.0", "symfony/mailgun-mailer": "^7.0", diff --git a/composer.lock b/composer.lock index a3b595528..653d7d2e7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a3341b6c7ce247b61dd696be4603ceec", + "content-hash": "6ef8502e152f3ff364c7d6a29ed42aa1", "packages": [ { "name": "algolia/algoliasearch-client-php", @@ -9619,6 +9619,38 @@ }, "time": "2024-07-25T21:00:20+00:00" }, + { + "name": "swoole/ide-helper", + "version": "5.1.3", + "source": { + "type": "git", + "url": "https://github.com/swoole/ide-helper.git", + "reference": "9cfc6669b83be0fa6fface91a6f372a0bb84bf1a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/swoole/ide-helper/zipball/9cfc6669b83be0fa6fface91a6f372a0bb84bf1a", + "reference": "9cfc6669b83be0fa6fface91a6f372a0bb84bf1a", + "shasum": "" + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Team Swoole", + "email": "team@swoole.com" + } + ], + "description": "IDE help files for Swoole.", + "support": { + "issues": "https://github.com/swoole/ide-helper/issues", + "source": "https://github.com/swoole/ide-helper/tree/5.1.3" + }, + "time": "2024-06-17T05:45:20+00:00" + }, { "name": "symfony/cache", "version": "v7.1.3", From 38cb13ae4ab66d19ff9d65d1e1b6743d67b263f3 Mon Sep 17 00:00:00 2001 From: Rafael White Date: Thu, 1 Aug 2024 16:52:44 -0400 Subject: [PATCH 3/7] update laravel octane section --- README.md | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/README.md b/README.md index a03058116..46764f1b0 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ DB_CRM_PASSWORD=password ## Running the project with Laravel Octane -After doing all the steps above, you could run the project with Laravel Octane by using the command ```php artisan octane:start --port 8080 --host=0.0.0.0```. +After doing all the steps above, you could run the project with Laravel Octane by using the command ```php artisan octane:start --server=swoole --host=0.0.0.0 --port=8000```. Use `--watch` in development allowing you to refresh modified files, this works assuming to have `npm install chokidar` installed in the project. **** @@ -116,12 +116,3 @@ Note: - To install Swoole you can use the command ```pecl install swoole``` - For production remove `--watch` from the command. - roles_kanvas_legacy will be deleted in the future. - - -## Feature Labs - -### Run the project using FrankenPHP - -``` sh -docker compose -f docker-compose.franken.yml up -d --build -``` From 8a45faabbcf17dfe69f784eff89187660ed4b58d Mon Sep 17 00:00:00 2001 From: Rafael White Date: Thu, 1 Aug 2024 16:53:11 -0400 Subject: [PATCH 4/7] Change port to 8000 to support laravel octane --- docker/nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/nginx.conf b/docker/nginx.conf index 4b4948e35..005b27f40 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -17,7 +17,7 @@ server { return 204; } - proxy_pass http://php:8080; + proxy_pass http://php:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; From ed62628ae7e3082f42c4a9e49307658df3c54ac6 Mon Sep 17 00:00:00 2001 From: Rafael White Date: Thu, 1 Aug 2024 17:02:18 -0400 Subject: [PATCH 5/7] Move App Key check on middleware --- app/Http/Middleware/KanvasAppKey.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/Http/Middleware/KanvasAppKey.php b/app/Http/Middleware/KanvasAppKey.php index 57bbbda67..caf1cb8a4 100644 --- a/app/Http/Middleware/KanvasAppKey.php +++ b/app/Http/Middleware/KanvasAppKey.php @@ -9,10 +9,14 @@ use Illuminate\Support\Facades\Auth; use Kanvas\Apps\Models\AppKey; use Kanvas\Apps\Models\Apps; +use Kanvas\Apps\Repositories\AppsRepository; +use Kanvas\Apps\Support\MountedAppProvider; use Kanvas\Companies\Models\CompaniesBranches; use Kanvas\Enums\AppEnums; use Symfony\Component\HttpFoundation\Response; use Throwable; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Kanvas\Exceptions\InternalServerErrorException; class KanvasAppKey { @@ -23,6 +27,19 @@ class KanvasAppKey */ public function handle(Request $request, Closure $next): Response { + $appIdentifier = $request->header(AppEnums::KANVAS_APP_HEADER->getValue(), config('kanvas.app.id')); + + try { + $app = AppsRepository::findFirstByKey($appIdentifier); + + (new MountedAppProvider($app))->register(); + } catch (ModelNotFoundException $e) { + throw new InternalServerErrorException( + 'No App configure with this key: ' . $appIdentifier, + $e->getMessage() + ); + } + $companyBranchHeader = AppEnums::KANVAS_APP_BRANCH_HEADER->getValue(); $appKeyHeader = AppEnums::KANVAS_APP_KEY_HEADER->getValue(); @@ -74,7 +91,6 @@ public function handle(Request $request, Closure $next): Response } } - return $next($request); } -} +} \ No newline at end of file From e80de5d6122d2c8401ca407fba02b4c7b35e5edc Mon Sep 17 00:00:00 2001 From: kaioken Date: Thu, 1 Aug 2024 17:34:46 -0400 Subject: [PATCH 6/7] refact: app kanvas key --- app/Http/Middleware/KanvasAppKey.php | 45 ++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/app/Http/Middleware/KanvasAppKey.php b/app/Http/Middleware/KanvasAppKey.php index e38b45c2a..0204120e1 100644 --- a/app/Http/Middleware/KanvasAppKey.php +++ b/app/Http/Middleware/KanvasAppKey.php @@ -16,8 +16,6 @@ use Kanvas\Enums\AppEnums; use Symfony\Component\HttpFoundation\Response; use Throwable; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Kanvas\Exceptions\InternalServerErrorException; class KanvasAppKey { @@ -25,17 +23,30 @@ public function handle(Request $request, Closure $next): Response { $appIdentifier = $request->header(AppEnums::KANVAS_APP_HEADER->getValue(), config('kanvas.app.id')); + if (! $this->registerApp($appIdentifier)) { + return response()->json(['message' => 'No App configured with this key: ' . $appIdentifier], 500); + } + + $this->handleCompanyBranch($request); + $this->handleAppKey($request); + + return $next($request); + } + + private function registerApp(string $appIdentifier): bool + { try { $app = AppsRepository::findFirstByKey($appIdentifier); - (new MountedAppProvider($app))->register(); + + return true; } catch (ModelNotFoundException $e) { - throw new InternalServerErrorException( - 'No App configure with this key: ' . $appIdentifier, - $e->getMessage() - ); + return false; } - + } + + private function handleCompanyBranch(Request $request): void + { $companyBranchHeader = AppEnums::KANVAS_APP_BRANCH_HEADER->getValue(); if ($request->hasHeader($companyBranchHeader)) { @@ -77,6 +88,22 @@ private function handleAppKey(Request $request): void } } - return $next($request); + private function scopeAppKeyAndApp(AppKey $kanvasAppKey, Apps $kanvasApp): void + { + app()->scoped(AppKey::class, fn () => $kanvasAppKey); + app()->scoped(Apps::class, fn () => $kanvasApp); + } + + private function setUserIfNoBearerToken(Request $request, AppKey $kanvasAppKey): void + { + if (empty($request->bearerToken())) { + Auth::setUser($kanvasAppKey->user()->firstOrFail()); + } + } + + private function updateLastUsedDate(AppKey $kanvasAppKey): void + { + $kanvasAppKey->last_used_date = now(); + $kanvasAppKey->saveOrFail(); } } \ No newline at end of file From 274b7186beee0c602555dae6db5372a05076266a Mon Sep 17 00:00:00 2001 From: kaioken Date: Thu, 1 Aug 2024 17:36:15 -0400 Subject: [PATCH 7/7] refact: app kanvas key --- app/Http/Middleware/KanvasAppKey.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Middleware/KanvasAppKey.php b/app/Http/Middleware/KanvasAppKey.php index 0204120e1..5c0fa64e1 100644 --- a/app/Http/Middleware/KanvasAppKey.php +++ b/app/Http/Middleware/KanvasAppKey.php @@ -106,4 +106,4 @@ private function updateLastUsedDate(AppKey $kanvasAppKey): void $kanvasAppKey->last_used_date = now(); $kanvasAppKey->saveOrFail(); } -} \ No newline at end of file +}