list warrens + explore nested folders
This commit is contained in:
10
backend/src/api/mod.rs
Normal file
10
backend/src/api/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::server::Router;
|
||||
|
||||
mod state;
|
||||
mod warrens;
|
||||
|
||||
pub use state::AppState;
|
||||
|
||||
pub(super) fn router() -> Router {
|
||||
Router::new().nest("/warrens", warrens::router())
|
||||
}
|
||||
21
backend/src/api/state.rs
Normal file
21
backend/src/api/state.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pool: Pool<Postgres>,
|
||||
serve_dir: String,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(pool: Pool<Postgres>, serve_dir: String) -> Self {
|
||||
Self { pool, serve_dir }
|
||||
}
|
||||
|
||||
pub fn pool(&self) -> &Pool<Postgres> {
|
||||
&self.pool
|
||||
}
|
||||
|
||||
pub fn serve_dir(&self) -> &str {
|
||||
&self.serve_dir
|
||||
}
|
||||
}
|
||||
27
backend/src/api/warrens/get_warren_path.rs
Normal file
27
backend/src/api/warrens/get_warren_path.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use axum::Json;
|
||||
use axum::extract::{Path, State};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::Result;
|
||||
use crate::api::AppState;
|
||||
use crate::warrens::Warren;
|
||||
|
||||
use crate::fs::DirectoryEntry;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(super) struct WarrenRequestPath {
|
||||
warren_id: Uuid,
|
||||
rest: Option<String>,
|
||||
}
|
||||
|
||||
pub(super) async fn route(
|
||||
State(state): State<AppState>,
|
||||
Path(WarrenRequestPath { warren_id, rest }): Path<WarrenRequestPath>,
|
||||
) -> Result<Json<Vec<DirectoryEntry>>> {
|
||||
let warren = Warren::get(state.pool(), &warren_id).await?;
|
||||
|
||||
let entries = warren.read_path(state.serve_dir(), rest).await?;
|
||||
|
||||
Ok(Json(entries))
|
||||
}
|
||||
9
backend/src/api/warrens/list_warrens.rs
Normal file
9
backend/src/api/warrens/list_warrens.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use axum::{Json, extract::State};
|
||||
|
||||
use crate::{Result, api::AppState, warrens::Warren};
|
||||
|
||||
pub(super) async fn route(State(state): State<AppState>) -> Result<Json<Vec<Warren>>> {
|
||||
let warrens = Warren::list(state.pool()).await?;
|
||||
|
||||
Ok(Json(warrens))
|
||||
}
|
||||
13
backend/src/api/warrens/mod.rs
Normal file
13
backend/src/api/warrens/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod get_warren_path;
|
||||
mod list_warrens;
|
||||
|
||||
use axum::routing::get;
|
||||
|
||||
use crate::server::Router;
|
||||
|
||||
pub(super) fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(list_warrens::route))
|
||||
.route("/{warren_id}", get(get_warren_path::route))
|
||||
.route("/{warren_id}/{*rest}", get(get_warren_path::route))
|
||||
}
|
||||
Reference in New Issue
Block a user