Skip to content

Commit 5e5af7e

Browse files
authored
Merge pull request #18 from AaronErhardt/update-deps
Update dependencies
2 parents 4876e78 + c45c113 commit 5e5af7e

File tree

5 files changed

+160
-131
lines changed

5 files changed

+160
-131
lines changed

Cargo.toml

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
[package]
22
name = "async-mongodb-session"
3-
version = "2.1.0"
3+
version = "3.0.0"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/yoshuawuyts/async-mongodb-session"
66
documentation = "https://docs.rs/async-mongodb-session"
77
description = "An async-session implementation for MongoDB"
88
readme = "README.md"
9-
edition = "2018"
9+
edition = "2021"
1010
keywords = ["tide", "web", "async", "session", "mongodb"]
1111
categories = [
1212
"network-programming",
1313
"asynchronous",
14-
"web-programming::http-server"
14+
"web-programming::http-server",
1515
]
1616
authors = [
17-
"Yoshua Wuyts <yoshuawuyts@gmail.com>",
18-
"Irina Shestak <shestak.irina@gmail.com>",
19-
"Anton Whalley <anton@venshare.com>",
20-
"Javier Viola <pepoviola@gmail.com>"
17+
"Yoshua Wuyts <yoshuawuyts@gmail.com>",
18+
"Irina Shestak <shestak.irina@gmail.com>",
19+
"Anton Whalley <anton@venshare.com>",
20+
"Javier Viola <pepoviola@gmail.com>",
21+
"Aaron Erhardt <aaron.erhardt@t-online.de>",
2122
]
2223

2324
[features]
25+
default = ["async-std-runtime"]
26+
async-std-runtime = ["mongodb/async-std-runtime"]
27+
tokio-runtime = ["mongodb/tokio-runtime"]
2428

2529
[dependencies]
26-
mongodb = { version = "2.2.1", default-features = false, features = ["async-std-runtime", "bson-chrono-0_4"] }
27-
# can not go higher due to dev-dependencie "tide" which requires "async-session" in version 2.0.1
28-
async-session = "2.0.1"
30+
async-session = "3"
31+
mongodb = { package = "mongodb", version = "2.3", default-features = false, features = [
32+
"bson-chrono-0_4",
33+
] }
2934

3035
[dev-dependencies]
31-
async-std = { version = "1.11.0", features = ["attributes"] }
32-
rand = {version = "0.8.5"}
33-
lazy_static = "1.4.0"
34-
tide = "0.16"
36+
async-std = { version = "1.12", features = ["attributes"] }
37+
lazy_static = "1.4"
38+
rand = "0.8.5"
39+
tokio = { version = "1.20", features = ["rt"] }

examples/tide/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "tide"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
async-mongodb-session = { path = "../../" }
10+
tide = "0.17.0-beta.1"
11+
async-std = "1.10"

examples/mongodb_session_store.rs renamed to examples/tide/src/main.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate async_mongodb_session;
2-
extern crate tide;
3-
41
use async_mongodb_session::MongodbSessionStore;
52

63
#[async_std::main]

src/lib.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@
1313
1414
#![forbid(unsafe_code, future_incompatible, rust_2018_idioms)]
1515
#![deny(missing_debug_implementations, nonstandard_style)]
16-
#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]
16+
#![warn(missing_docs, rustdoc::missing_doc_code_examples, unreachable_pub)]
1717

1818
use async_session::chrono::{Duration, Utc};
1919
use async_session::{async_trait, Result, Session, SessionStore};
20-
use mongodb::{bson, Collection};
21-
use mongodb::bson::{doc, Bson, Document};
20+
use mongodb::bson::{self, doc, Bson, Document};
2221
use mongodb::options::{ReplaceOptions, SelectionCriteria};
2322
use mongodb::Client;
2423

2524
/// A MongoDB session store.
2625
#[derive(Debug, Clone)]
2726
pub struct MongodbSessionStore {
28-
client: mongodb::Client,
29-
db: String,
30-
coll_name: String,
27+
collection: mongodb::Collection<Document>,
28+
database: mongodb::Database,
3129
}
3230

