Skip to content

Commit 5b0304f

Browse files
author
Christopher Kolstad
committed
Add tests
1 parent 7d12354 commit 5b0304f

File tree

4 files changed

+647
-15
lines changed

4 files changed

+647
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "actix-session-sqlx"
33
version = "0.1.0"
44
edition = "2021"
5+
authors = ["Christopher Kolstad <[email protected]>", "Simon Hornby <[email protected]>"]
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

src/lib.rs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use sqlx::postgres::PgPoolOptions;
77
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng as _};
88
use time::Duration;
99
use serde_json;
10+
use crate::ConnectionData::{ConnectionPool, ConnectionString};
1011

1112
/// Use Postgres via Sqlx as session storage backend.
1213
///
@@ -39,8 +40,39 @@ use serde_json;
3940
/// .run()
4041
/// .await
4142
/// }
42-
43-
43+
/// ```
44+
/// If you already have a connection pool, you can use something like
45+
/*/// ```no_run
46+
/// use actix_web::{web, App, HttpServer, HttpResponse, Error};
47+
/// use actix_session_sqlx::SqlxPostgresqlSessionStore;
48+
/// use actix_session::SessionMiddleware;
49+
/// use actix_web::cookie::Key;
50+
///
51+
/// // The secret key would usually be read from a configuration file/environment variables.
52+
/// fn get_secret_key() -> Key {
53+
/// # todo!()
54+
/// // [...]
55+
/// }
56+
/// #[actix_web::main]
57+
/// async fn main() -> std::io::Result<()> {
58+
/// use sqlx::postgres::PgPoolOptions;
59+
/// let secret_key = get_secret_key();
60+
/// let pool = PgPoolOptions::find_some_way_to_build_your_pool(psql_connection_string);
61+
/// let store = SqlxPostgresqlSessionStore::from_pool(pool).await.expect("Could not build session store");
62+
///
63+
/// HttpServer::new(move ||
64+
/// App::new()
65+
/// .wrap(SessionMiddleware::new(
66+
/// store.clone(),
67+
/// secret_key.clone()
68+
/// ))
69+
/// .default_service(web::to(|| HttpResponse::Ok())))
70+
/// .bind(("127.0.0.1", 8080))?
71+
/// .run()
72+
/// .await
73+
/// }
74+
/// ```
75+
*/
4476
#[derive(Clone)]
4577
struct CacheConfiguration {
4678
cache_keygen: Arc<dyn Fn(&str) -> String + Send + Sync>,
@@ -74,35 +106,54 @@ fn generate_session_key() -> SessionKey {
74106
impl SqlxPostgresqlSessionStore {
75107
pub fn builder<S: Into<String>>(connection_string: S) -> SqlxPostgresqlSessionStoreBuilder {
76108
SqlxPostgresqlSessionStoreBuilder {
77-
connection_string: connection_string.into(),
109+
connection_data: ConnectionString(connection_string.into()),
78110
configuration: CacheConfiguration::default()
79111
}
80112
}
81113

82114
pub async fn new<S: Into<String>>(connection_string: S) -> Result<SqlxPostgresqlSessionStore, anyhow::Error> {
83115
Self::builder(connection_string).build().await
84116
}
117+
118+
pub async fn from_pool(pool: Pool<Postgres>) -> SqlxPostgresqlSessionStoreBuilder {
119+
SqlxPostgresqlSessionStoreBuilder {
120+
connection_data: ConnectionPool(pool.clone()),
121+
configuration: CacheConfiguration::default()
122+
}
123+
}
124+
}
125+
126+
pub enum ConnectionData {
127+
ConnectionString(String),
128+
ConnectionPool(Pool<Postgres>)
85129
}
86130

87131
#[must_use]
88132
pub struct SqlxPostgresqlSessionStoreBuilder {
89-
connection_string: String,
133+
connection_data: ConnectionData,
90134
configuration: CacheConfiguration,
91135
}
92136

93137
impl SqlxPostgresqlSessionStoreBuilder {
94138
pub async fn build(self) -> Result<SqlxPostgresqlSessionStore, anyhow::Error> {
95-
PgPoolOptions::new()
96-
.max_connections(1)
97-
.connect(self.connection_string.as_str())
98-
.await
99-
.map_err(Into::into)
100-
.map(|pool| {
101-
SqlxPostgresqlSessionStore {
102-
client_pool: pool,
103-
configuration: self.configuration
104-
}
139+
match self.connection_data {
140+
ConnectionString(conn_string) => {
141+
PgPoolOptions::new()
142+
.max_connections(1)
143+
.connect(conn_string.as_str())
144+
.await
145+
.map_err(Into::into)
146+
.map(|pool| {
147+
SqlxPostgresqlSessionStore {
148+
client_pool: pool,
149+
configuration: self.configuration
150+
}
151+
})
152+
},
153+
ConnectionPool(pool) => Ok(SqlxPostgresqlSessionStore {
154+
client_pool: pool, configuration: self.configuration
105155
})
156+
}
106157
}
107158
}
108159
pub(crate) type SessionState = HashMap<String, String>;

tests/session_store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod test_helpers;
12
#[cfg(test)]
23
pub mod tests {
34
use actix_session::storage::SessionStore;
@@ -54,6 +55,6 @@ pub mod tests {
5455
println!("{:#?}", data);
5556
assert!(data
5657
.is_ok());
57-
//acceptance_test_suite(move || postgres_store.clone(), true).await;
58+
super::test_helpers::acceptance_test_suite(move || postgres_store.clone(), true).await;
5859
}
5960
}

0 commit comments

Comments
 (0)