Config minimal global session storage
This commit is contained in:
parent
7c7f7b327b
commit
4410eb4240
@ -1,24 +1,30 @@
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, sync::{Arc, Mutex}};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct Sessions {
|
||||
sessions: HashMap<String, SessionData>,
|
||||
sessions: Mutex<HashMap<String, SessionData>>,
|
||||
}
|
||||
|
||||
impl Sessions {
|
||||
pub fn new() -> Self {
|
||||
Sessions {
|
||||
sessions: HashMap::new(),
|
||||
sessions: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, session_id: String, session_data: SessionData) {
|
||||
self.sessions.insert(session_id, session_data);
|
||||
pub fn insert(&self, session_id: String, session_data: SessionData) {
|
||||
let mut map = self.sessions.lock().unwrap();
|
||||
map.insert(session_id, session_data);
|
||||
}
|
||||
|
||||
pub fn get(&self, session_id: &str) -> Option<&SessionData> {
|
||||
self.sessions.get(session_id)
|
||||
pub fn get(&self, session_id: &str) -> Option<SessionData> {
|
||||
let map = self.sessions.lock().unwrap();
|
||||
|
||||
match map.get(session_id) {
|
||||
Some(s) => Some(s.clone()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,3 +32,9 @@ impl Sessions {
|
||||
pub struct SessionData {
|
||||
user_id: i32,
|
||||
}
|
||||
|
||||
impl SessionData {
|
||||
pub fn new(user_id: i32) -> Self {
|
||||
SessionData { user_id }
|
||||
}
|
||||
}
|
||||
|
20
src/controller/login.rs
Normal file
20
src/controller/login.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use maud::Markup;
|
||||
use rocket::{form::Form, http::Status, State};
|
||||
|
||||
use crate::auth::session::{SessionData, Sessions};
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct LoginData {
|
||||
email: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[get("/login")]
|
||||
pub fn login(sessions: &State<Sessions>) -> &'static str {
|
||||
sessions.insert(
|
||||
"123456".into(),
|
||||
SessionData::new(1),
|
||||
);
|
||||
|
||||
":D"
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
use maud::Markup;
|
||||
|
||||
pub mod login;
|
||||
pub mod register;
|
||||
pub mod user;
|
||||
|
||||
|
@ -14,6 +14,9 @@ fn rocket() -> _ {
|
||||
.manage(auth::session::Sessions::new())
|
||||
.register("/", catchers![view::not_authorized])
|
||||
.mount("/", routes![controller::index,])
|
||||
.mount("/f", routes![controller::user::create_user,])
|
||||
.mount("/f", routes![
|
||||
controller::user::create_user,
|
||||
controller::login::login,
|
||||
])
|
||||
.mount("/static", FileServer::from("static"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user