2023-09-22 09:23:49 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Service ;
2023-09-22 09:23:49 +00:00
use App\Models\ServiceApplication ;
use Livewire\Component ;
2024-02-05 13:40:54 +00:00
class ServiceApplicationView extends Component
2023-09-22 09:23:49 +00:00
{
public ServiceApplication $application ;
2023-09-25 13:48:43 +00:00
public $parameters ;
2023-09-22 09:23:49 +00:00
protected $rules = [
'application.human_name' => 'nullable' ,
2023-09-22 12:47:25 +00:00
'application.description' => 'nullable' ,
2023-09-22 09:23:49 +00:00
'application.fqdn' => 'nullable' ,
2023-09-26 12:45:52 +00:00
'application.image' => 'required' ,
'application.exclude_from_status' => 'required|boolean' ,
'application.required_fqdn' => 'required|boolean' ,
2023-11-17 19:08:21 +00:00
'application.is_log_drain_enabled' => 'nullable|boolean' ,
2024-02-15 19:44:01 +00:00
'application.is_gzip_enabled' => 'nullable|boolean' ,
2024-03-04 09:46:13 +00:00
'application.is_stripprefix_enabled' => 'nullable|boolean' ,
2023-09-22 09:23:49 +00:00
];
public function render ()
{
2024-02-05 13:40:54 +00:00
return view ( 'livewire.project.service.service-application-view' );
2023-09-22 09:23:49 +00:00
}
2024-04-05 13:59:59 +00:00
public function updatedApplicationFqdn ()
{
$this -> application -> fqdn = str ( $this -> application -> fqdn ) -> replaceEnd ( ',' , '' ) -> trim ();
$this -> application -> fqdn = str ( $this -> application -> fqdn ) -> replaceStart ( ',' , '' ) -> trim ();
$this -> application -> fqdn = str ( $this -> application -> fqdn ) -> trim () -> explode ( ',' ) -> map ( function ( $domain ) {
return str ( $domain ) -> trim () -> lower ();
});
$this -> application -> fqdn = $this -> application -> fqdn -> unique () -> implode ( ',' );
$this -> application -> save ();
}
2023-09-26 12:45:52 +00:00
public function instantSave ()
{
2023-09-25 13:48:43 +00:00
$this -> submit ();
}
2023-11-17 19:08:21 +00:00
public function instantSaveAdvanced ()
{
2024-03-04 10:01:14 +00:00
if ( ! $this -> application -> service -> destination -> server -> isLogDrainEnabled ()) {
2023-11-22 13:21:03 +00:00
$this -> application -> is_log_drain_enabled = false ;
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'error' , 'Log drain is not enabled on the server. Please enable it first.' );
2023-11-22 13:21:03 +00:00
return ;
}
$this -> application -> save ();
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'success' , 'You need to restart the service for the changes to take effect.' );
2023-11-17 19:08:21 +00:00
}
2023-09-25 13:48:43 +00:00
public function delete ()
{
try {
$this -> application -> delete ();
2024-02-22 13:53:42 +00:00
$this -> dispatch ( 'success' , 'Application deleted.' );
2023-12-27 15:45:01 +00:00
return redirect () -> route ( 'project.service.configuration' , $this -> parameters );
2023-09-25 13:48:43 +00:00
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
}
2023-09-25 10:49:55 +00:00
public function mount ()
{
2023-09-25 13:48:43 +00:00
$this -> parameters = get_route_parameters ();
2023-09-25 10:49:55 +00:00
}
2023-09-22 09:23:49 +00:00
public function submit ()
{
try {
2024-04-08 10:15:44 +00:00
check_domain_usage ( resource : $this -> application );
2023-09-22 09:23:49 +00:00
$this -> validate ();
$this -> application -> save ();
2023-09-27 10:45:53 +00:00
updateCompose ( $this -> application );
2024-04-02 13:15:43 +00:00
if ( str ( $this -> application -> fqdn ) -> contains ( ',' )) {
2024-04-08 09:16:25 +00:00
$this -> dispatch ( 'warning' , 'Some services do not support multiple domains, which can lead to problems and is NOT RECOMMENDED.<br><br>Only use multiple domains if you know what you are doing.' );
2024-04-02 13:15:43 +00:00
} else {
$this -> dispatch ( 'success' , 'Service saved.' );
}
2023-09-22 09:23:49 +00:00
} catch ( \Throwable $e ) {
2023-09-27 10:45:53 +00:00
return handleError ( $e , $this );
2023-09-22 09:23:49 +00:00
} finally {
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'generateDockerCompose' );
2023-09-22 09:23:49 +00:00
}
}
}