Aurora2500 / rocket-session-store

An expansible rocket session store.
9 stars 7 forks source link

rocket 0.5.0-rc.2 #5

Open raymclee opened 2 years ago

raymclee commented 2 years ago

hi there! can i use this crate with rocket 0.5.0-rc.2?

justinacolmena commented 1 year ago

Yes.

I have a fully asynchronous build of the "hello world" program using this crate with a few unused dependencies thrown in for good measure. It builds and runs successfully on "stable-x86_64-unknown-linux-gnu unchanged - rustc 1.66.0" but beware "rustc 1.68.0-nightly (935dc0721 2022-12-19) running on x86_64-unknown-linux-gnu" crashes on an internal compiler error with following code.

I am very new to rust (knew nothing at all about it as of the date of the above post) and I don't know much about async programming, but I like the await, Ok(), Err(), Some() and None() functions that let me know exactly what I'm dealing with, hopefully to connect with a PostgreSQL (or any other) database but I have not added any of that into my code yet.

Cargo.toml

[package]
name = "farmgate"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dotenvy = "0.15.6"
comrak = "0.15.0"
bbscope = "0.0.6"
rocket = { version = "0.5.0-rc.2", features = ["secrets"] }
rocket-session-store = "0.2.0"

src/main.rs


use rocket::{
    Rocket,
    get,
    routes,
    launch,
};
use rocket_session_store::{
    memory::MemoryStore,
    SessionStore,
    SessionResult,
    Session,
    CookieConfig,
};

use dotenvy::dotenv;
use std::env;

use rocket::tokio::time::{sleep, Duration};
use rocket::response::content;
use comrak::{markdown_to_html, ComrakOptions};
use bbscope::BBCode;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    let memory_store: MemoryStore::<String> = MemoryStore::default();
    let store: SessionStore<String> = SessionStore {
        store: Box::new(memory_store),
        name: "token".into(),
        duration: Duration::from_secs(3600 * 24 * 3),
        // The cookie config is used to set the cookie's path and other options.
        cookie: CookieConfig::default(),
    };
   let _rocket = rocket::build().attach(store.fairing())
        .mount("/", routes![index])
        .launch()
        .await?;

    Ok(())
}

#[get("/")]
async fn index(session: Session<'_, String>) -> SessionResult<String> {
    let name: Option<String> = session.get().await?;
    if let Some(name) = name {
        Ok(format!("Hello, {}!", name))
    } else {
        Ok("Hello, world!".into())
    }
}