From c4a4801414f0febe2ed8eb260a2dcbef06f9b140 Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Thu, 4 May 2023 22:29:14 +0200
Subject: [PATCH] fix modal, add env variables, etc
---
.../Application/EnvironmentVariable.php | 48 -----------------
.../Application/EnvironmentVariable/Add.php | 43 +++++++++++++++
.../Application/EnvironmentVariable/Show.php | 35 ++++++++++++
app/Models/Application.php | 15 ++----
app/Models/EnvironmentVariable.php | 46 ++++++++++++++++
...03_27_081716_create_applications_table.php | 2 -
...548_create_environment_variables_table.php | 39 ++++++++++++++
database/seeders/ApplicationSeeder.php | 10 ----
database/seeders/DatabaseSeeder.php | 2 +
.../seeders/EnvironmentVariableSeeder.php | 22 ++++++++
.../views/components/inputs/input.blade.php | 3 +-
resources/views/components/layout.blade.php | 3 ++
.../views/components/naked-modal.blade.php | 19 +++++++
resources/views/errors/401.blade.php | 5 ++
resources/views/errors/402.blade.php | 5 ++
resources/views/errors/403.blade.php | 5 ++
resources/views/errors/404.blade.php | 5 ++
resources/views/errors/419.blade.php | 5 ++
resources/views/errors/429.blade.php | 5 ++
resources/views/errors/500.blade.php | 5 ++
resources/views/errors/503.blade.php | 5 ++
resources/views/errors/layout.blade.php | 53 +++++++++++++++++++
resources/views/errors/minimal.blade.php | 34 ++++++++++++
.../environment-variable.blade.php | 28 ----------
.../environment-variable/add.blade.php | 13 +++++
.../environment-variable/show.blade.php | 19 +++++++
.../application/configuration.blade.php | 7 +--
27 files changed, 379 insertions(+), 102 deletions(-)
delete mode 100644 app/Http/Livewire/Project/Application/EnvironmentVariable.php
create mode 100644 app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php
create mode 100644 app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php
create mode 100644 app/Models/EnvironmentVariable.php
create mode 100644 database/migrations/2023_05_04_194548_create_environment_variables_table.php
create mode 100644 database/seeders/EnvironmentVariableSeeder.php
create mode 100644 resources/views/components/naked-modal.blade.php
create mode 100644 resources/views/errors/401.blade.php
create mode 100644 resources/views/errors/402.blade.php
create mode 100644 resources/views/errors/403.blade.php
create mode 100644 resources/views/errors/404.blade.php
create mode 100644 resources/views/errors/419.blade.php
create mode 100644 resources/views/errors/429.blade.php
create mode 100644 resources/views/errors/500.blade.php
create mode 100644 resources/views/errors/503.blade.php
create mode 100644 resources/views/errors/layout.blade.php
create mode 100644 resources/views/errors/minimal.blade.php
delete mode 100644 resources/views/livewire/project/application/environment-variable.blade.php
create mode 100644 resources/views/livewire/project/application/environment-variable/add.blade.php
create mode 100644 resources/views/livewire/project/application/environment-variable/show.blade.php
diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable.php b/app/Http/Livewire/Project/Application/EnvironmentVariable.php
deleted file mode 100644
index aeb15c878..000000000
--- a/app/Http/Livewire/Project/Application/EnvironmentVariable.php
+++ /dev/null
@@ -1,48 +0,0 @@
-parameters = Route::current()->parameters();
- if (data_get($this->env, 'value') !== null) {
- $this->value = $this->env['value'];
- $this->isBuildOnly = $this->env['isBuildOnly'];
- } else {
- $this->isNewEnv = true;
- }
- }
- public function updateEnv()
- {
- $application = Application::where('uuid', $this->parameters['application_uuid'])->first();
- $application->environment_variables->set("{$this->keyName}.value", $this->value);
- $application->environment_variables->set("{$this->keyName}.isBuildOnly", $this->isBuildOnly);
- $application->save();
- }
- public function submit()
- {
- $this->updateEnv();
- $this->emit('reloadWindow');
- }
- public function delete()
- {
- $application = Application::where('uuid', $this->parameters['application_uuid'])->first();
- $application->environment_variables->forget($this->keyName);
- $application->save();
- $this->emit('reloadWindow');
- }
-}
diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php
new file mode 100644
index 000000000..c6bd504f5
--- /dev/null
+++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php
@@ -0,0 +1,43 @@
+parameters = Route::current()->parameters();
+ }
+ public function submit()
+ {
+ try {
+ $application_id = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail()->id;
+ EnvironmentVariable::create([
+ 'key' => $this->key,
+ 'value' => $this->value,
+ 'is_build_time' => $this->is_build_time,
+ 'application_id' => $application_id,
+ ]);
+ $this->emit('reloadWindow');
+ } catch (mixed $e) {
+ dd('asdf');
+ if ($e instanceof QueryException) {
+ dd($e->errorInfo);
+ $this->emit('error', $e->errorInfo[2]);
+ } else {
+ $this->emit('error', $e);
+ }
+ }
+ }
+}
diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php
new file mode 100644
index 000000000..f32260ca0
--- /dev/null
+++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php
@@ -0,0 +1,35 @@
+ 'required|string',
+ 'env.value' => 'required|string',
+ 'env.is_build_time' => 'required|boolean',
+ ];
+ public function mount()
+ {
+ $this->parameters = Route::current()->parameters();
+ }
+ public function submit()
+ {
+ $this->validate();
+ $this->env->save();
+ }
+ public function delete()
+ {
+ $this->env->delete();
+ $this->emit('reloadWindow');
+ }
+}
diff --git a/app/Models/Application.php b/app/Models/Application.php
index c63e6d6d0..8e518c417 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -4,8 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Spatie\Activitylog\Models\Activity;
-use Illuminate\Database\Eloquent\Builder;
-use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
+use Illuminate\Database\Eloquent\Relations\HasMany;
class Application extends BaseModel
{
@@ -38,14 +37,6 @@ class Application extends BaseModel
'ports_exposes',
'publish_directory',
];
- public $casts = [
- 'environment_variables' => SchemalessAttributes::class,
- ];
- public function scopeWithEnvironmentVariables(): Builder
- {
- return $this->environment_variables->modelScope();
- }
-
public function publishDirectory(): Attribute
{
return Attribute::make(
@@ -83,6 +74,10 @@ class Application extends BaseModel
: explode(',', $this->ports_exposes)
);
}
+ public function environment_variables(): HasMany
+ {
+ return $this->hasMany(EnvironmentVariable::class);
+ }
public function environment()
{
return $this->belongsTo(Environment::class);
diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php
new file mode 100644
index 000000000..5c61fca74
--- /dev/null
+++ b/app/Models/EnvironmentVariable.php
@@ -0,0 +1,46 @@
+ 'encrypted',
+ 'is_build_time' => 'boolean',
+ ];
+ private function get_environment_variables(string $environment_variable): string|null
+ {
+ $team_id = session('currentTeam')->id;
+ if (str_contains(trim($environment_variable), '{{') && str_contains(trim($environment_variable), '}}')) {
+ $environment_variable = preg_replace('/\s+/', '', $environment_variable);
+ $environment_variable = str_replace('{{', '', $environment_variable);
+ $environment_variable = str_replace('}}', '', $environment_variable);
+ if (str_starts_with($environment_variable, 'global.')) {
+ $environment_variable = str_replace('global.', '', $environment_variable);
+ // $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
+ return $environment_variable;
+ }
+ }
+ return decrypt($environment_variable);
+ }
+ private function set_environment_variables(string $environment_variable): string|null
+ {
+ if (!str_contains(trim($environment_variable), '{{') && !str_contains(trim($environment_variable), '}}')) {
+ return encrypt($environment_variable);
+ }
+ return $environment_variable;
+ }
+ protected function value(): Attribute
+ {
+ return Attribute::make(
+ get: fn (string $value) => $this->get_environment_variables($value),
+ set: fn (string $value) => $this->set_environment_variables($value),
+ );
+ }
+}
diff --git a/database/migrations/2023_03_27_081716_create_applications_table.php b/database/migrations/2023_03_27_081716_create_applications_table.php
index 462b58e6f..b408589d2 100644
--- a/database/migrations/2023_03_27_081716_create_applications_table.php
+++ b/database/migrations/2023_03_27_081716_create_applications_table.php
@@ -39,8 +39,6 @@ return new class extends Migration
$table->string('base_directory')->default('/');
$table->string('publish_directory')->nullable();
- $table->schemalessAttributes('environment_variables');
-
$table->string('health_check_path')->default('/');
$table->string('health_check_port')->nullable();
$table->string('health_check_host')->default('localhost');
diff --git a/database/migrations/2023_05_04_194548_create_environment_variables_table.php b/database/migrations/2023_05_04_194548_create_environment_variables_table.php
new file mode 100644
index 000000000..758e74227
--- /dev/null
+++ b/database/migrations/2023_05_04_194548_create_environment_variables_table.php
@@ -0,0 +1,39 @@
+id();
+
+ $table->string('key');
+ $table->string('value')->nullable();
+ $table->boolean('is_build_time')->default(false);
+
+ $table->foreignId('application_id')->nullable();
+ $table->foreignId('service_id')->nullable();
+ $table->foreignId('database_id')->nullable();
+
+ $table->unique(['key', 'application_id', 'is_build_time']);
+ $table->unique(['key', 'service_id', 'is_build_time']);
+ $table->unique(['key', 'database_id', 'is_build_time']);
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::dropIfExists('environment_variables');
+ }
+};
diff --git a/database/seeders/ApplicationSeeder.php b/database/seeders/ApplicationSeeder.php
index 3f17d6ee6..2a03a7911 100644
--- a/database/seeders/ApplicationSeeder.php
+++ b/database/seeders/ApplicationSeeder.php
@@ -35,16 +35,6 @@ class ApplicationSeeder extends Seeder
'destination_type' => StandaloneDocker::class,
'source_id' => $github_public_source->id,
'source_type' => GithubApp::class,
- 'environment_variables' => [
- 'NODE_ENV' => [
- 'value' => 'production',
- 'isBuildOnly' => true,
- ],
- 'PORT' => [
- 'value' => 3000,
- 'isBuildOnly' => false,
- ]
- ]
]);
}
}
diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php
index 9e1e78669..a45d8462e 100644
--- a/database/seeders/DatabaseSeeder.php
+++ b/database/seeders/DatabaseSeeder.php
@@ -2,6 +2,7 @@
namespace Database\Seeders;
+use App\Models\Environment;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
@@ -27,6 +28,7 @@ class DatabaseSeeder extends Seeder
ApplicationSettingsSeeder::class,
DBSeeder::class,
ServiceSeeder::class,
+ EnvironmentVariableSeeder::class,
LocalPersistentVolumeSeeder::class,
]);
}
diff --git a/database/seeders/EnvironmentVariableSeeder.php b/database/seeders/EnvironmentVariableSeeder.php
new file mode 100644
index 000000000..d49e97fc2
--- /dev/null
+++ b/database/seeders/EnvironmentVariableSeeder.php
@@ -0,0 +1,22 @@
+ 'NODE_ENV',
+ 'value' => 'production',
+ 'is_build_time' => true,
+ 'application_id' => 1,
+ ]);
+ }
+}
diff --git a/resources/views/components/inputs/input.blade.php b/resources/views/components/inputs/input.blade.php
index 9f04097c4..975e59f8d 100644
--- a/resources/views/components/inputs/input.blade.php
+++ b/resources/views/components/inputs/input.blade.php
@@ -26,7 +26,8 @@
@if ($type === 'textarea')
@else
-
@endif
diff --git a/resources/views/components/layout.blade.php b/resources/views/components/layout.blade.php
index 258df343c..b9ba8c3a9 100644
--- a/resources/views/components/layout.blade.php
+++ b/resources/views/components/layout.blade.php
@@ -76,6 +76,9 @@
Livewire.on('reloadWindow', () => {
window.location.reload();
})
+ Livewire.on('error', (message) => {
+ alert(message);
+ })
@endauth
+
+
+
+ @yield('message')
+
+
+
+
diff --git a/resources/views/components/naked-modal.blade.php b/resources/views/components/naked-modal.blade.php
new file mode 100644
index 000000000..b78c09b3e
--- /dev/null
+++ b/resources/views/components/naked-modal.blade.php
@@ -0,0 +1,19 @@
+@props([
+ 'show' => null,
+ 'message' => 'Are you sure you want to delete this?',
+ 'action' => 'delete',
+])
+
+
+
+
+
{{ $message }}
+
+
+ Yes
+
+ No
+
+
+
+
diff --git a/resources/views/errors/401.blade.php b/resources/views/errors/401.blade.php
new file mode 100644
index 000000000..5c586db96
--- /dev/null
+++ b/resources/views/errors/401.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Unauthorized'))
+@section('code', '401')
+@section('message', __('Unauthorized'))
diff --git a/resources/views/errors/402.blade.php b/resources/views/errors/402.blade.php
new file mode 100644
index 000000000..3bc23efd2
--- /dev/null
+++ b/resources/views/errors/402.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Payment Required'))
+@section('code', '402')
+@section('message', __('Payment Required'))
diff --git a/resources/views/errors/403.blade.php b/resources/views/errors/403.blade.php
new file mode 100644
index 000000000..a5506f01f
--- /dev/null
+++ b/resources/views/errors/403.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Forbidden'))
+@section('code', '403')
+@section('message', __($exception->getMessage() ?: 'Forbidden'))
diff --git a/resources/views/errors/404.blade.php b/resources/views/errors/404.blade.php
new file mode 100644
index 000000000..7549540d8
--- /dev/null
+++ b/resources/views/errors/404.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Not Found'))
+@section('code', '404')
+@section('message', __('Not Found'))
diff --git a/resources/views/errors/419.blade.php b/resources/views/errors/419.blade.php
new file mode 100644
index 000000000..c09216e21
--- /dev/null
+++ b/resources/views/errors/419.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Page Expired'))
+@section('code', '419')
+@section('message', __('Page Expired'))
diff --git a/resources/views/errors/429.blade.php b/resources/views/errors/429.blade.php
new file mode 100644
index 000000000..f01b07b8e
--- /dev/null
+++ b/resources/views/errors/429.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Too Many Requests'))
+@section('code', '429')
+@section('message', __('Too Many Requests'))
diff --git a/resources/views/errors/500.blade.php b/resources/views/errors/500.blade.php
new file mode 100644
index 000000000..d9e95d9b9
--- /dev/null
+++ b/resources/views/errors/500.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Server Error'))
+@section('code', '500')
+@section('message', __('Server Error'))
diff --git a/resources/views/errors/503.blade.php b/resources/views/errors/503.blade.php
new file mode 100644
index 000000000..c5a9dde14
--- /dev/null
+++ b/resources/views/errors/503.blade.php
@@ -0,0 +1,5 @@
+@extends('errors::minimal')
+
+@section('title', __('Service Unavailable'))
+@section('code', '503')
+@section('message', __('Service Unavailable'))
diff --git a/resources/views/errors/layout.blade.php b/resources/views/errors/layout.blade.php
new file mode 100644
index 000000000..019c2cde1
--- /dev/null
+++ b/resources/views/errors/layout.blade.php
@@ -0,0 +1,53 @@
+
+
+
+
+
+
+
@yield('title')
+
+
+
+
+
+