lasthourcloud/routes/web.php

268 lines
10 KiB
PHP
Raw Normal View History

2023-03-17 14:33:48 +00:00
<?php
2023-04-19 10:42:15 +00:00
use App\Http\Controllers\ApplicationController;
use App\Http\Controllers\ProjectController;
2023-04-25 07:38:05 +00:00
use App\Models\InstanceSettings;
2023-05-03 10:38:57 +00:00
use App\Models\PrivateKey;
2023-05-02 10:47:52 +00:00
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
2023-05-11 13:20:02 +00:00
use App\Models\Environment;
2023-05-08 11:36:49 +00:00
use App\Models\GithubApp;
use App\Models\Project;
2023-05-04 09:14:37 +00:00
use App\Models\Server;
2023-05-08 11:36:49 +00:00
use Illuminate\Http\Request;
2023-03-17 14:33:48 +00:00
use Illuminate\Support\Facades\Route;
2023-05-08 19:56:44 +00:00
use Illuminate\Support\Str;
2023-03-17 14:33:48 +00:00
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
2023-03-20 12:04:22 +00:00
Route::middleware(['auth'])->group(function () {
2023-05-11 13:20:02 +00:00
Route::get('/magic', function () {
try {
2023-05-16 10:17:39 +00:00
$id = session('currentTeam')->id;
2023-05-11 13:20:02 +00:00
$is_new_project = request()->query('project') === 'new';
$is_new_environment = request()->query('environment') === 'new';
// Get servers
if (request()->query('servers') === 'true') {
2023-05-16 10:17:39 +00:00
$servers = Server::where('team_id', $id)->get();
2023-05-11 13:20:02 +00:00
return response()->json([
'servers' => $servers,
]);
}
// Get destinations
2023-05-12 09:59:02 +00:00
if ((request()->query('server') && request()->query('destinations') === 'true') || request()->query('destinations') === 'true') {
2023-05-11 13:20:02 +00:00
$destinations = Server::destinations(request()->query('server'));
return response()->json([
'destinations' => $destinations->toArray(),
]);
}
2023-05-16 08:03:34 +00:00
// Get private Keys
if (request()->query('privateKeys') === 'true') {
2023-05-16 10:17:39 +00:00
$privateKeys = PrivateKey::where('team_id', $id)->get();
2023-05-16 08:03:34 +00:00
return response()->json([
'privateKeys' => $privateKeys->toArray(),
]);
}
2023-05-16 10:17:39 +00:00
// Get sources
if (request()->query('sources') === 'true') {
$github_apps = GithubApp::private();
$sources = $github_apps;
return response()->json([
'sources' => $sources->toArray(),
]);
}
2023-05-11 13:20:02 +00:00
// Get projects
2023-05-12 09:38:23 +00:00
if ((request()->query('server') && request()->query('destination') && request()->query('projects') === 'true') || request()->query('projects') === 'true') {
2023-05-16 10:17:39 +00:00
$projects = Project::where('team_id', $id)->get()->sortBy('name');
2023-05-11 13:20:02 +00:00
return response()->json([
'projects' => $projects->toArray(),
]);
}
// Get environments
if (request()->query('server') && request()->query('destination') && request()->query('project') && request()->query('environments') === 'true') {
2023-05-16 10:17:39 +00:00
$environments = Project::where('team_id', $id)->where('uuid', request()->query('project'))->first()->environments;
2023-05-11 13:20:02 +00:00
return response()->json([
'environments' => $environments->toArray(),
]);
}
if ($is_new_project) {
$project = Project::create([
'name' => request()->query('name') ?? generateRandomName(),
2023-05-16 10:17:39 +00:00
'team_id' => $id,
2023-05-11 13:20:02 +00:00
]);
return response()->json([
'project_uuid' => $project->uuid
]);
}
if ($is_new_environment) {
$environment = Project::where('uuid', request()->query('project'))->first()->environments->where('name', request()->query('name'))->first();
if (!$environment) {
$environment = Environment::create([
'name' => request()->query('name') ?? generateRandomName(),
'project_id' => Project::where('uuid', request()->query('project'))->first()->id,
]);
}
return response()->json([
2023-05-12 08:45:28 +00:00
'environment_name' => $environment->name
2023-05-11 13:20:02 +00:00
]);
}
return response()->json([
'magic' => true,
]);
} catch (\Throwable $e) {
return generalErrorHandler($e, isJson: true);
}
});
2023-04-25 07:38:05 +00:00
Route::get('/', function () {
2023-05-08 11:36:49 +00:00
$id = session('currentTeam')->id;
$projects = Project::where('team_id', $id)->get();
$servers = Server::where('team_id', $id)->get();
2023-05-02 10:47:52 +00:00
$destinations = $servers->map(function ($server) {
return $server->standaloneDockers->merge($server->swarmDockers);
})->flatten();
2023-05-08 11:36:49 +00:00
$private_keys = PrivateKey::where('team_id', $id)->get();
$github_apps = GithubApp::private();
2023-04-25 09:01:56 +00:00
return view('dashboard', [
2023-04-25 08:47:13 +00:00
'servers' => $servers->sortBy('name'),
2023-05-02 10:47:52 +00:00
'projects' => $projects->sortBy('name'),
'destinations' => $destinations->sortBy('name'),
2023-05-03 10:38:57 +00:00
'private_keys' => $private_keys->sortBy('name'),
2023-05-08 11:36:49 +00:00
'github_apps' => $github_apps->sortBy('name'),
2023-04-25 07:38:05 +00:00
]);
2023-04-25 09:01:56 +00:00
})->name('dashboard');
Route::get('/profile', function () {
return view('profile');
2023-04-25 07:38:05 +00:00
})->name('profile');
2023-05-16 12:38:11 +00:00
Route::get('/profile/team', function () {
return view('team');
})->name('team');
2023-04-25 07:38:05 +00:00
Route::get('/settings', function () {
$isRoot = auth()->user()->isRoot();
if ($isRoot) {
2023-05-16 15:09:50 +00:00
$settings = InstanceSettings::get();
2023-04-25 07:38:05 +00:00
return view('settings', [
'settings' => $settings
]);
} else {
2023-04-25 12:43:35 +00:00
return redirect()->route('dashboard');
2023-04-25 07:38:05 +00:00
}
})->name('settings');
2023-04-14 08:00:42 +00:00
Route::get('/update', function () {
return view('update');
2023-04-25 07:38:05 +00:00
})->name('update');
2023-05-04 08:00:08 +00:00
Route::get('/command-center', function () {
2023-05-12 13:39:07 +00:00
$servers = Server::validated();
if ($servers->count() === 0) {
return redirect()->route('dashboard');
}
return view('command-center', [
'servers' => $servers,
]);
2023-05-04 08:00:08 +00:00
})->name('command-center');
2023-04-25 07:38:05 +00:00
});
2023-05-03 10:38:57 +00:00
Route::middleware(['auth'])->group(function () {
Route::get('/private-key/new', fn () => view('private-key.new'))->name('private-key.new');
Route::get('/private-key/{private_key_uuid}', function () {
$private_key = PrivateKey::where('uuid', request()->private_key_uuid)->first();
return view('private-key.show', [
'private_key' => $private_key,
2023-05-16 15:09:50 +00:00
]);G
2023-05-03 10:38:57 +00:00
})->name('private-key.show');
});
2023-05-08 11:36:49 +00:00
Route::middleware(['auth'])->group(function () {
Route::get('/source/new', fn () => view('source.new'))->name('source.new');
Route::get('/source/github/{github_app_uuid}', function (Request $request) {
2023-05-09 09:33:50 +00:00
$github_app = GithubApp::where('uuid', request()->github_app_uuid)->first();
2023-05-09 12:42:10 +00:00
$name = Str::of(Str::kebab($github_app->name))->start('coolify-');
2023-05-16 15:09:50 +00:00
$settings = InstanceSettings::get();
2023-05-08 19:56:44 +00:00
$host = $request->schemeAndHttpHost();
if ($settings->fqdn) {
$host = $settings->fqdn;
}
2023-05-09 09:33:50 +00:00
$installation_path = $github_app->html_url === 'https://github.com' ? 'apps' : 'github-apps';
$installation_url = "$github_app->html_url/$installation_path/$name/installations/new";
2023-05-08 19:56:44 +00:00
return view('source.github.show', [
'github_app' => $github_app,
'host' => $host,
2023-05-09 09:33:50 +00:00
'name' => $name,
'installation_url' => $installation_url,
2023-05-08 19:56:44 +00:00
]);
2023-05-08 11:36:49 +00:00
})->name('source.github.show');
});
2023-04-25 08:47:13 +00:00
Route::middleware(['auth'])->group(function () {
2023-05-15 12:54:03 +00:00
Route::get('/server/new', fn () => view('server.new', [
'private_keys' => PrivateKey::where('team_id', session('currentTeam')->id)->get(),
]))->name('server.new');
2023-04-25 08:47:13 +00:00
Route::get('/server/{server_uuid}', function () {
$server = session('currentTeam')->load(['servers'])->servers->firstWhere('uuid', request()->server_uuid);
if (!$server) {
abort(404);
}
return view('server.show', [
2023-05-02 10:47:52 +00:00
'server' => $server,
2023-04-25 08:47:13 +00:00
]);
})->name('server.show');
2023-05-03 10:38:57 +00:00
Route::get('/server/{server_uuid}/private-key', function () {
return view('server.private-key');
})->name('server.private-key');
2023-04-25 08:47:13 +00:00
});
2023-05-02 10:47:52 +00:00
Route::middleware(['auth'])->group(function () {
2023-05-03 09:16:08 +00:00
Route::get('/destination/new', function () {
2023-05-08 07:16:50 +00:00
$servers = Server::validated();
2023-05-12 11:10:09 +00:00
$pre_selected_server_uuid = data_get(request()->query(), 'server');
if ($pre_selected_server_uuid) {
$server = $servers->firstWhere('uuid', $pre_selected_server_uuid);
if ($server) {
$server_id = $server->id;
}
}
2023-05-03 09:16:08 +00:00
return view('destination.new', [
2023-05-04 09:14:37 +00:00
"servers" => $servers,
2023-05-12 11:10:09 +00:00
"server_id" => $server_id ?? null,
2023-05-03 09:16:08 +00:00
]);
})->name('destination.new');
2023-05-02 10:47:52 +00:00
Route::get('/destination/{destination_uuid}', function () {
$standalone_dockers = StandaloneDocker::where('uuid', request()->destination_uuid)->first();
$swarm_dockers = SwarmDocker::where('uuid', request()->destination_uuid)->first();
if (!$standalone_dockers && !$swarm_dockers) {
abort(404);
}
$destination = $standalone_dockers ? $standalone_dockers : $swarm_dockers;
return view('destination.show', [
2023-05-04 09:14:37 +00:00
'destination' => $destination->load(['server']),
2023-05-02 10:47:52 +00:00
]);
})->name('destination.show');
});
2023-04-25 07:38:05 +00:00
Route::middleware(['auth'])->group(function () {
Route::get(
'/project/{project_uuid}',
[ProjectController::class, 'environments']
)->name('project.environments');
2023-04-26 11:25:41 +00:00
Route::get(
'/project/{project_uuid}/{environment_name}/new',
2023-05-11 13:20:02 +00:00
[ProjectController::class, 'new']
2023-04-26 11:25:41 +00:00
)->name('project.resources.new');
2023-04-25 07:38:05 +00:00
Route::get(
'/project/{project_uuid}/{environment_name}',
[ProjectController::class, 'resources']
)->name('project.resources');
Route::get(
'/project/{project_uuid}/{environment_name}/application/{application_uuid}',
[ApplicationController::class, 'configuration']
2023-04-25 09:01:56 +00:00
)->name('project.application.configuration');
2023-04-25 07:38:05 +00:00
Route::get(
'/project/{project_uuid}/{environment_name}/application/{application_uuid}/deployment',
[ApplicationController::class, 'deployments']
2023-04-25 09:01:56 +00:00
)->name('project.application.deployments');
2023-04-25 07:38:05 +00:00
Route::get(
'/project/{project_uuid}/{environment_name}/application/{application_uuid}/deployment/{deployment_uuid}',
[ApplicationController::class, 'deployment']
2023-04-25 09:01:56 +00:00
)->name('project.application.deployment');
});