#591 - server-logs

This commit is contained in:
Saeed Vaziry
2025-05-30 13:52:39 +02:00
parent 104cd2fce8
commit 857319025f
10 changed files with 416 additions and 54 deletions

View File

@ -1,11 +1,12 @@
<?php
namespace App\Actions\Server;
namespace App\Actions\ServerLog;
use App\Models\Server;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class CreateServerLog
class CreateLog
{
/**
* @param array<string, mixed> $input
@ -14,6 +15,8 @@ class CreateServerLog
*/
public function create(Server $server, array $input): void
{
Validator::make($input, self::rules())->validate();
$server->logs()->create([
'is_remote' => true,
'name' => $input['path'],

View File

@ -0,0 +1,34 @@
<?php
namespace App\Actions\ServerLog;
use App\Models\ServerLog;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class UpdateLog
{
/**
* @param array<string, mixed> $input
*
* @throws ValidationException
*/
public function update(ServerLog $serverLog, array $input): void
{
Validator::make($input, self::rules())->validate();
$serverLog->update([
'name' => $input['path'],
]);
}
/**
* @return array<string, string>
*/
public static function rules(): array
{
return [
'path' => 'required',
];
}
}

View File

@ -54,6 +54,14 @@ public function render($request, Throwable $e): Response
abort(404, class_basename($e->getModel()).' not found.');
}
if ($e instanceof SSHError) {
if ($request->header('X-Inertia')) {
return back()->with('error', $e->getLog()?->getContent(30) ?? $e->getMessage());
}
return response()->json(['error' => $e->getLog()?->getContent(30) ?? $e->getMessage()], 500);
}
return parent::render($request, $e);
}
}

View File

@ -2,19 +2,53 @@
namespace App\Http\Controllers;
use App\Actions\ServerLog\CreateLog;
use App\Actions\ServerLog\UpdateLog;
use App\Http\Resources\ServerLogResource;
use App\Models\Server;
use App\Models\ServerLog;
use App\Models\Site;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\RouteAttributes\Attributes\Delete;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Patch;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;
#[Prefix('servers/{server}/logs')]
#[Middleware(['auth', 'has-project'])]
class ServerLogController extends Controller
{
#[Get('/', name: 'logs')]
public function index(Server $server): Response
{
$this->authorize('viewAny', [ServerLog::class, $server]);
return Inertia::render('server-logs/index', [
'title' => 'Server logs',
'logs' => ServerLogResource::collection($server->logs()->where('is_remote', 0)->latest()->simplePaginate(config('web.pagination_size'))),
]);
}
#[Get('/remote', name: 'logs.remote')]
public function remote(Server $server): Response
{
$this->authorize('viewAny', [ServerLog::class, $server]);
return Inertia::render('server-logs/index', [
'title' => 'Remote logs',
'logs' => ServerLogResource::collection($server->logs()->where('is_remote', 1)->latest()->simplePaginate(config('web.pagination_size'))),
'remote' => true,
]);
}
#[Get('/json/{site?}', name: 'logs.json')]
public function json(Server $server, ?Site $site = null): ResourceCollection
{
@ -35,4 +69,45 @@ public function show(Server $server, ServerLog $log): string
return $log->getContent();
}
/**
* @throws Throwable
*/
#[Get('/{log}/download', name: 'logs.download')]
public function download(Server $server, ServerLog $log): StreamedResponse
{
$this->authorize('view', $log);
return $log->download();
}
#[Post('/', name: 'logs.store')]
public function store(Request $request, Server $server): RedirectResponse
{
$this->authorize('create', [ServerLog::class, $server]);
app(CreateLog::class)->create($server, $request->input());
return back()->with('success', 'Log created successfully');
}
#[Patch('{log}', name: 'logs.update')]
public function update(Request $request, Server $server, ServerLog $log): RedirectResponse
{
$this->authorize('update', $log);
app(UpdateLog::class)->update($log, $request->input());
return back()->with('success', 'Log updated successfully');
}
#[Delete('{log}', name: 'logs.destroy')]
public function destroy(Server $server, ServerLog $log): RedirectResponse
{
$this->authorize('delete', $log);
$log->delete();
return back()->with('success', 'Log deleted successfully');
}
}