diff --git a/app/Contracts/Repository/ServerRepositoryInterface.php b/app/Contracts/Repository/ServerRepositoryInterface.php index d70584b50..d7e3dfc5d 100644 --- a/app/Contracts/Repository/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/ServerRepositoryInterface.php @@ -139,16 +139,4 @@ interface ServerRepositoryInterface extends RepositoryInterface * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator; - - /** - * Returns every server that exists for a given node. - * - * This is different from {@see loadAllServersForNode} because - * it does not paginate the response. - * - * @param int $node - * - * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection - */ - public function loadEveryServerForNode(int $node); } diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php index 2e4aa8ab1..67a30acf2 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php @@ -3,11 +3,13 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; use Illuminate\Http\Request; +use Pterodactyl\Models\Server; use Illuminate\Http\JsonResponse; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Services\Eggs\EggConfigurationService; use Pterodactyl\Repositories\Eloquent\ServerRepository; +use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection; use Pterodactyl\Services\Servers\ServerConfigurationStructureService; class ServerDetailsController extends Controller @@ -27,11 +29,6 @@ class ServerDetailsController extends Controller */ private $configurationStructureService; - /** - * @var \Pterodactyl\Repositories\Eloquent\NodeRepository - */ - private $nodeRepository; - /** * ServerConfigurationController constructor. * @@ -49,7 +46,6 @@ class ServerDetailsController extends Controller $this->eggConfigurationService = $eggConfigurationService; $this->repository = $repository; $this->configurationStructureService = $configurationStructureService; - $this->nodeRepository = $nodeRepository; } /** @@ -66,7 +62,7 @@ class ServerDetailsController extends Controller { $server = $this->repository->getByUuid($uuid); - return JsonResponse::create([ + return new JsonResponse([ 'settings' => $this->configurationStructureService->handle($server), 'process_configuration' => $this->eggConfigurationService->handle($server), ]); @@ -76,25 +72,19 @@ class ServerDetailsController extends Controller * Lists all servers with their configurations that are assigned to the requesting node. * * @param \Illuminate\Http\Request $request - * - * @return \Illuminate\Http\JsonResponse - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @return \Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection */ public function list(Request $request) { + /** @var \Pterodactyl\Models\Node $node */ $node = $request->attributes->get('node'); - $servers = $this->repository->loadEveryServerForNode($node->id); - $configurations = []; + // Avoid run-away N+1 SQL queries by pre-loading the relationships that are used + // within each of the services called below. + $servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables') + ->where('node_id', $node->id) + ->paginate($request->input('per_page', 50)); - foreach ($servers as $server) { - $configurations[$server->uuid] = [ - 'settings' => $this->configurationStructureService->handle($server), - 'process_configuration' => $this->eggConfigurationService->handle($server), - ]; - } - - return JsonResponse::create($configurations); + return new ServerConfigurationCollection($servers); } } diff --git a/app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php b/app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php index bc365e63c..d222f58ec 100644 --- a/app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php +++ b/app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php @@ -69,7 +69,7 @@ class DaemonAuthenticate // Ensure that all of the correct parts are provided in the header. if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) { throw new BadRequestHttpException( - 'The Authorization headed provided was not in a valid format.' + 'The Authorization header provided was not in a valid format.' ); } diff --git a/app/Http/Resources/Wings/ServerConfigurationCollection.php b/app/Http/Resources/Wings/ServerConfigurationCollection.php new file mode 100644 index 000000000..5ee877a98 --- /dev/null +++ b/app/Http/Resources/Wings/ServerConfigurationCollection.php @@ -0,0 +1,35 @@ +make(EggConfigurationService::class); + $configuration = Container::getInstance()->make(ServerConfigurationStructureService::class); + + return $this->collection->map(function (Server $server) use ($configuration, $egg) { + return [ + 'uuid' => $server->uuid, + 'settings' => $configuration->handle($server), + 'process_configuration' => $egg->handle($server), + ]; + })->toArray(); + } +} diff --git a/app/Models/Server.php b/app/Models/Server.php index 60a5de439..c8b8615fe 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -83,6 +83,13 @@ class Server extends Model 'oom_disabled' => true, ]; + /** + * The default relationships to load for all server models. + * + * @var string[] + */ + protected $with = ['allocation']; + /** * The attributes that should be mutated to dates. * diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php index 5e884a661..fdfd82fc2 100644 --- a/app/Repositories/Eloquent/ServerRepository.php +++ b/app/Repositories/Eloquent/ServerRepository.php @@ -270,22 +270,4 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt ->where('node_id', '=', $node) ->paginate($limit); } - - /** - * Returns every server that exists for a given node. - * - * This is different from {@see loadAllServersForNode} because - * it does not paginate the response. - * - * @param int $node - * - * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection - */ - public function loadEveryServerForNode(int $node) - { - return $this->getBuilder() - ->with('nest') - ->where('node_id', '=', $node) - ->get(); - } } diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index fb4412170..dc2a4bfb2 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -7,8 +7,6 @@ use Pterodactyl\Models\Server; class ServerConfigurationStructureService { - const REQUIRED_RELATIONS = ['allocation', 'allocations', 'egg']; - /** * @var \Pterodactyl\Services\Servers\EnvironmentService */ @@ -31,13 +29,11 @@ class ServerConfigurationStructureService * daemon, if you modify the structure eggs will break unexpectedly. * * @param \Pterodactyl\Models\Server $server - * @param bool $legacy + * @param bool $legacy deprecated * @return array */ public function handle(Server $server, bool $legacy = false): array { - $server->loadMissing(self::REQUIRED_RELATIONS); - return $legacy ? $this->returnLegacyFormat($server) : $this->returnCurrentFormat($server); @@ -93,6 +89,7 @@ class ServerConfigurationStructureService * * @param \Pterodactyl\Models\Server $server * @return array + * @deprecated */ protected function returnLegacyFormat(Server $server) {