Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Servers/ServerController.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Server;
7
use Illuminate\Http\JsonResponse;
8
use Spatie\QueryBuilder\QueryBuilder;
9
use Pterodactyl\Services\Servers\ServerCreationService;
10
use Pterodactyl\Services\Servers\ServerDeletionService;
11
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
12
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;
13
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
14
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
15
use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;
16
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
17
18
class ServerController extends ApplicationApiController
19
{
20
/**
21
* ServerController constructor.
22
*/
23
public function __construct(
24
private ServerCreationService $creationService,
25
private ServerDeletionService $deletionService,
26
) {
27
parent::__construct();
28
}
29
30
/**
31
* Return all the servers that currently exist on the Panel.
32
*/
33
public function index(GetServersRequest $request): array
34
{
35
$servers = QueryBuilder::for(Server::query())
36
->allowedFilters(['uuid', 'uuidShort', 'name', 'description', 'image', 'external_id'])
37
->allowedSorts(['id', 'uuid'])
38
->paginate($request->query('per_page') ?? 50);
39
40
return $this->fractal->collection($servers)
41
->transformWith($this->getTransformer(ServerTransformer::class))
42
->toArray();
43
}
44
45
/**
46
* Create a new server on the system.
47
*
48
* @throws \Throwable
49
* @throws \Illuminate\Validation\ValidationException
50
* @throws \Pterodactyl\Exceptions\DisplayException
51
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
52
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
53
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
54
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
55
*/
56
public function store(StoreServerRequest $request): JsonResponse
57
{
58
$server = $this->creationService->handle($request->validated(), $request->getDeploymentObject());
59
60
return $this->fractal->item($server)
61
->transformWith($this->getTransformer(ServerTransformer::class))
62
->respond(201);
63
}
64
65
/**
66
* Show a single server transformed for the application API.
67
*/
68
public function view(GetServerRequest $request, Server $server): array
69
{
70
return $this->fractal->item($server)
71
->transformWith($this->getTransformer(ServerTransformer::class))
72
->toArray();
73
}
74
75
/**
76
* Deletes a server.
77
*
78
* @throws \Pterodactyl\Exceptions\DisplayException
79
*/
80
public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response
81
{
82
$this->deletionService->withForce($force === 'force')->handle($server);
83
84
return $this->returnNoContent();
85
}
86
}
87
88