mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
update actix-session everywhere
This commit is contained in:
parent
5c9f69bb07
commit
c557986915
24
Cargo.lock
generated
24
Cargo.lock
generated
@ -390,24 +390,6 @@ dependencies = [
|
|||||||
"time 0.3.11",
|
"time 0.3.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "actix-session"
|
|
||||||
version = "0.6.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0c9138a66462f1e65da829f9c0de81b44a96dfe193a4f19bfea32ee2be312368"
|
|
||||||
dependencies = [
|
|
||||||
"actix-service",
|
|
||||||
"actix-utils",
|
|
||||||
"actix-web",
|
|
||||||
"anyhow",
|
|
||||||
"async-trait",
|
|
||||||
"derive_more",
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
"time 0.3.11",
|
|
||||||
"tracing",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-session"
|
name = "actix-session"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
@ -1136,7 +1118,7 @@ name = "basics"
|
|||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-session 0.5.0",
|
"actix-session 0.7.0",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"async-stream",
|
"async-stream",
|
||||||
"env_logger 0.9.0",
|
"env_logger 0.9.0",
|
||||||
@ -1677,7 +1659,7 @@ dependencies = [
|
|||||||
name = "cookie-session"
|
name = "cookie-session"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-session 0.6.2",
|
"actix-session 0.7.0",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"env_logger 0.9.0",
|
"env_logger 0.9.0",
|
||||||
"log",
|
"log",
|
||||||
@ -6207,7 +6189,7 @@ name = "todo"
|
|||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-session 0.5.0",
|
"actix-session 0.7.0",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"actix-web-lab",
|
"actix-web-lab",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
@ -5,6 +5,6 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
actix-session = { version = "0.6", features = ["cookie-session"] }
|
actix-session = { version = "0.7", features = ["cookie-session"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
|
@ -4,9 +4,9 @@ version = "1.0.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
|
||||||
actix-files = "0.6"
|
actix-files = "0.6"
|
||||||
actix-session = "0.5"
|
actix-session = { version = "0.7", features = ["cookie-session"] }
|
||||||
|
actix-web = "4"
|
||||||
|
|
||||||
async-stream = "0.3"
|
async-stream = "0.3"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use std::convert::Infallible;
|
use std::{convert::Infallible, io};
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use actix_files::{Files, NamedFile};
|
use actix_files::{Files, NamedFile};
|
||||||
use actix_session::{CookieSession, Session};
|
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error, get,
|
error, get,
|
||||||
http::{
|
http::{
|
||||||
@ -13,6 +12,9 @@ use actix_web::{
|
|||||||
};
|
};
|
||||||
use async_stream::stream;
|
use async_stream::stream;
|
||||||
|
|
||||||
|
// NOTE: Not a suitable session key for production.
|
||||||
|
static SESSION_SIGNING_KEY: &[u8] = &[0; 64];
|
||||||
|
|
||||||
/// favicon handler
|
/// favicon handler
|
||||||
#[get("/favicon")]
|
#[get("/favicon")]
|
||||||
async fn favicon() -> Result<impl Responder> {
|
async fn favicon() -> Result<impl Responder> {
|
||||||
@ -76,14 +78,21 @@ async fn with_param(req: HttpRequest, path: web::Path<(String,)>) -> HttpRespons
|
|||||||
async fn main() -> io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
|
|
||||||
|
// random key means that restarting server will invalidate existing session cookies
|
||||||
|
let key = actix_web::cookie::Key::from(SESSION_SIGNING_KEY);
|
||||||
|
|
||||||
log::info!("starting HTTP server at http://localhost:8080");
|
log::info!("starting HTTP server at http://localhost:8080");
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
// enable automatic response compression - usually register this first
|
// enable automatic response compression - usually register this first
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
// cookie session middleware
|
// cookie session middleware
|
||||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
.wrap(
|
||||||
|
SessionMiddleware::builder(CookieSessionStore::default(), key.clone())
|
||||||
|
.cookie_secure(false)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
// enable logger - always register Actix Web Logger middleware last
|
// enable logger - always register Actix Web Logger middleware last
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
// register favicon
|
// register favicon
|
||||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-files = "0.6"
|
actix-files = "0.6"
|
||||||
actix-session = "0.5"
|
actix-session = { version = "0.7", features = ["cookie-session"] }
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
actix-web-lab = "0.16"
|
actix-web-lab = "0.16"
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
use actix_session::CookieSession;
|
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http,
|
http,
|
||||||
middleware::{ErrorHandlers, Logger},
|
middleware::{ErrorHandlers, Logger},
|
||||||
@ -15,13 +15,16 @@ mod db;
|
|||||||
mod model;
|
mod model;
|
||||||
mod session;
|
mod session;
|
||||||
|
|
||||||
static SESSION_SIGNING_KEY: &[u8] = &[0; 32];
|
// NOTE: Not a suitable session key for production.
|
||||||
|
static SESSION_SIGNING_KEY: &[u8] = &[0; 64];
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
|
|
||||||
|
let key = actix_web::cookie::Key::from(SESSION_SIGNING_KEY);
|
||||||
|
|
||||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
let pool = db::init_pool(&database_url)
|
let pool = db::init_pool(&database_url)
|
||||||
.await
|
.await
|
||||||
@ -35,7 +38,9 @@ async fn main() -> io::Result<()> {
|
|||||||
let mut templates = Tera::new("templates/**/*").expect("errors in tera templates");
|
let mut templates = Tera::new("templates/**/*").expect("errors in tera templates");
|
||||||
templates.autoescape_on(vec!["tera"]);
|
templates.autoescape_on(vec!["tera"]);
|
||||||
|
|
||||||
let session_store = CookieSession::signed(SESSION_SIGNING_KEY).secure(false);
|
let session_store = SessionMiddleware::builder(CookieSessionStore::default(), key.clone())
|
||||||
|
.cookie_secure(false)
|
||||||
|
.build();
|
||||||
|
|
||||||
let error_handlers = ErrorHandlers::new()
|
let error_handlers = ErrorHandlers::new()
|
||||||
.handler(
|
.handler(
|
||||||
|
@ -5,11 +5,11 @@ use serde::{Deserialize, Serialize};
|
|||||||
const FLASH_KEY: &str = "flash";
|
const FLASH_KEY: &str = "flash";
|
||||||
|
|
||||||
pub fn set_flash(session: &Session, flash: FlashMessage) -> Result<()> {
|
pub fn set_flash(session: &Session, flash: FlashMessage) -> Result<()> {
|
||||||
session.insert(FLASH_KEY, flash)
|
Ok(session.insert(FLASH_KEY, flash)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_flash(session: &Session) -> Result<Option<FlashMessage>> {
|
pub fn get_flash(session: &Session) -> Result<Option<FlashMessage>> {
|
||||||
session.get::<FlashMessage>(FLASH_KEY)
|
Ok(session.get::<FlashMessage>(FLASH_KEY)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_flash(session: &Session) {
|
pub fn clear_flash(session: &Session) {
|
||||||
|
Loading…
Reference in New Issue
Block a user