-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathtunnel.rs
More file actions
67 lines (63 loc) · 1.96 KB
/
tunnel.rs
File metadata and controls
67 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::routes::errors::ErrorResponse;
use crate::state::AppState;
use crate::tunnel::TunnelInfo;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
use std::sync::Arc;
/// Start the tunnel
#[utoipa::path(
post,
path = "/tunnel/start",
responses(
(status = 200, description = "Tunnel started successfully", body = TunnelInfo),
(status = 400, description = "Bad request", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse)
)
)]
#[axum::debug_handler]
pub async fn start_tunnel(State(state): State<Arc<AppState>>) -> Response {
match state.tunnel_manager.start().await {
Ok(info) => (StatusCode::OK, Json(info)).into_response(),
Err(e) => {
tracing::error!("Failed to start tunnel: {}", e);
ErrorResponse::internal(e.to_string()).into_response()
}
}
}
/// Stop the tunnel
#[utoipa::path(
post,
path = "/tunnel/stop",
responses(
(status = 200, description = "Tunnel stopped successfully"),
(status = 500, description = "Internal server error", body = ErrorResponse)
)
)]
pub async fn stop_tunnel(State(state): State<Arc<AppState>>) -> Response {
state.tunnel_manager.stop(true).await;
StatusCode::OK.into_response()
}
/// Get tunnel info
#[utoipa::path(
get,
path = "/tunnel/status",
responses(
(status = 200, description = "Tunnel info", body = TunnelInfo)
)
)]
pub async fn get_tunnel_status(State(state): State<Arc<AppState>>) -> Response {
let info = state.tunnel_manager.get_info().await;
(StatusCode::OK, Json(info)).into_response()
}
pub fn routes(state: Arc<AppState>) -> Router {
Router::new()
.route("/tunnel/start", post(start_tunnel))
.route("/tunnel/stop", post(stop_tunnel))
.route("/tunnel/status", get(get_tunnel_status))
.with_state(state)
}