1+ use axum:: { extract:: Path , http:: StatusCode , response:: IntoResponse , Json } ;
2+ use serde_json:: json;
3+ use bollard:: Docker ;
4+
5+ use crate :: errors:: error_status:: ErrorStatus ;
6+
7+ fn get_docker_client ( ) -> Result < Docker , ErrorStatus > {
8+ Docker :: connect_with_local_defaults ( )
9+ . map_err ( |_| ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , format ! ( "Error connecting to Docker" ) ) )
10+ }
11+
12+ pub async fn get_containers ( ) -> impl IntoResponse {
13+ let options = Some ( bollard:: container:: ListContainersOptions :: < String > {
14+ all : true ,
15+ ..Default :: default ( )
16+ } ) ;
17+
18+ let docker = get_docker_client ( ) ;
19+
20+ let container = match docker {
21+ Ok ( docker) => {
22+ match docker. list_containers ( options) . await {
23+ Ok ( containers) => containers,
24+ Err ( _) => return ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , "Error listing containers" . to_string ( ) ) . into_response ( )
25+ }
26+ } ,
27+ Err ( error) => return error. into_response ( )
28+ } ;
29+
30+ let response = json ! ( { "containers" : container} ) ;
31+ ( StatusCode :: OK , Json ( response) ) . into_response ( )
32+ }
33+
34+ pub async fn get_container ( Path ( id) : Path < String > ) -> impl IntoResponse {
35+ let docker = get_docker_client ( ) ;
36+
37+ let container = match docker {
38+ Ok ( docker) => {
39+ match docker. inspect_container ( & id, None ) . await {
40+ Ok ( container) => container,
41+ Err ( _) => return ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , "Error inspecting container" . to_string ( ) ) . into_response ( )
42+ }
43+ } ,
44+ Err ( error) => return error. into_response ( )
45+ } ;
46+
47+ let response = json ! ( { "container" : container} ) ;
48+ ( StatusCode :: OK , Json ( response) ) . into_response ( )
49+ }
50+
51+ pub async fn start_container ( Path ( id) : Path < String > ) -> impl IntoResponse {
52+ let docker = get_docker_client ( ) ;
53+
54+ match docker {
55+ Ok ( docker) => {
56+ match docker. start_container ( & id, None :: < bollard:: container:: StartContainerOptions < String > > ) . await {
57+ Ok ( _) => ( StatusCode :: OK , Json ( json ! ( { "message" : "Container started" } ) ) ) . into_response ( ) ,
58+ Err ( err) => {
59+ println ! ( "Error starting container {}" , err) ;
60+ ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , "Error starting container" . to_string ( ) )
61+ } . into_response ( )
62+ }
63+ } ,
64+ Err ( error) => error. into_response ( )
65+ }
66+ }
67+
68+ pub async fn stop_container ( Path ( id) : Path < String > ) -> impl IntoResponse {
69+ let docker = get_docker_client ( ) ;
70+
71+ match docker {
72+ Ok ( docker) => {
73+ match docker. stop_container ( & id, None :: < bollard:: container:: StopContainerOptions > ) . await {
74+ Ok ( _) => ( StatusCode :: OK , Json ( json ! ( { "message" : "Container stopped" } ) ) ) . into_response ( ) ,
75+ Err ( _) => ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , "Error stopping container" . to_string ( ) ) . into_response ( )
76+ }
77+ } ,
78+ Err ( error) => error. into_response ( )
79+ }
80+ }
81+
82+ pub async fn restart_container ( Path ( id) : Path < String > ) -> impl IntoResponse {
83+ let docker = get_docker_client ( ) ;
84+
85+ match docker {
86+ Ok ( docker) => {
87+ match docker. restart_container ( & id, None :: < bollard:: container:: RestartContainerOptions > ) . await {
88+ Ok ( _) => ( StatusCode :: OK , Json ( json ! ( { "message" : "Container restarted" } ) ) ) . into_response ( ) ,
89+ Err ( _) => ErrorStatus :: new ( StatusCode :: INTERNAL_SERVER_ERROR , "Error restarting container" . to_string ( ) ) . into_response ( )
90+ }
91+ } ,
92+ Err ( error) => error. into_response ( )
93+ }
94+ }
0 commit comments