Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes #12

Merged
merged 8 commits into from
Jun 16, 2024
55 changes: 49 additions & 6 deletions app/Filament/Admin/Resources/DnsSettingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;

class DnsSettingResource extends Resource
{
Expand All @@ -26,12 +28,9 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('domain_id')
->required()
->numeric(),
Forms\Components\Select::make('record_type')
->options([
'A' => 'A',
'MX' => 'MX',
])
->required(),
Forms\Components\TextInput::make('record_type')
->required()
->maxLength(255),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Expand All @@ -41,6 +40,10 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('ttl')
->required()
->numeric(),
Forms\Components\TextInput::make('priority')
->numeric()
->visibleIf('record_type', 'MX')
->requiredIf('record_type', 'MX'),
]);
}

Expand All @@ -57,6 +60,9 @@ public static function table(Table $table): Table
->searchable(),
Tables\Columns\TextColumn::make('value')
->searchable(),
Tables\Columns\TextColumn::make('priority')
->numeric()
->visibleIf('record_type', 'MX'),
Tables\Columns\TextColumn::make('ttl')
->numeric()
->sortable(),
Expand Down Expand Up @@ -97,4 +103,41 @@ public static function getPages(): array
'edit' => Pages\EditDnsSetting::route('/{record}/edit'),
];
}

protected function updateBindDnsRecord(DnsSetting $dnsSetting): void
{
switch ($dnsSetting->record_type) {
case 'A':
$this->generateARecordEntry($dnsSetting);
break;
case 'MX':
$this->generateMxRecordEntry($dnsSetting);
break;
}

$this->restartBindContainer();
}

protected function generateARecordEntry(DnsSetting $dnsSetting): void
{
$entry = "{$dnsSetting->name} IN A {$dnsSetting->value}";
$zonePath = "/etc/bind/records/{$dnsSetting->domain->name}.db";

Storage::disk('bind')->append($zonePath, $entry);
}

protected function generateMxRecordEntry(DnsSetting $dnsSetting): void
{
$entry = "{$dnsSetting->name} IN MX {$dnsSetting->priority} {$dnsSetting->value}";
$zonePath = "/etc/bind/records/{$dnsSetting->domain->name}.db";

Storage::disk('bind')->append($zonePath, $entry);
}

protected function restartBindContainer(): void
{
$process = new Process(['docker-compose', 'restart', 'bind9']);
$process->setWorkingDirectory(base_path());
$process->run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ class CreateDomain extends CreateRecord

protected function handleRecordCreation(array $data): Domain
{
$domain = static::getModel()::create($data);
$user = auth()->user();
if ($user->hasReachedDockerComposeLimit()) {
throw new \Exception('You have reached the limit of Docker Compose instances for your hosting plan.');
}

$domain = static::getModel()::create([
...$data,
'hosting_plan_id' => $user->currentHostingPlan()->id,
]);

$composeContent = $this->generateDockerComposeContent($data);
Storage::disk('local')->put('docker-compose-'.$data['domain_name'].'.yml', $composeContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected function getHeaderActions(): array

protected function handleRecordUpdate(Domain $record, array $data): Domain
{
$user = auth()->user();
if ($user->hasReachedDockerComposeLimit()) {
throw new \Exception('You have reached the limit of Docker Compose instances for your hosting plan.');
}

$record->update($data);

$composeContent = $this->generateDockerComposeContent($data);
Expand Down
27 changes: 27 additions & 0 deletions app/Listeners/DnsSettingSavedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Listeners;

use App\Filament\Admin\Resources\DnsSettingResource;
use App\Models\DnsSetting;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DnsSettingSavedListener
{
/**
* Create the event listener.
*/
public function __construct(protected DnsSettingResource $dnsSettingResource)
{
//
}

/**
* Handle the event.
*/
public function handle(DnsSetting $dnsSetting): void
{
$this->dnsSettingResource->updateBindDnsRecord($dnsSetting);
}
}
23 changes: 1 addition & 22 deletions app/Models/DnsSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

namespace App\Models;

use App\Events\DnsSettingSaved;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DnsSetting extends Model
{
use HasFactory;

/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::saved(function ($dnsSetting) {
event(new DnsSettingSaved($dnsSetting));
});
}

/**
* The table associated with the model.
*
Expand All @@ -40,15 +27,7 @@ protected static function booted()
'name',
'value',
'ttl',
];

/**
* The validation rules for the model attributes.
*
* @var array
*/
public $rules = [
'record_type' => 'required|in:A,MX',
'priority',
];

/**
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Domain extends Model
'domain_name',
'registration_date',
'expiration_date',
'hosting_plan_id',
];

/**
Expand All @@ -46,6 +47,14 @@ public function user()
return $this->belongsTo(User::class);
}

/**
* Get the hosting plan associated with the domain.
*/
public function hostingPlan()
{
return $this->belongsTo(UserHostingPlan::class);
}

/**
* Get the email accounts for the domain.
*/
Expand Down
24 changes: 24 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ public function resourceUsages()
return $this->hasMany(ResourceUsage::class);
}

public function hasReachedDockerComposeLimit(): bool
{
$currentPlan = $this->currentHostingPlan();

if (!$currentPlan) {
return true; // Default to true if no hosting plan is found
}

if ($currentPlan->name === 'free') {
return $this->domains()->count() >= 1;
}

if ($currentPlan->name === 'premium') {
return false; // Unlimited instances for premium plan
}

return true; // Default to true for other plans
}

public function currentHostingPlan()
{
return $this->userHostingPlans()->latest()->first();
}



}
Expand Down
7 changes: 2 additions & 5 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class EventServiceProvider extends ServiceProvider
{
use App\Events\DnsSettingSaved;
use App\Listeners\UpdateBindDnsRecords;

/**
* The event to listener mappings for the application.
*
Expand All @@ -21,8 +18,8 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
DnsSettingSaved::class => [
UpdateBindDnsRecords::class,
DnsSetting::class => [
DnsSettingSavedListener::class,
],
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('domains', function (Blueprint $table) {
$table->id();
$table->string('domain_name');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('hosting_plan_id')->constrained('user_hosting_plans')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('domains');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddPriorityToDnsSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dns_settings', function (Blueprint $table) {
$table->integer('priority')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dns_settings', function (Blueprint $table) {
$table->dropColumn('priority');
});
}
}
3 changes: 3 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Database\Seeders\HostingPlanSeeder;

class DatabaseSeeder extends Seeder
{
Expand All @@ -19,5 +20,7 @@ public function run(): void
'name' => 'Test User',
'email' => 'test@example.com',
]);

$this->call(HostingPlanSeeder::class);
}
}
33 changes: 33 additions & 0 deletions database/seeders/HostingPlanSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\HostingPlan;

class HostingPlanSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
HostingPlan::create([
'name' => 'Free',
'description' => 'Free hosting plan with limited resources',
'disk_space' => 1000, // 1 GB
'bandwidth' => 10000, // 10 GB
'price' => 0.00,
]);

HostingPlan::create([
'name' => 'Premium',
'description' => 'Premium hosting plan with enhanced resources',
'disk_space' => 10000, // 10 GB
'bandwidth' => 100000, // 100 GB
'price' => 9.99,
]);
}
}
Loading