Skip to content

Commit

Permalink
Merge pull request #313 from EscolaLMS/feature/multi-domains
Browse files Browse the repository at this point in the history
Add multi domains
  • Loading branch information
qunabu authored Feb 22, 2024
2 parents 062c681 + e6a9ea3 commit b501995
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use EscolaLms\Webinar\Enum\WebinarTermReminderStatusEnum;
use EscolaLms\Webinar\Jobs\ReminderAboutWebinarJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Gecche\Multidomain\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Http\Middleware\ForceJsonResponse;
use App\Http\Middleware\ManualRoutesAuthorization;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Gecche\Multidomain\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;
use Gecche\Multidomain\Horizon\HorizonApplicationServiceProvider;

class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
Expand Down
10 changes: 8 additions & 2 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
|
*/

$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
$domainParams = [
'domain_detection_function_web' => function() {
return \Illuminate\Support\Arr::get($_SERVER,'HTTP_X_FORWARDED_HOST') ?? \Illuminate\Support\Arr::get($_SERVER,'HTTP_HOST');
}
];

$app = new Gecche\Multidomain\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__), null, $domainParams
);

/*
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"escolalms/video": "^0",
"escolalms/vouchers": "^0",
"escolalms/webinar": "^0",
"gecche/laravel-multidomain": "^5.0",
"guzzlehttp/guzzle": "^7.4.5",
"laravel/framework": "^9",
"laravel/helpers": "^1.7",
Expand Down
67 changes: 66 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Gecche\Multidomain\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Expand Down
25 changes: 25 additions & 0 deletions config/domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

return [
'env_stub' => '.env',
'storage_dirs' => [
'app' => [
'public' => [
],
],
'framework' => [
'cache' => [
],
'testing' => [
],
'sessions' => [
],
'views' => [
],
],
'logs' => [
],
],
'domains' => [
],
];
6 changes: 2 additions & 4 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'local' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'url' => env('APP_URL') . '/storage' . env('APP_PUBLIC_STORAGE'),
'visibility' => 'public',
],

Expand All @@ -60,7 +60,7 @@
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'url' => env('APP_URL') . '/storage' . env('APP_PUBLIC_STORAGE'),
'visibility' => 'public',
],

Expand All @@ -73,8 +73,6 @@
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),


],

],
Expand Down
11 changes: 11 additions & 0 deletions config/horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,16 @@
'balanceCooldown' => 3,
],
],

'stage' => [
'supervisor-1' => [
'maxProcesses' => 3,
],
'supervisor-long-job' => [
'connection' => 'redis-long-job',
'queue' => ['queue-long-job'],
'maxProcesses' => 1,
],
],
],
];
10 changes: 10 additions & 0 deletions docker/conf/supervisor/example/horizon.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[program:laravel-horizon-$HORIZON_DOMAIN]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan horizon --domain=$HORIZON_DOMAIN
user=devilbox
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stopwaitsecs=3600
logfile=/etc/supervisor/conf.d/horizon.log
10 changes: 10 additions & 0 deletions docker/conf/supervisor/example/scheduler.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[program:laravel-schedule-$SCHEDULER_DOMAIN]
process_name=%(program_name)s_%(process_num)02d
command=/bin/sh -c "while [ true ]; do (php /var/www/html/artisan schedule:run --domain=$SCHEDULER_DOMAIN --verbose --no-interaction &); sleep 60; done"
user=devilbox
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stopwaitsecs=3600
logfile=/etc/supervisor/conf.d/schedule.log
2 changes: 1 addition & 1 deletion docker/conf/supervisor/services/horizon.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[program:laravel-horizon]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan horizon
command=php /var/www/html/artisan horizon --domain=default
user=devilbox
autostart=true
autorestart=true
Expand Down
19 changes: 19 additions & 0 deletions docker/envs/envs.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ function add_env(string $key, string $new_value, string $env_path)
}
}
}

if (getenv('MULTI_DOMAINS')) {
$domains = explode(',', getenv('MULTI_DOMAINS'));

foreach ($domains as $domain) {
exec('php artisan domain:add ' . $domain);
$domainKey = str_replace(['.', '-'], '_', strtoupper($domain));
$domainVariables = [];
foreach (getenv() as $key => $value) {
if (str_starts_with($key, $domainKey)) {
$variableKey = str_replace($domainKey . '_', '', $key);
$domainVariables[$variableKey] = $value;
}
}

exec("php artisan domain:update_env $domain --domain_values='" . json_encode($domainVariables) ."'");
echo "Updated $domain env file\n";
}
}
70 changes: 70 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,73 @@ fi

php artisan storage:link --force --no-interaction
php artisan h5p:storage-link

# MULTI_DOMAINS
if [ -n "$MULTI_DOMAINS" ]; then
IFS=',' read -ra domains <<< "$MULTI_DOMAINS"

for domain in "${domains[@]}"; do
echo "$domain"

# horizon
if [ -z "$DISBALE_HORIZON" ] || [ "$DISBALE_HORIZON" != "true" ];
then
cp "docker/conf/supervisor/example/horizon.conf.example" "/etc/supervisor/custom.d/horizon.$domain.conf"
sed "s/\$HORIZON_DOMAIN/$domain/g" "docker/conf/supervisor/example/horizon.conf.example" > "/etc/supervisor/custom.d/horizon.$domain.conf"
else
echo "Horizon disabled"
fi
# scheduler
if [ -z "$DISBALE_SCHEDULER" ] || [ "$DISBALE_SCHEDULER" != "true" ];
then
cp "docker/conf/supervisor/example/scheduler.conf.example" "/etc/supervisor/custom.d/scheduler.$domain.conf"
sed "s/\$SCHEDULER_DOMAIN/$domain/g" "docker/conf/supervisor/example/scheduler.conf.example" > "/etc/supervisor/custom.d/scheduler.$domain.conf"
else
echo "Schedule disabled"
fi

DOMAIN_KEY=$(echo "$domain" | tr '[:lower:]' '[:upper:]')
DOMAIN_KEY=$(echo "$DOMAIN_KEY" | tr '.-' '__')

DB_NAME_KEY="${DOMAIN_KEY}_DB_DATABASE"
DB_NAME_VALUE=${!DB_NAME_KEY}

# create db if not exists
if PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$LARAVEL_DB_HOST" -U "$POSTGRES_USER" -d "$DB_NAME_VALUE" -c ";" >/dev/null 2>&1; then
echo "DB $DB_NAME_VALUE already exists"
else
PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$LARAVEL_DB_HOST" -U "$POSTGRES_USER" -c "CREATE DATABASE $DB_NAME_VALUE;"
echo "DB $DB_NAME_VALUE has been created"
fi

# db migrate
if [ "$DISBALE_DB_MIGRATE" == 'true' ]
then
echo "Disable db migrate"
else
php artisan migrate --force --domain=$domain
fi

# db seed
if [ "$DISBALE_DB_SEED" == 'true' ]
then
echo "Disable db:seed"
else
php artisan db:seed --domain=$domain --class=PermissionsSeeder --force --no-interaction
fi

# storage
STORAGE_DIRECTORY=$(echo "$domain" | tr '[:upper:]' '[:lower:]' | tr '.' '_')
STORAGE_PUBLIC_KEY="${DOMAIN_KEY}_APP_PUBLIC_STORAGE"
STORAGE_PUBLIC_NAME=${!STORAGE_PUBLIC_KEY}
if [ -n "$STORAGE_PUBLIC_NAME" ]; then
ln -s /var/www/html/storage/${STORAGE_DIRECTORY}/app/public public/storage${STORAGE_PUBLIC_NAME}
ln -s /var/www/html/storage/${STORAGE_DIRECTORY}/app/h5p public/h5p${STORAGE_PUBLIC_NAME}
fi

php artisan optimize:clear --domain=$domain

done
else
echo "Environment variable MULTI_DOMAINS is empty."
fi
20 changes: 2 additions & 18 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,8 @@
|
*/


Route::get('/debug-sentry', function () {
throw new \Exception('Test Sentry error!');
});

//TODO Removed after testing jitsi components
Route::group(['middleware' => ['auth:api']], function () {
Route::get('/seeds/consultations/{author?}/{user?}', function ($author = null, $user = null) {
$seed = new \EscolaLms\Consultations\Database\Seeders\ConsultationTermsSeeder($author, $user);
$consultationTerms = $seed->run();
return response()->json(['msg' => 'success']);
});
});

Route::get('/seeds/consultations/{author?}/{user?}', function ($author = null, $user = null) {
$seed = new \EscolaLms\Consultations\Database\Seeders\ConsultationTermsSeeder($author, $user);
$consultationTerms = $seed->run();
return response()->json(['msg' => 'success']);
Route::get('domain', function () {
return env('APP_NAME');
});

Route::get('events', [EventAPIController::class, 'index']);

0 comments on commit b501995

Please sign in to comment.