ConnectionTest im Backend funktioniert jetzt

This commit is contained in:
2025-08-07 14:34:00 +02:00
parent 573661825b
commit ad893b48a7
21 changed files with 580 additions and 109 deletions

View File

@@ -17,9 +17,15 @@ use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\Select;
use Illuminate\Support\Facades\File;
use App\Api\Plugins\ApiPluginInterface;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\IconColumn;
use Filament\Forms\Components\Actions;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\ViewField;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;
class ApiProviderResource extends Resource
{
@@ -30,41 +36,130 @@ class ApiProviderResource extends Resource
public static function form(Form $form): Form
{
$plugins = self::getAvailablePlugins();
$livewire = $form->getLivewire();
return $form
->schema([
TextInput::make('name')
->label(__('filament.resource.api_provider.form.name'))
->required()
->maxLength(255),
Toggle::make('enabled')
->label(__('filament.resource.api_provider.form.enabled'))
->default(true),
TextInput::make('api_url')
->label(__('filament.resource.api_provider.form.api_url'))
->required()
->url()
->maxLength(255),
TextInput::make('username')
->label(__('filament.resource.api_provider.form.username'))
->nullable()
->maxLength(255),
TextInput::make('password')
->label(__('filament.resource.api_provider.form.password'))
->password()
->nullable()
->maxLength(255),
TextInput::make('token')
->label(__('filament.resource.api_provider.form.token'))
->nullable()
->maxLength(255),
Select::make('plugin')
->options($plugins)
->nullable()
->label(__('filament.resource.api_provider.form.plugin')),
Forms\Components\Section::make()
->schema([
TextInput::make('name')
->label(__('filament.resource.api_provider.form.name'))
->required()
->maxLength(255),
Toggle::make('enabled')
->label(__('filament.resource.api_provider.form.enabled'))
->default(true),
TextInput::make('api_url')
->label(__('filament.resource.api_provider.form.api_url'))
->required()
->url()
->maxLength(255),
TextInput::make('username')
->label(__('filament.resource.api_provider.form.username'))
->nullable()
->maxLength(255),
TextInput::make('password')
->label(__('filament.resource.api_provider.form.password'))
->password()
->nullable()
->maxLength(255),
TextInput::make('token')
->label(__('filament.resource.api_provider.form.token'))
->nullable()
->maxLength(255),
Select::make('plugin')
->options($plugins)
->nullable()
->label(__('filament.resource.api_provider.form.plugin')),
Actions::make([
Action::make('test_connection')
->label(__('filament.resource.api_provider.action.test_connection'))
->icon(function (\Livewire\Component $livewire) {
return match ($livewire->testResultState) {
'success' => 'heroicon-o-check-circle',
'failed' => 'heroicon-o-x-circle',
default => 'heroicon-o-link',
};
})
->color(function (\Livewire\Component $livewire) {
return match ($livewire->testResultState) {
'success' => 'success',
'failed' => 'danger',
default => 'gray',
};
})
->action(function (array $data, Forms\Components\Component $component, \Livewire\Component $livewire) {
$formData = $component->getLivewire()->form->getState();
$apiUrl = str_replace('127.0.0.1', 'localhost', $formData['api_url'] ?? null);
$plugin = $formData['plugin'] ?? null;
$username = $formData['username'] ?? null;
$password = $formData['password'] ?? null;
$token = $formData['token'] ?? null;
try {
$http = Http::timeout(25);
if ($username && $password) {
$http->withBasicAuth($username, $password);
} elseif ($token) {
$http->withToken($token);
}
$response = $http->get($apiUrl);
if ($response->successful()) {
Log::info('External API connection successful.', [
'api_url' => $apiUrl,
'plugin' => $plugin,
]);
Notification::make()
->title(__('filament.resource.api_provider.notification.connection_successful'))
->success()
->send();
$component->getLivewire()->dispatch('testConnectionFinished', result: 'success');
} else {
Log::warning('External API connection failed: Non-successful response.', [
'api_url' => $apiUrl,
'plugin' => $plugin,
'status' => $response->status(),
'response_body' => $response->body(),
]);
Notification::make()
->title(__('filament.resource.api_provider.notification.connection_failed'))
->body($response->json('message', 'An unknown error occurred.'))
->danger()
->send();
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
}
} catch (\Illuminate\Http\Client\RequestException $e) {
Log::error('External API connection failed: Timeout or network error.', [
'api_url' => $apiUrl,
'plugin' => $plugin,
'error_message' => $e->getMessage(),
]);
Notification::make()
->title(__('filament.resource.api_provider.notification.connection_failed'))
->body('Timeout or network error: ' . $e->getMessage())
->danger()
->send();
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
} catch (\Exception $e) {
Log::error('External API connection failed: An unexpected error occurred.', [
'api_url' => $apiUrl,
'plugin' => $plugin,
'error_message' => $e->getMessage(),
]);
Notification::make()
->title(__('filament.resource.api_provider.notification.connection_failed'))
->body('An unexpected error occurred: ' . $e->getMessage())
->danger()
->send();
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
}
}),
]),
])
]);
}
public static function table(Table $table): Table
{
return $table
@@ -103,42 +198,38 @@ class ApiProviderResource extends Resource
Tables\Actions\CreateAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListApiProviders::route('/'),
'create' => Pages\CreateApiProvider::route('/create'),
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
];
public static function getPages(): array
{
return [
'index' => Pages\ListApiProviders::route('/'),
'create' => Pages\CreateApiProvider::route('/create'),
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
];
}
protected static function getAvailablePlugins(): array
{
$plugins = [];
$path = app_path('Api/Plugins');
$files = File::files($path);
foreach ($files as $file) {
$filename = $file->getFilenameWithoutExtension();
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
continue;
protected static function getAvailablePlugins(): array
{
$plugins = [];
$path = app_path('Api/Plugins');
$files = File::files($path);
foreach ($files as $file) {
$filename = $file->getFilenameWithoutExtension();
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
continue;
}
$class = "App\Api\Plugins\\" . $filename;
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
// Do not instantiate here, just get identifier and name if possible statically or by convention
// For now, we'll use filename as identifier and name
$plugins[$filename] = $filename;
}
$class = 'App\\Api\\Plugins\\' . $filename;
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
// Do not instantiate here, just get identifier and name if possible statically or by convention
// For now, we'll use filename as identifier and name
$plugins[$filename] = $filename;
}
}
return $plugins;
}
}
return $plugins;
}
}