3331
impl MongodbSessionStore {
@@ -40,9 +38,9 @@ impl MongodbSessionStore {
4038
/// .await?;
4139
/// # Ok(()) }) }
4240
/// ```
43-
pub async fn new(uri: &str, db: &str, coll_name: &str) -> mongodb::error::Result<Self> {
41+
pub async fn new(uri: &str, db_name: &str, coll_name: &str) -> mongodb::error::Result<Self> {
4442
let client = Client::with_uri_str(uri).await?;
45-
let middleware = Self::from_client(client, db, coll_name);
43+
let middleware = Self::from_client(client, db_name, coll_name);
4644
middleware.create_expire_index("expireAt", 0).await?;
4745
Ok(middleware)
4846
}
@@ -66,16 +64,17 @@ impl MongodbSessionStore {
6664
/// let store = MongodbSessionStore::from_client(client, "db_name", "collection");
6765
/// # Ok(()) }) }
6866
/// ```
69-
pub fn from_client(client: Client, db: &str, coll_name: &str) -> Self {
67+
pub fn from_client(client: Client, db_name: &str, coll_name: &str) -> Self {
68+
let database = client.database(db_name);
69+
let collection = database.collection(coll_name);
7070
Self {
71-
client,
72-
db: db.to_string(),
73-
coll_name: coll_name.to_string(),
71+
database,
72+
collection,
7473
}
7574
}
7675

7776
/// Initialize the default expiration mechanism, based on the document expiration
78-
/// that mongodb provides https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time.
77+
/// that mongodb provides <https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time>.
7978
/// The default ttl applyed to sessions without expiry is 20 minutes.
8079
/// If the `expireAt` date field contains a date in the past, mongodb considers the document expired and will be deleted.
8180
/// Note: mongodb runs the expiration logic every 60 seconds.
@@ -89,8 +88,7 @@ impl MongodbSessionStore {
8988
/// # Ok(()) }) }
9089
/// ```
9190
pub async fn initialize(&self) -> Result {
92-
let _ = &self.index_on_expiry_at().await?;
93-
Ok(())
91+
self.index_on_expiry_at().await
9492
}
9593

9694
/// private associated function
@@ -102,7 +100,7 @@ impl MongodbSessionStore {
102100
expire_after_seconds: u32,
103101
) -> mongodb::error::Result<()> {
104102
let create_index = doc! {
105-
"createIndexes": &self.coll_name,
103+
"createIndexes": self.collection.name(),
106104
"indexes": [
107105
{
108106
"key" : { field_name: 1 },
@@ -111,8 +109,7 @@ impl MongodbSessionStore {
111109
}
112110
]
113111
};
114-
self.client
115-
.database(&self.db)
112+
self.database
116113
.run_command(
117114
create_index,
118115
SelectionCriteria::ReadPreference(mongodb::options::ReadPreference::Primary),
@@ -123,7 +120,7 @@ impl MongodbSessionStore {
123120

124121
/// Create a new index for the `expireAt` property, allowing to expire sessions at a specific clock time.
125122
/// If the `expireAt` date field contains a date in the past, mongodb considers the document expired and will be deleted.
126-
/// https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time
123+
/// <https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time>
127124
/// ```rust
128125
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
129126
/// # use async_mongodb_session::MongodbSessionStore;
@@ -142,14 +139,13 @@ impl MongodbSessionStore {
142139
#[async_trait]
143140
impl SessionStore for MongodbSessionStore {
144141
async fn store_session(&self, session: Session) -> Result<Option<String>> {
145-
let coll = self.client.database(&self.db).collection(&self.coll_name);
142+
let coll = &self.collection;
146143

147144
let value = bson::to_bson(&session)?;
148145
let id = session.id();
149146
let query = doc! { "session_id": id };
150147
let expire_at = match session.expiry() {
151148
None => Utc::now() + Duration::from_std(std::time::Duration::from_secs(1200)).unwrap(),
152-
153149
Some(expiry) => *{ expiry },
154150
};
155151
let replacement = doc! { "session_id": id, "session": value, "expireAt": expire_at, "created": Utc::now() };
@@ -162,7 +158,7 @@ impl SessionStore for MongodbSessionStore {
162158

163159
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
164160
let id = Session::id_from_cookie_value(&cookie_value)?;
165-
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
161+
let coll = &self.collection;
166162
let filter = doc! { "session_id": id };
167163
match coll.find_one(filter, None).await? {
168164
None => Ok(None),
@@ -175,7 +171,7 @@ impl SessionStore for MongodbSessionStore {
175171
// https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
176172
// This prevents those documents being returned
177173
if let Some(expiry_at) = doc.get("expireAt").and_then(Bson::as_datetime) {
178-
if expiry_at < &Utc::now().into() {
174+
if expiry_at.to_chrono() < Utc::now() {
179175
return Ok(None);
180176
}
181177
}
@@ -185,14 +181,14 @@ impl SessionStore for MongodbSessionStore {
185181
}
186182

187183
async fn destroy_session(&self, session: Session) -> Result {
188-
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
184+
let coll = &self.collection;
189185
coll.delete_one(doc! { "session_id": session.id() }, None)
190186
.await?;
191187
Ok(())
192188
}
193189

194190
async fn clear_store(&self) -> Result {
195-
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
191+
let coll = &self.collection;
196192
coll.drop(None).await?;
197193
self.initialize().await?;
198194
Ok(())

0 commit comments

Comments
 (0)