|
9 | 9 | use Illuminate\Process\InvokedProcess;
|
10 | 10 | use Illuminate\Support\Collection;
|
11 | 11 | use Illuminate\Support\Facades\Process;
|
| 12 | +use Illuminate\Support\Facades\Validator; |
12 | 13 | use Illuminate\Support\Str;
|
13 | 14 | use OpenApi\Attributes as OA;
|
14 | 15 | use RuntimeException;
|
@@ -1427,4 +1428,67 @@ public function getMetrics(int $mins = 5)
|
1427 | 1428 | return $parsedCollection->toArray();
|
1428 | 1429 | }
|
1429 | 1430 | }
|
| 1431 | + |
| 1432 | + public function generateConfig($is_json = false) |
| 1433 | + { |
| 1434 | + $config = collect([]); |
| 1435 | + if ($this->build_pack = 'nixpacks') { |
| 1436 | + $config = collect([ |
| 1437 | + 'build_pack' => 'nixpacks', |
| 1438 | + 'docker_registry_image_name' => $this->docker_registry_image_name, |
| 1439 | + 'docker_registry_image_tag' => $this->docker_registry_image_tag, |
| 1440 | + 'install_command' => $this->install_command, |
| 1441 | + 'build_command' => $this->build_command, |
| 1442 | + 'start_command' => $this->start_command, |
| 1443 | + 'base_directory' => $this->base_directory, |
| 1444 | + 'publish_directory' => $this->publish_directory, |
| 1445 | + 'custom_docker_run_options' => $this->custom_docker_run_options, |
| 1446 | + 'ports_exposes' => $this->ports_exposes, |
| 1447 | + 'ports_mappings' => $this->ports_mapping, |
| 1448 | + 'settings' => collect([ |
| 1449 | + 'is_static' => $this->settings->is_static, |
| 1450 | + ]), |
| 1451 | + ]); |
| 1452 | + } |
| 1453 | + $config = $config->filter(function ($value) { |
| 1454 | + return str($value)->isNotEmpty(); |
| 1455 | + }); |
| 1456 | + if ($is_json) { |
| 1457 | + return json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 1458 | + } |
| 1459 | + |
| 1460 | + return $config; |
| 1461 | + } |
| 1462 | + public function setConfig($config) { |
| 1463 | + |
| 1464 | + $config = $config; |
| 1465 | + $validator = Validator::make(['config' => $config], [ |
| 1466 | + 'config' => 'required|json', |
| 1467 | + ]); |
| 1468 | + if ($validator->fails()) { |
| 1469 | + throw new \Exception('Invalid JSON format'); |
| 1470 | + } |
| 1471 | + $config = json_decode($config, true); |
| 1472 | + |
| 1473 | + $deepValidator = Validator::make(['config' => $config], [ |
| 1474 | + 'config.build_pack' => 'required|string', |
| 1475 | + 'config.base_directory' => 'required|string', |
| 1476 | + 'config.publish_directory' => 'required|string', |
| 1477 | + 'config.ports_exposes' => 'required|string', |
| 1478 | + 'config.settings.is_static' => 'required|boolean', |
| 1479 | + ]); |
| 1480 | + if ($deepValidator->fails()) { |
| 1481 | + throw new \Exception('Invalid data'); |
| 1482 | + } |
| 1483 | + $config = $deepValidator->validated()['config']; |
| 1484 | + |
| 1485 | + try { |
| 1486 | + $settings = data_get($config, 'settings', []); |
| 1487 | + data_forget($config, 'settings'); |
| 1488 | + $this->update($config); |
| 1489 | + $this->settings()->update($settings); |
| 1490 | + } catch (\Exception $e) { |
| 1491 | + throw new \Exception('Failed to update application settings'); |
| 1492 | + } |
| 1493 | + } |
1430 | 1494 | }
|
0 commit comments