From d16888b70777ae3aa8b14f321098dd36e3b8ba9b Mon Sep 17 00:00:00 2001 From: Meghea Iulian Date: Wed, 19 Mar 2025 10:22:34 +0200 Subject: [PATCH] feat(api): separate create and one-click routes Moves previous POST /services to POST /services/one-click. Adds new POST /services that takes a docker_compose. --- .../Controllers/Api/ServicesController.php | 34 +++-- openapi.yaml | 118 +++++++++++++++++- routes/api.php | 2 +- 3 files changed, 133 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/Api/ServicesController.php b/app/Http/Controllers/Api/ServicesController.php index f3cc03d8a..81b51633f 100644 --- a/app/Http/Controllers/Api/ServicesController.php +++ b/app/Http/Controllers/Api/ServicesController.php @@ -13,6 +13,7 @@ use App\Models\Server; use App\Models\Service; use Illuminate\Http\Request; use OpenApi\Attributes as OA; +use Symfony\Component\Yaml\Yaml; class ServicesController extends Controller { @@ -88,10 +89,10 @@ class ServicesController extends Controller } #[OA\Post( - summary: 'Create', + summary: 'Create one-click', description: 'Create a one-click service', - path: '/services', - operationId: 'create-service', + path: '/services/one-click', + operationId: 'create-one-click-service', security: [ ['bearerAuth' => []], ], @@ -211,7 +212,7 @@ class ServicesController extends Controller responses: [ new OA\Response( response: 201, - description: 'Create a service.', + description: 'Service created successfully.', content: [ new OA\MediaType( mediaType: 'application/json', @@ -235,7 +236,7 @@ class ServicesController extends Controller ), ] )] - public function create_service(Request $request) + public function create_one_click_service(Request $request) { $allowedFields = ['type', 'name', 'description', 'project_uuid', 'environment_name', 'environment_uuid', 'server_uuid', 'destination_uuid', 'instant_deploy']; @@ -381,10 +382,10 @@ class ServicesController extends Controller } #[OA\Post( - summary: 'Create (compose)', + summary: 'Create', description: 'Create a service', path: '/services', - operationId: 'compose-service', + operationId: 'create-service', security: [ ['bearerAuth' => []], ], @@ -405,7 +406,7 @@ class ServicesController extends Controller 'server_uuid' => ['type' => 'string', 'description' => 'Server UUID.'], 'destination_uuid' => ['type' => 'string', 'description' => 'Destination UUID. Required if server has multiple destinations.'], 'instant_deploy' => ['type' => 'boolean', 'default' => false, 'description' => 'Start the service immediately after creation.'], - 'connect_to_docker_network' => ['type' => 'boolean', 'description' => 'The flag to connect the service to the predefined Docker network.'], + 'connect_to_docker_network' => ['type' => 'boolean', 'default' => false, 'description' => 'The flag to connect the service to the predefined Docker network.'], 'docker_compose_raw' => ['type' => 'string', 'description' => 'The Docker Compose raw content.'], ], @@ -415,7 +416,7 @@ class ServicesController extends Controller responses: [ new OA\Response( response: 201, - description: 'Create a service.', + description: 'Service created successfully.', content: [ new OA\MediaType( mediaType: 'application/json', @@ -439,7 +440,7 @@ class ServicesController extends Controller ), ] )] - public function compose_service(Request $request) + public function create_service(Request $request) { $allowedFields = ['name', 'description', 'project_uuid', 'environment_name', 'environment_uuid', 'server_uuid', 'destination_uuid', 'instant_deploy', "docker_compose_raw", "connect_to_docker_network"]; @@ -487,9 +488,6 @@ class ServicesController extends Controller } $serverUuid = $request->server_uuid; $instantDeploy = $request->instant_deploy ?? false; - if ($request->is_public && ! $request->public_port) { - $request->offsetSet('is_public', false); - } $project = Project::whereTeamId($teamId)->whereUuid($request->project_uuid)->first(); if (! $project) { return response()->json(['message' => 'Project not found.'], 404); @@ -534,13 +532,14 @@ class ServicesController extends Controller $dockerComposeRaw = Yaml::dump(Yaml::parse($dockerCompose), 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); $service = new Service; - $service->fill($request->all()); - + $service->name = $request->name; + $service->description = $request->description; $service->docker_compose_raw = $dockerComposeRaw; $service->environment_id = $environment->id; $service->server_id = $server->id; $service->destination_id = $destination->id; $service->destination_type = $destination->getMorphClass(); + $service->connect_to_docker_network = $request->connect_to_docker_network; $service->save(); @@ -557,11 +556,10 @@ class ServicesController extends Controller return $domain; }); - - return response()->json([ + return response()->json(serializeApiResponse([ 'uuid' => $service->uuid, 'domains' => $domains, - ]); + ]))->setStatusCode(201); } #[OA\Get( diff --git a/openapi.yaml b/openapi.yaml index 2d1803113..d14f3edf9 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -1494,6 +1494,49 @@ paths: security: - bearerAuth: [] + '/applications/{uuid}/logs': + get: + tags: + - Applications + summary: 'Get application logs.' + description: 'Get application logs by UUID.' + operationId: get-application-logs-by-uuid + parameters: + - + name: uuid + in: path + description: 'UUID of the application.' + required: true + schema: + type: string + format: uuid + - + name: lines + in: query + description: 'Number of lines to show from the end of the logs.' + required: false + schema: + type: integer + format: int32 + default: 100 + responses: + '200': + description: 'Get application logs by UUID.' + content: + application/json: + schema: + properties: + logs: { type: string } + type: object + '401': + $ref: '#/components/responses/401' + '400': + $ref: '#/components/responses/400' + '404': + $ref: '#/components/responses/404' + security: + - + bearerAuth: [] '/applications/{uuid}/envs': get: tags: @@ -3953,8 +3996,79 @@ paths: tags: - Services summary: Create - description: 'Create a one-click service' + description: 'Create a service' operationId: create-service + requestBody: + required: true + content: + application/json: + schema: + required: + - server_uuid + - project_uuid + - environment_name + - environment_uuid + - docker_compose_raw + properties: + name: + type: string + maxLength: 255 + description: 'Name of the service.' + description: + type: string + nullable: true + description: 'Description of the service.' + project_uuid: + type: string + description: 'Project UUID.' + environment_name: + type: string + description: 'Environment name. You need to provide at least one of environment_name or environment_uuid.' + environment_uuid: + type: string + description: 'Environment UUID. You need to provide at least one of environment_name or environment_uuid.' + server_uuid: + type: string + description: 'Server UUID.' + destination_uuid: + type: string + description: 'Destination UUID. Required if server has multiple destinations.' + instant_deploy: + type: boolean + default: false + description: 'Start the service immediately after creation.' + connect_to_docker_network: + type: boolean + default: false + description: 'The flag to connect the service to the predefined Docker network.' + docker_compose_raw: + type: string + description: 'The Docker Compose raw content.' + type: object + responses: + '201': + description: 'Service created successfully.' + content: + application/json: + schema: + properties: + uuid: { type: string, description: 'Service UUID.' } + domains: { type: array, items: { type: string }, description: 'Service domains.' } + type: object + '401': + $ref: '#/components/responses/401' + '400': + $ref: '#/components/responses/400' + security: + - + bearerAuth: [] + /services/one-click: + post: + tags: + - Services + summary: 'Create one-click' + description: 'Create a one-click service' + operationId: create-one-click-service requestBody: required: true content: @@ -4001,7 +4115,7 @@ paths: type: object responses: '201': - description: 'Create a service.' + description: 'Service created successfully.' content: application/json: schema: diff --git a/routes/api.php b/routes/api.php index 30c37f8f9..fbd02f926 100644 --- a/routes/api.php +++ b/routes/api.php @@ -114,7 +114,7 @@ Route::group([ Route::get('/services', [ServicesController::class, 'services'])->middleware(['api.ability:read']); Route::post('/services', [ServicesController::class, 'create_service'])->middleware(['api.ability:write']); - Route::post('/services/compose', [ServicesController::class, 'compose_service'])->middleware(['api.ability:write']); + Route::post('/services/one-click', [ServicesController::class, 'create_one_click_service'])->middleware(['api.ability:write']); Route::get('/services/{uuid}', [ServicesController::class, 'service_by_uuid'])->middleware(['api.ability:read']); // Route::patch('/services/{uuid}', [ServicesController::class, 'update_by_uuid'])->middleware(['ability:write']);