Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: Add Sqlite session backend #298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actix-session/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changes

## Unreleased - 2021-xx-xx

- Added Sqlite session backend

## 0.7.2 - 2022-09-11
- Set SameSite attribute when adding a session removal cookie. [#284]
Expand Down
10 changes: 8 additions & 2 deletions actix-session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cookie-session = []
redis-actor-session = ["actix-redis", "actix", "futures-core", "rand"]
redis-rs-session = ["redis", "rand"]
redis-rs-tls-session = ["redis-rs-session", "redis/tokio-native-tls-comp"]
sqlite-session = ["rusqlite", "r2d2", "r2d2_sqlite", "rand"]

[dependencies]
actix-service = "2"
Expand All @@ -48,16 +49,21 @@ futures-core = { version = "0.3.7", default-features = false, optional = true }
# redis-rs-session
redis = { version = "0.21", default-features = false, features = ["aio", "tokio-comp", "connection-manager"], optional = true }

# sqlite-session
rusqlite = { version = "0.27", features = ["bundled", "chrono"], optional = true }
r2d2 = { version = "0.8", optional = true }
r2d2_sqlite = { version = "0.20", optional = true }

[dev-dependencies]
actix-session = { path = ".", features = ["cookie-session", "redis-actor-session", "redis-rs-session"] }
actix-session = { path = ".", features = ["cookie-session", "sqlite-session"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actix-session = { path = ".", features = ["cookie-session", "sqlite-session"] }
actix-session = { path = ".", features = ["cookie-session", "redis-actor-session", "sqlite-session"] }

We should keep enabling all features for the test suite, that's where your error is coming from.

actix-test = "0.1.0-beta.10"
actix-web = { version = "4", default_features = false, features = ["cookies", "secure-cookies", "macros"] }
env_logger = "0.9"
log = "0.4"

[[example]]
name = "basic"
required-features = ["redis-actor-session"]
required-features = ["sqlite-session"]
Comment on lines -60 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a new example for sqlite instead of modifying the existing one for Redis.


[[example]]
name = "authentication"
Expand Down
34 changes: 23 additions & 11 deletions actix-session/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use actix_session::{storage::RedisActorSessionStore, Session, SessionMiddleware};
use actix_web::{cookie::Key, middleware, web, App, Error, HttpRequest, HttpServer, Responder};
use actix_session::{storage::SqliteSessionStore, Session, SessionMiddleware};
use actix_web::{
cookie::{time::Duration, Key},
middleware, web, App, Error, HttpRequest, HttpServer, Responder,
};
use r2d2_sqlite::{self, SqliteConnectionManager};

/// simple handler
async fn index(req: HttpRequest, session: Session) -> Result<impl Responder, Error> {
println!("{:?}", req);

async fn index(_req: HttpRequest, session: Session) -> Result<impl Responder, Error> {
// session
if let Some(count) = session.get::<i32>("counter")? {
println!("SESSION value: {}", count);
session.insert("counter", count + 1)?;
} else {
session.insert("counter", 1)?;
}

Ok("Welcome!")
}

Expand All @@ -23,17 +24,28 @@ async fn main() -> std::io::Result<()> {
// The signing key would usually be read from a configuration file/environment variables.
let signing_key = Key::generate();

let manager = SqliteConnectionManager::file("sessions.db");
let pool = r2d2::Pool::<r2d2_sqlite::SqliteConnectionManager>::new(manager).unwrap();

let sqlite_session_store = SqliteSessionStore::new(pool, true).unwrap();

log::info!("starting HTTP server at http://localhost:8080");

HttpServer::new(move || {
App::new()
// enable logger
.wrap(middleware::Logger::default())
// cookie session middleware
.wrap(SessionMiddleware::new(
RedisActorSessionStore::new("127.0.0.1:6379"),
signing_key.clone(),
))
.wrap(
SessionMiddleware::builder(sqlite_session_store.clone(), signing_key.clone())
.cookie_name(String::from("session"))
.cookie_secure(false)
.session_lifecycle(
actix_session::config::PersistentSession::default()
.session_ttl(Duration::seconds(100)),
)
.build(),
)
.wrap(middleware::Logger::default())
// register simple route, handle all methods
.service(web::resource("/").to(index))
})
Expand Down
11 changes: 10 additions & 1 deletion actix-session/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ mod redis_actor;
#[cfg(feature = "redis-rs-session")]
mod redis_rs;

#[cfg(any(feature = "redis-actor-session", feature = "redis-rs-session"))]
#[cfg(feature = "sqlite-session")]
mod sqlite;

#[cfg(any(
feature = "redis-actor-session",
feature = "redis-rs-session",
feature = "sqlite-session"
))]
mod utils;

#[cfg(feature = "cookie-session")]
Expand All @@ -26,3 +33,5 @@ pub use cookie::CookieSessionStore;
pub use redis_actor::{RedisActorSessionStore, RedisActorSessionStoreBuilder};
#[cfg(feature = "redis-rs-session")]
pub use redis_rs::{RedisSessionStore, RedisSessionStoreBuilder};
#[cfg(feature = "sqlite-session")]
pub use sqlite::SqliteSessionStore;
Loading