-
-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathServiceController.php
More file actions
139 lines (100 loc) · 4.75 KB
/
ServiceController.php
File metadata and controls
139 lines (100 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace App\Http\Controllers\API;
use App\Actions\Service\Manage;
use App\Actions\Service\Uninstall;
use App\Http\Controllers\Controller;
use App\Http\Resources\ServiceResource;
use App\Models\Project;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Spatie\RouteAttributes\Attributes\Delete;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;
#[Prefix('api/projects/{project}/servers/{server}/services')]
#[Middleware(['auth:sanctum', 'can-see-project'])]
class ServiceController extends Controller
{
#[Get('/', name: 'api.projects.servers.services', middleware: 'ability:read')]
public function index(Project $project, Server $server): ResourceCollection
{
$this->authorize('viewAny', [Service::class, $server]);
$this->validateRoute($project, $server);
return ServiceResource::collection($server->services()->with('log')->simplePaginate(25));
}
#[Get('{service}', name: 'api.projects.servers.services.show', middleware: 'ability:read')]
public function show(Project $project, Server $server, Service $service): ServiceResource
{
$this->authorize('view', [$service, $server]);
$this->validateRoute($project, $server, $service);
$service->load('log');
return new ServiceResource($service);
}
#[Post('{service}/start', name: 'api.projects.servers.services.start', middleware: 'ability:write')]
public function start(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->start($service);
return response()->noContent();
}
#[Post('{service}/stop', name: 'api.projects.servers.services.stop', middleware: 'ability:write')]
public function stop(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->stop($service);
return response()->noContent();
}
#[Post('{service}/restart', name: 'api.projects.servers.services.restart', middleware: 'ability:write')]
public function restart(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->restart($service);
return response()->noContent();
}
#[Post('{service}/reload', name: 'api.projects.servers.services.reload', middleware: 'ability:write')]
public function reload(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->reload($service);
return response()->noContent();
}
#[Post('{service}/enable', name: 'api.projects.servers.services.enable', middleware: 'ability:write')]
public function enable(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->enable($service);
return response()->noContent();
}
#[Post('{service}/disable', name: 'api.projects.servers.services.disable', middleware: 'ability:write')]
public function disable(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('update', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Manage::class)->disable($service);
return response()->noContent();
}
#[Delete('{service}', name: 'api.projects.servers.services.uninstall', middleware: 'ability:write')]
public function uninstall(Project $project, Server $server, Service $service): \Illuminate\Http\Response
{
$this->authorize('delete', [$service, $server]);
$this->validateRoute($project, $server, $service);
app(Uninstall::class)->uninstall($service);
return response()->noContent();
}
private function validateRoute(Project $project, Server $server, ?Service $service = null): void
{
if ($project->id !== $server->project_id) {
abort(404, 'Server not found in project');
}
if ($service && $service->server_id !== $server->id) {
abort(404, 'Service not found in server');
}
}
}