59 lines
1.5 KiB
Rust
59 lines
1.5 KiB
Rust
use axum::{
|
|
Json,
|
|
extract::{Path, State},
|
|
};
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::{error::ApiError, service::UpstreamPayload, state::AppState};
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct WorkspacePath {
|
|
pub workspace_id: String,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct WorkspaceUpstreamPath {
|
|
pub workspace_id: String,
|
|
pub upstream_id: String,
|
|
}
|
|
|
|
pub async fn list_upstreams(
|
|
Path(path): Path<WorkspacePath>,
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let items = state
|
|
.service
|
|
.list_workspace_upstreams(&path.workspace_id.as_str().into())
|
|
.await?;
|
|
Ok(Json(json!({ "items": items })))
|
|
}
|
|
|
|
pub async fn create_upstream(
|
|
Path(path): Path<WorkspacePath>,
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<UpstreamPayload>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let upstream = state
|
|
.service
|
|
.save_workspace_upstream(&path.workspace_id.as_str().into(), None, payload)
|
|
.await?;
|
|
Ok(Json(json!(upstream)))
|
|
}
|
|
|
|
pub async fn update_upstream(
|
|
Path(path): Path<WorkspaceUpstreamPath>,
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<UpstreamPayload>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let upstream = state
|
|
.service
|
|
.save_workspace_upstream(
|
|
&path.workspace_id.as_str().into(),
|
|
Some(&path.upstream_id.as_str().into()),
|
|
payload,
|
|
)
|
|
.await?;
|
|
Ok(Json(json!(upstream)))
|
|
}
|