Skip to content

Commit e8780b4

Browse files
committed
update for latest changes to async-session pr
1 parent cde2294 commit e8780b4

File tree

2 files changed

+23
-52
lines changed

2 files changed

+23
-52
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ sqlite = ["sqlx/sqlite"]
1111
[dependencies]
1212
async-session = "1.0.2"
1313
sqlx = { version = "0.3.5" }
14-
chrono = "0.4.13"
1514

1615
[patch.crates-io]
1716
async-session = { git = "https://github.com/jbr/async-session", branch = "tide" }

src/sqlite.rs

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
use async_session::{async_trait, base64, log, serde_json, Session, SessionStore};
2-
use chrono::Utc;
1+
use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session, SessionStore};
32
use sqlx::prelude::*;
4-
use sqlx::sqlite::SqlitePool;
3+
use sqlx::{pool::PoolConnection, sqlite::SqlitePool, SqliteConnection};
54

65
#[derive(Clone, Debug)]
76
pub struct SqliteStore {
87
client: SqlitePool,
9-
ttl: chrono::Duration,
10-
prefix: Option<String>,
118
table_name: String,
129
}
1310

@@ -16,12 +13,10 @@ impl SqliteStore {
1613
Self {
1714
client,
1815
table_name: "async_sessions".into(),
19-
ttl: chrono::Duration::days(1),
20-
prefix: None,
2116
}
2217
}
2318

24-
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
19+
pub async fn new(database_url: &str) -> sqlx::Result<Self> {
2520
Ok(Self::from_client(SqlitePool::new(database_url).await?))
2621
}
2722

@@ -37,15 +32,6 @@ impl SqliteStore {
3732
self
3833
}
3934

40-
pub fn with_ttl(mut self, ttl: std::time::Duration) -> Self {
41-
self.ttl = chrono::Duration::from_std(ttl).unwrap();
42-
self
43-
}
44-
45-
pub fn expiry(&self) -> i64 {
46-
(Utc::now() + self.ttl).timestamp()
47-
}
48-
4935
pub async fn migrate(&self) -> sqlx::Result<()> {
5036
log::info!("migrating sessions on `{}`", self.table_name);
5137

@@ -68,15 +54,13 @@ impl SqliteStore {
6854
query.replace("%%TABLE_NAME%%", &self.table_name)
6955
}
7056

71-
async fn connection(&self) -> sqlx::Result<sqlx::pool::PoolConnection<sqlx::SqliteConnection>> {
57+
async fn connection(&self) -> sqlx::Result<PoolConnection<SqliteConnection>> {
7258
self.client.acquire().await
7359
}
7460
}
7561

7662
#[async_trait]
7763
impl SessionStore for SqliteStore {
78-
type Error = sqlx::Error;
79-
8064
async fn load_session(&self, cookie_value: String) -> Option<Session> {
8165
let id = Session::id_from_cookie_value(&cookie_value).ok()?;
8266
let mut connection = self.connection().await.ok()?;
@@ -96,7 +80,7 @@ impl SessionStore for SqliteStore {
9680
serde_json::from_str(&session).ok()?
9781
}
9882

99-
async fn store_session(&self, mut session: Session) -> Option<String> {
83+
async fn store_session(&self, session: Session) -> Option<String> {
10084
let id = session.id();
10185
let string = serde_json::to_string(&session).ok()?;
10286
let mut connection = self.connection().await.ok()?;
@@ -111,16 +95,16 @@ impl SessionStore for SqliteStore {
11195
"#,
11296
))
11397
.bind(&id)
114-
.bind(self.expiry())
98+
.bind(&session.expiry().map(|expiry| expiry.timestamp()))
11599
.bind(&string)
116100
.execute(&mut connection)
117101
.await
118102
.unwrap();
119103

120-
session.take_cookie_value()
104+
session.into_cookie_value()
121105
}
122106

123-
async fn destroy_session(&self, session: Session) -> Result<(), Self::Error> {
107+
async fn destroy_session(&self, session: Session) -> Result {
124108
let id = session.id();
125109
let mut connection = self.connection().await?;
126110
sqlx::query(&self.substitute_table_name(
@@ -131,10 +115,11 @@ impl SessionStore for SqliteStore {
131115
.bind(&id)
132116
.execute(&mut connection)
133117
.await?;
118+
134119
Ok(())
135120
}
136121

137-
async fn clear_store(&self) -> Result<(), Self::Error> {
122+
async fn clear_store(&self) -> Result {
138123
let mut connection = self.connection().await?;
139124
sqlx::query(&self.substitute_table_name(
140125
r#"
@@ -143,35 +128,22 @@ impl SessionStore for SqliteStore {
143128
))
144129
.execute(&mut connection)
145130
.await?;
146-
Ok(())
147-
}
148-
}
149131

150-
#[derive(Debug)]
151-
pub enum Error {
152-
SqlxError(sqlx::Error),
153-
SerdeError(serde_json::Error),
154-
}
155-
156-
impl std::fmt::Display for Error {
157-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158-
match self {
159-
Error::SqlxError(e) => e.fmt(f),
160-
Error::SerdeError(e) => e.fmt(f),
161-
}
132+
Ok(())
162133
}
163-
}
164134

165-
impl From<serde_json::Error> for Error {
166-
fn from(e: serde_json::Error) -> Self {
167-
Self::SerdeError(e)
168-
}
169-
}
135+
async fn cleanup(&self) -> Result {
136+
let mut connection = self.connection().await?;
137+
sqlx::query(&self.substitute_table_name(
138+
r#"
139+
DELETE FROM %%TABLE_NAME%%
140+
WHERE expires < ?
141+
"#,
142+
))
143+
.bind(Utc::now().timestamp())
144+
.execute(&mut connection)
145+
.await?;
170146

171-
impl From<sqlx::Error> for Error {
172-
fn from(e: sqlx::Error) -> Self {
173-
Self::SqlxError(e)
147+
Ok(())
174148
}
175149
}
176-
177-
impl std::error::Error for Error {}

0 commit comments

Comments
 (0)