setup session

This commit is contained in:
Jackzie 2025-04-15 17:18:08 -05:00
parent 786aa30183
commit 2773305304
4 changed files with 111 additions and 18 deletions

62
server/Cargo.lock generated
View file

@ -1331,7 +1331,7 @@ dependencies = [
"num-integer",
"num-iter",
"num-traits",
"rand",
"rand 0.8.5",
"smallvec",
"zeroize",
]
@ -1613,8 +1613,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_chacha 0.3.1",
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
dependencies = [
"rand_chacha 0.9.0",
"rand_core 0.9.3",
"zerocopy",
]
[[package]]
@ -1624,7 +1635,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.3",
]
[[package]]
@ -1636,6 +1657,15 @@ dependencies = [
"getrandom 0.2.15",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom 0.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.5.11"
@ -1730,7 +1760,7 @@ dependencies = [
"num_cpus",
"parking_lot",
"pin-project-lite",
"rand",
"rand 0.8.5",
"ref-cast",
"rocket_codegen",
"rocket_http",
@ -1748,6 +1778,19 @@ dependencies = [
"yansi",
]
[[package]]
name = "rocket-session-store"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fec8901f53c6e4cc677c25771b8629a65f745869e2da77490f3580a0b09e1c5"
dependencies = [
"rand 0.9.0",
"rocket",
"serde",
"serde_json",
"thiserror 1.0.69",
]
[[package]]
name = "rocket_codegen"
version = "0.5.1"
@ -1819,7 +1862,7 @@ dependencies = [
"num-traits",
"pkcs1",
"pkcs8",
"rand_core",
"rand_core 0.6.4",
"signature",
"spki",
"subtle",
@ -1984,7 +2027,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
dependencies = [
"digest",
"rand_core",
"rand_core 0.6.4",
]
[[package]]
@ -2162,7 +2205,7 @@ dependencies = [
"memchr",
"once_cell",
"percent-encoding",
"rand",
"rand 0.8.5",
"rsa",
"serde",
"sha1",
@ -2202,7 +2245,7 @@ dependencies = [
"md-5",
"memchr",
"once_cell",
"rand",
"rand 0.8.5",
"serde",
"serde_json",
"sha2",
@ -2281,6 +2324,7 @@ dependencies = [
"int-enum",
"log",
"rocket",
"rocket-session-store",
"rocket_dyn_templates",
"serde",
"serde_json",

View file

@ -16,4 +16,5 @@ serde_json = "1.0.140"
int-enum = "1.2.0"
dotenvy = "0.15.7"
rocket_dyn_templates = { version = "0.2.0", features = ["handlebars"] }
humanize-bytes = "1.0.6"
humanize-bytes = "1.0.6"
rocket-session-store = "0.2.1"

View file

@ -1,15 +1,21 @@
use std::sync::Arc;
use std::time::Duration;
use log::{debug, error, info, trace, warn};
use rocket::{catch, launch, routes, Request, State};
use rocket::data::ByteUnit;
use rocket::fs::{relative, FileServer};
use rocket::futures::AsyncWriteExt;
use rocket::http::private::cookie::CookieBuilder;
use rocket::serde::Serialize;
use rocket_dyn_templates::handlebars::{handlebars_helper, Context, Handlebars, Helper, HelperResult, Output, RenderContext};
use rocket_dyn_templates::Template;
use rocket_session_store::memory::MemoryStore;
use rocket_session_store::SessionStore;
use sqlx::{migrate, Pool, Postgres};
use sqlx::postgres::PgPoolOptions;
use sqlx::types::Json;
use tokio::sync::Mutex;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use crate::managers::libraries::LibraryManager;
use crate::managers::repos::RepoManager;
use crate::objs::library::Library;
@ -31,6 +37,11 @@ pub type DB = Pool<Postgres>;
const MAX_UPLOAD_SIZE: ByteUnit = ByteUnit::Mebibyte(100_000);
#[derive(Clone, Debug, Serialize)]
struct SessionData {
user_name: String
}
#[launch]
async fn rocket() -> _ {
setup_logger();
@ -62,18 +73,26 @@ async fn rocket() -> _ {
Arc::new(Mutex::new(manager))
};
let memory_store: MemoryStore::<SessionData> = MemoryStore::default();
let store: SessionStore<SessionData> = SessionStore {
store: Box::new(memory_store),
name: "storage-session".into(),
duration: Duration::from_secs(3600 * 24 * 14),
// The cookie builder is used to set the cookie's path and other options.
// Name and value don't matter, they'll be overridden on each request.
cookie_builder: CookieBuilder::new("", "")
// Most web apps will want to use "/", but if your app is served from
// `example.com/myapp/` for example you may want to use "/myapp/" (note the trailing
// slash which prevents the cookie from being sent for `example.com/myapp2/`).
.path("/")
};
rocket::build()
.manage(pool)
.manage(repo_manager)
.manage(libraries_manager)
.mount("/static", FileServer::from(relative!("static")))
.mount("/api/library", routes![
api::library::move_file, api::library::upload_file, api::library::download_file, api::library::list_files, api::library::get_file, api::library::delete_file,
])
.mount("/", routes![
ui::help::about,
ui::user::index, ui::user::redirect_list_library_files, ui::user::list_library_files, ui::user::get_library_file
])
.attach(store.fairing())
.attach(Template::custom(|engines| {
let hb = &mut engines.handlebars;
@ -83,6 +102,15 @@ async fn rocket() -> _ {
hb.register_helper("is-active-exact", Box::new(helpers::is_active));
}))
.mount("/static", FileServer::from(relative!("static")))
.mount("/api/library", routes![
api::library::move_file, api::library::upload_file, api::library::download_file, api::library::list_files, api::library::get_file, api::library::delete_file,
])
.mount("/", routes![
ui::help::about,
ui::user::index, ui::user::redirect_list_library_files, ui::user::list_library_files, ui::user::get_library_file,
ui::help::test_get, ui::help::test_set
])
}
#[catch(404)]

View file

@ -1,7 +1,27 @@
use rocket::{get, Route};
use rocket::serde::json::Json;
use rocket_dyn_templates::{context, Template};
use rocket_session_store::{Session, SessionResult};
use serde::Serialize;
use crate::SessionData;
#[get("/help/about")]
pub fn about(route: &Route) -> Template {
Template::render("about", context! { route: route.uri.path() })
}
#[get("/test/set")]
pub async fn test_set(session: Session<'_, SessionData>) -> &str {
session.set(SessionData {
user_name: "test".to_string()
}).await;
"set."
}
#[get("/test/get")]
pub async fn test_get(session: Session<'_, SessionData>) -> Result<Json<SessionData>, String> {
session.get().await
.map_err(|e| e.to_string())?
.map(|d| Json(d))
.ok_or_else(|| "Could not find user".to_string())
}