coolify/routes/api.php

126 lines
5.3 KiB
PHP
Raw Normal View History

2023-03-17 15:33:48 +01:00
<?php
use App\Actions\Database\StartMariadb;
2023-10-20 14:58:00 +02:00
use App\Actions\Database\StartMongodb;
use App\Actions\Database\StartMysql;
2023-10-20 14:51:01 +02:00
use App\Actions\Database\StartPostgresql;
2023-10-20 14:58:00 +02:00
use App\Actions\Database\StartRedis;
use App\Actions\Service\StartService;
2023-09-25 20:57:52 +02:00
use App\Models\User;
2023-12-27 16:45:01 +01:00
use App\Providers\RouteServiceProvider;
2023-10-20 14:51:01 +02:00
use Illuminate\Http\Request;
2023-03-17 15:33:48 +01:00
use Illuminate\Support\Facades\Route;
2023-10-20 14:51:01 +02:00
use Visus\Cuid2\Cuid2;
2023-03-17 15:33:48 +01:00
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
2023-04-28 11:06:55 +02:00
Route::get('/health', function () {
return 'OK';
2023-03-17 15:33:48 +01:00
});
2023-10-20 14:51:01 +02:00
Route::group([
'middleware' => ['auth:sanctum'],
'prefix' => 'v1'
], function () {
Route::get('/deploy', function (Request $request) {
$token = auth()->user()->currentAccessToken();
$teamId = data_get($token, 'team_id');
$uuid = $request->query->get('uuid');
$force = $request->query->get('force') ?? false;
if (is_null($teamId)) {
return response()->json(['error' => 'Invalid token.'], 400);
}
if (!$uuid) {
return response()->json(['error' => 'No UUID provided.'], 400);
}
$resource = getResourceByUuid($uuid, $teamId);
if ($resource) {
$type = $resource->getMorphClass();
if ($type === 'App\Models\Application') {
queue_application_deployment(
application_id: $resource->id,
deployment_uuid: new Cuid2(7),
force_rebuild: $force,
);
return response()->json(['message' => 'Deployment queued.'], 200);
} else if ($type === 'App\Models\StandalonePostgresql') {
if (str($resource->status)->startsWith('running')) {
return response()->json(['message' => 'Database already running.'], 200);
}
2023-10-20 14:51:01 +02:00
StartPostgresql::run($resource);
$resource->update([
'started_at' => now(),
]);
return response()->json(['message' => 'Database started.'], 200);
2023-10-20 14:58:00 +02:00
} else if ($type === 'App\Models\StandaloneRedis') {
if (str($resource->status)->startsWith('running')) {
return response()->json(['message' => 'Database already running.'], 200);
}
2023-10-20 14:58:00 +02:00
StartRedis::run($resource);
$resource->update([
'started_at' => now(),
]);
return response()->json(['message' => 'Database started.'], 200);
} else if ($type === 'App\Models\StandaloneMongodb') {
if (str($resource->status)->startsWith('running')) {
return response()->json(['message' => 'Database already running.'], 200);
}
2023-10-20 14:58:00 +02:00
StartMongodb::run($resource);
$resource->update([
'started_at' => now(),
]);
return response()->json(['message' => 'Database started.'], 200);
} else if ($type === 'App\Models\StandaloneMysql') {
if (str($resource->status)->startsWith('running')) {
return response()->json(['message' => 'Database already running.'], 200);
}
StartMysql::run($resource);
$resource->update([
'started_at' => now(),
]);
return response()->json(['message' => 'Database started.'], 200);
} else if ($type === 'App\Models\StandaloneMariadb') {
if (str($resource->status)->startsWith('running')) {
return response()->json(['message' => 'Database already running.'], 200);
}
StartMariadb::run($resource);
$resource->update([
'started_at' => now(),
]);
return response()->json(['message' => 'Database started.'], 200);
} else if ($type === 'App\Models\Service') {
2023-10-20 14:58:00 +02:00
StartService::run($resource);
return response()->json(['message' => 'Service started. It could take a while, be patient.'], 200);
2023-10-20 14:51:01 +02:00
}
}
return response()->json(['error' => "No resource found with {$uuid}."], 404);
2023-10-20 14:51:01 +02:00
});
});
2023-09-25 20:57:52 +02:00
Route::middleware(['throttle:5'])->group(function () {
2023-10-20 14:51:01 +02:00
Route::get('/unsubscribe/{token}', function () {
2023-09-25 20:57:52 +02:00
try {
$token = request()->token;
$email = decrypt($token);
if (!User::whereEmail($email)->exists()) {
2023-12-27 16:45:01 +01:00
return redirect(RouteServiceProvider::HOME);
2023-09-25 20:57:52 +02:00
}
if (User::whereEmail($email)->first()->marketing_emails === false) {
return 'You have already unsubscribed from marketing emails.';
}
User::whereEmail($email)->update(['marketing_emails' => false]);
return 'You have been unsubscribed from marketing emails.';
} catch (\Throwable $e) {
return 'Something went wrong. Please try again or contact support.';
}
})->name('unsubscribe.marketing.emails');
});