-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
661 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[async_std::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
tide::log::start(); | ||
let mut app = tide::new(); | ||
|
||
app.middleware(tide::sessions::SessionMiddleware::new( | ||
tide::sessions::MemoryStore::new(), | ||
std::env::var("TIDE_SECRET") | ||
.expect( | ||
"Please provide a TIDE_SECRET value of at \ | ||
least 32 bytes in order to run this example", | ||
) | ||
.as_bytes(), | ||
)); | ||
|
||
app.middleware(tide::utils::Before( | ||
|mut request: tide::Request<()>| async move { | ||
let session = request.session_mut(); | ||
let visits: usize = session.get("visits").unwrap_or_default(); | ||
session.insert("visits", visits + 1).unwrap(); | ||
request | ||
}, | ||
)); | ||
|
||
app.at("/").get(|req: tide::Request<()>| async move { | ||
let visits: usize = req.session().get("visits").unwrap(); | ||
Ok(format!("you have visited this website {} times", visits)) | ||
}); | ||
|
||
app.at("/reset") | ||
.get(|mut req: tide::Request<()>| async move { | ||
req.session_mut().destroy(); | ||
Ok(tide::Redirect::new("/")) | ||
}); | ||
|
||
app.listen("127.0.0.1:8080").await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.