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 } ;
3
2
use sqlx:: prelude:: * ;
4
- use sqlx:: sqlite:: SqlitePool ;
3
+ use sqlx:: { pool :: PoolConnection , sqlite:: SqlitePool , SqliteConnection } ;
5
4
6
5
#[ derive( Clone , Debug ) ]
7
6
pub struct SqliteStore {
8
7
client : SqlitePool ,
9
- ttl : chrono:: Duration ,
10
- prefix : Option < String > ,
11
8
table_name : String ,
12
9
}
13
10
@@ -16,12 +13,10 @@ impl SqliteStore {
16
13
Self {
17
14
client,
18
15
table_name : "async_sessions" . into ( ) ,
19
- ttl : chrono:: Duration :: days ( 1 ) ,
20
- prefix : None ,
21
16
}
22
17
}
23
18
24
- pub async fn new ( database_url : & str ) -> Result < Self , sqlx:: Error > {
19
+ pub async fn new ( database_url : & str ) -> sqlx:: Result < Self > {
25
20
Ok ( Self :: from_client ( SqlitePool :: new ( database_url) . await ?) )
26
21
}
27
22
@@ -37,15 +32,6 @@ impl SqliteStore {
37
32
self
38
33
}
39
34
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
-
49
35
pub async fn migrate ( & self ) -> sqlx:: Result < ( ) > {
50
36
log:: info!( "migrating sessions on `{}`" , self . table_name) ;
51
37
@@ -68,15 +54,13 @@ impl SqliteStore {
68
54
query. replace ( "%%TABLE_NAME%%" , & self . table_name )
69
55
}
70
56
71
- async fn connection ( & self ) -> sqlx:: Result < sqlx :: pool :: PoolConnection < sqlx :: SqliteConnection > > {
57
+ async fn connection ( & self ) -> sqlx:: Result < PoolConnection < SqliteConnection > > {
72
58
self . client . acquire ( ) . await
73
59
}
74
60
}
75
61
76
62
#[ async_trait]
77
63
impl SessionStore for SqliteStore {
78
- type Error = sqlx:: Error ;
79
-
80
64
async fn load_session ( & self , cookie_value : String ) -> Option < Session > {
81
65
let id = Session :: id_from_cookie_value ( & cookie_value) . ok ( ) ?;
82
66
let mut connection = self . connection ( ) . await . ok ( ) ?;
@@ -96,7 +80,7 @@ impl SessionStore for SqliteStore {
96
80
serde_json:: from_str ( & session) . ok ( ) ?
97
81
}
98
82
99
- async fn store_session ( & self , mut session : Session ) -> Option < String > {
83
+ async fn store_session ( & self , session : Session ) -> Option < String > {
100
84
let id = session. id ( ) ;
101
85
let string = serde_json:: to_string ( & session) . ok ( ) ?;
102
86
let mut connection = self . connection ( ) . await . ok ( ) ?;
@@ -111,16 +95,16 @@ impl SessionStore for SqliteStore {
111
95
"# ,
112
96
) )
113
97
. bind ( & id)
114
- . bind ( self . expiry ( ) )
98
+ . bind ( & session . expiry ( ) . map ( |expiry| expiry . timestamp ( ) ) )
115
99
. bind ( & string)
116
100
. execute ( & mut connection)
117
101
. await
118
102
. unwrap ( ) ;
119
103
120
- session. take_cookie_value ( )
104
+ session. into_cookie_value ( )
121
105
}
122
106
123
- async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
107
+ async fn destroy_session ( & self , session : Session ) -> Result {
124
108
let id = session. id ( ) ;
125
109
let mut connection = self . connection ( ) . await ?;
126
110
sqlx:: query ( & self . substitute_table_name (
@@ -131,10 +115,11 @@ impl SessionStore for SqliteStore {
131
115
. bind ( & id)
132
116
. execute ( & mut connection)
133
117
. await ?;
118
+
134
119
Ok ( ( ) )
135
120
}
136
121
137
- async fn clear_store ( & self ) -> Result < ( ) , Self :: Error > {
122
+ async fn clear_store ( & self ) -> Result {
138
123
let mut connection = self . connection ( ) . await ?;
139
124
sqlx:: query ( & self . substitute_table_name (
140
125
r#"
@@ -143,35 +128,22 @@ impl SessionStore for SqliteStore {
143
128
) )
144
129
. execute ( & mut connection)
145
130
. await ?;
146
- Ok ( ( ) )
147
- }
148
- }
149
131
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 ( ( ) )
162
133
}
163
- }
164
134
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 ?;
170
146
171
- impl From < sqlx:: Error > for Error {
172
- fn from ( e : sqlx:: Error ) -> Self {
173
- Self :: SqlxError ( e)
147
+ Ok ( ( ) )
174
148
}
175
149
}
176
-
177
- impl std:: error:: Error for Error { }
0 commit comments