Skip to content

Commit 8d1f0ba

Browse files
committed
FIX: routes and cors
1 parent a8d1e2d commit 8d1f0ba

6 files changed

Lines changed: 43 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ bollard = "0.18.1"
99
tokio = { version = "1.43.0", features = ["full"] }
1010
serde = { version = "1.0.217", features = ["derive"] }
1111
serde_json = "1.0.138"
12+
tower-http = { version = "0.6.2", features = ["cors"] }
13+
tower = "0.5.2"

src/controllers/docker.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ fn get_docker_client() -> Result<Docker, ErrorStatus> {
99
.map_err(|_| ErrorStatus::new(StatusCode::INTERNAL_SERVER_ERROR, format!("Error connecting to Docker")))
1010
}
1111

12+
pub async fn is_alive() -> impl IntoResponse {
13+
let docker = get_docker_client();
14+
15+
match docker {
16+
Ok(_) => (StatusCode::OK, Json(json!({"message": "Docker is alive"}))).into_response(),
17+
Err(error) => error.into_response()
18+
}
19+
}
20+
1221
pub async fn get_containers() -> impl IntoResponse {
1322
let options = Some(bollard::container::ListContainersOptions::<String> {
1423
all: true,

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ mod routes;
22
mod controllers;
33
mod errors;
44

5+
use axum::http::Method;
56
use routes::setup_routes;
7+
use tower_http::cors::{AllowOrigin, CorsLayer};
68

79
#[tokio::main]
8-
async fn main() {
9-
let app = setup_routes();
10+
async fn main() {
11+
let cors = CorsLayer::new()
12+
.allow_methods(vec![Method::GET, Method::POST, Method::PUT, Method::DELETE])
13+
.allow_origin(AllowOrigin::exact("http://localhost:5173".parse().unwrap()));
14+
15+
let app = setup_routes().layer(cors);
1016

1117
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
1218
axum::serve(listener, app).await.unwrap();

src/routes/docker.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use axum::{Router, routing::get};
1+
use axum::{routing::{get, post}, Router};
22

33
use crate::controllers::docker;
44

55
pub fn create_routes() -> Router {
66
Router::new()
7-
.route("/", get(docker::get_containers))
8-
.route("/{id}", get(docker::get_container))
9-
.route("/start/{id}", get(docker::start_container))
10-
.route("/stop/{id}", get(docker::stop_container))
11-
.route("/restart/{id}", get(docker::restart_container))
7+
.route("/alive", get(docker::is_alive))
8+
.route("/containers", get(docker::get_containers))
9+
.route("/containers/{id}", get(docker::get_container))
10+
.route("/containers/{id}/start", post(docker::start_container))
11+
.route("/containers/{id}/stop", post(docker::stop_container))
12+
.route("/containers/{id}/restart", post(docker::restart_container))
1213
}

src/routes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ mod docker;
55
pub fn setup_routes() -> Router {
66
Router::new()
77
.nest("/docker", docker::create_routes())
8+
89
}

0 commit comments

Comments
 (0)