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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
use chrono::Duration;
///This is the Sessions Config it is used to Setup the SQL database and sets the hashmap saved Memory and Session life spans.
#[derive(Debug, Clone)]
pub struct SqlxSessionConfig {
/// Sessions lifespan
pub(crate) lifespan: Duration,
/// Session cookie name
pub(crate) cookie_name: String,
/// Session cookie path
pub(crate) cookie_path: String,
/// Session ID character length
pub(crate) cookie_len: usize,
/// Session Database name
pub(crate) database: String,
/// Session Database username for login
pub(crate) username: String,
/// Session Database password for login
pub(crate) password: String,
/// Session Database Host address
pub(crate) host: String,
/// Session Database Port address
pub(crate) port: u16,
/// Session Database table name default is async_sessions
pub(crate) table_name: String,
/// Session Database Max Poll Connections. Can not be 0
pub(crate) max_connections: u32,
/// Session Memory lifespan, deturmines when to unload it from memory
/// this works fine since the data can stay in the database till its needed
/// if not yet expired.
pub(crate) memory_lifespan: Duration,
}
impl SqlxSessionConfig {
/// Set session database pools max connections limit.
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn set_max_connections(mut self, max: u32) -> Self {
let max = std::cmp::max(max, 1);
self.max_connections = max;
self
}
/// Set session lifetime (expiration time) within database storage.
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_lifetime(mut self, time: Duration) -> Self {
self.lifespan = time;
self
}
/// Set session lifetime (expiration time) within Memory storage.
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_memory_lifetime(mut self, time: Duration) -> Self {
self.memory_lifespan = time;
self
}
/// Set session cookie name
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_cookie_name(mut self, name: &str) -> Self {
self.cookie_name = name.into();
self
}
/// Set session cookie length
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_cookie_len(mut self, length: usize) -> Self {
self.cookie_len = length;
self
}
/// Set session cookie path
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_cookie_path(mut self, path: &str) -> Self {
self.cookie_path = path.into();
self
}
/// Set session database name
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_database(mut self, database: &str) -> Self {
self.database = database.into();
self
}
/// Set session username
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_username(mut self, username: &str) -> Self {
self.username = username.into();
self
}
/// Set session user password
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_password(mut self, password: &str) -> Self {
self.password = password.into();
self
}
/// Set session database table name
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_table_name(mut self, table_name: &str) -> Self {
self.table_name = table_name.into();
self
}
/// Set session database hostname
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_host(mut self, host: &str) -> Self {
self.host = host.into();
self
}
/// Set session database port
///
/// Call on the fairing before passing it to `rocket.attach()`
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
}
impl Default for SqlxSessionConfig {
fn default() -> Self {
Self {
/// Set to 6hour for default in Database Session stores.
lifespan: Duration::hours(6),
cookie_name: "sqlx_session".into(),
cookie_path: "/".into(),
cookie_len: 16,
database: "".into(),
username: "".into(),
password: "".into(),
host: "localhost".into(),
port: 5432,
table_name: "async_sessions".into(),
max_connections: 5,
/// Unload memory after 60mins if it has not been accessed.
memory_lifespan: Duration::minutes(60),
}
}
}