View File

@@ -5,11 +5,20 @@ namespace App\Filament\Resources\ApiProviderResource\Pages;
use App\Filament\Resources\ApiProviderResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Livewire\Attributes\On;
class CreateApiProvider extends CreateRecord
{
public $testResultState = 'none';
protected static string $resource = ApiProviderResource::class;
#[On('testConnectionFinished')]
public function onTestConnectionFinished($result)
{
$this->testResultState = $result;
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');

View File

@@ -5,11 +5,20 @@ namespace App\Filament\Resources\ApiProviderResource\Pages;
use App\Filament\Resources\ApiProviderResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Livewire\Attributes\On;
class EditApiProvider extends EditRecord
{
public $testResultState = 'none';
protected static string $resource = ApiProviderResource::class;
#[On('testConnectionFinished')]
public function onTestConnectionFinished($result)
{
$this->testResultState = $result;
}
protected function getHeaderActions(): array
{
return [

View File

@@ -31,11 +31,27 @@ class SettingResource extends Resource
->maxLength(255)
->hiddenOn('edit'),
Forms\Components\Fieldset::make()
->label(fn (?Setting $record) => $record ? $record->key : __('New Setting'))
->label(fn (?Setting $record) => $record ? __('filament.resource.setting.form.' . $record->key) : __('New Setting'))
->schema([
TextInput::make('value')
->label(__('Value'))
->disableLabel()
->label(__('Bitte Wert eingeben'))
->disableLabel(fn (?Setting $record) => $record && ($record->key !== 'image_refresh_interval' && $record->key !== 'new_image_timespan_minutes' && $record->key !== 'gallery_heading'))
->formatStateUsing(function (?string $state, ?Setting $record) {
if ($record && $record->key === 'image_refresh_interval') {
return (int)$state / 1000;
} else if ($record && $record->key === 'new_image_timespan_minutes') {
return (int)$state;
}
return $state;
})
->dehydrateStateUsing(function (?string $state, ?Setting $record) {
if ($record && $record->key === 'image_refresh_interval') {
return (int)$state * 1000;
} else if ($record && $record->key === 'new_image_timespan_minutes') {
return (int)$state;
}
return $state;
})
])
]);
}

View File

@@ -27,47 +27,70 @@ class StyleResource extends Resource
public static function form(Form $form): Form
{
return $form
->columns('full')
->schema([
TextInput::make('title')
->label(__('filament.resource.style.form.title'))
->required()
->maxLength(255),
Toggle::make('enabled')
->label(__('filament.resource.style.form.enabled'))
->default(true),
Textarea::make('prompt')
->label(__('filament.resource.style.form.prompt'))
->required()
->rows(5),
Textarea::make('description')
->label(__('filament.resource.style.form.description'))
->required()
->rows(5),
FileUpload::make('preview_image')
->label(__('filament.resource.style.form.preview_image'))
->disk('public')
->directory('style_previews')
->image()
->imageEditor()
->required()
->rules(['mimes:jpeg,png,bmp,gif,webp']),
Textarea::make('parameters')
->label(__('filament.resource.style.form.parameters'))
->nullable()
->rows(15)
->json()
->helperText(__('filament.resource.style.form.parameters_help'))
->formatStateUsing(fn (?array $state): ?string => $state ? json_encode($state, JSON_PRETTY_PRINT) : null),
Select::make('ai_model_id')
->relationship('aiModel', 'name')
->label(__('filament.resource.style.form.ai_model'))
->required(),
Forms\Components\Tabs::make('Style Details')
->tabs([
Forms\Components\Tabs\Tab::make('General')
->schema([
Forms\Components\Grid::make(2)
->schema([
TextInput::make('title')
->label(__('filament.resource.style.form.title'))
->required()
->maxLength(255),
Toggle::make('enabled')
->label(__('filament.resource.style.form.enabled'))
->default(true),
]),
Forms\Components\Grid::make(2)
->schema([
Textarea::make('prompt')
->label(__('filament.resource.style.form.prompt'))
->required()
->rows(5),
Textarea::make('description')
->label(__('filament.resource.style.form.description'))
->required()
->rows(5),
]),
Select::make('ai_model_id')
->relationship('aiModel', 'name')
->label(__('filament.resource.style.form.ai_model'))
->required(),
FileUpload::make('preview_image')
->label(__('filament.resource.style.form.preview_image'))
->disk('public')
->directory('style_previews')
->image()
->imageEditor()
->required()
->rules(['mimes:jpeg,png,bmp,gif,webp']),
]),
Forms\Components\Tabs\Tab::make('Details')
->schema([
Forms\Components\TextInput::make('sort_order')
->numeric()
->default(0)
->label(__('filament.resource.style.form.sort_order')),
Textarea::make('parameters')
->label(__('filament.resource.style.form.parameters'))
->nullable()
->rows(15)
->json()
->helperText(__('filament.resource.style.form.parameters_help'))
->formatStateUsing(fn (?array $state): ?string => $state ? json_encode($state, JSON_PRETTY_PRINT) : null),
]),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->defaultSort('sort_order')
->reorderable('sort_order')
->columns([
TextColumn::make('title')->label(__('filament.resource.style.table.title'))->searchable()->sortable(),
IconColumn::make('enabled')
@@ -75,6 +98,7 @@ class StyleResource extends Resource
->boolean(),
TextColumn::make('aiModel.name')->label(__('filament.resource.style.table.ai_model'))->searchable()->sortable(),
ImageColumn::make('preview_image')->label(__('filament.resource.style.table.preview_image'))->disk('public'),
TextColumn::make('sort_order')->label(__('filament.resource.style.table.sort_order'))->sortable(),
])
->filters([
SelectFilter::make('ai_model')

View File

@@ -5,11 +5,13 @@ namespace App\Filament\Resources\StyleResource\Pages;
use App\Filament\Resources\StyleResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Concerns\CanReorderRecords;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
class ListStyles extends ListRecords
{
use CanReorderRecords;
protected static string $resource = StyleResource::class;
protected function getHeaderActions(): array