Closed jkilpatr closed 5 years ago
Great! Let me know when you are finished
Hey, @jkilpatr , how are we going? It is safe to merge it?
@svartalf hey sorry for the delay, prod caught on fire and I got distracted with hot patches.
So I modified the .7 branch of https://github.com/actix/examples/tree/0.7/cookie-session like so
//! Example of cookie based session
//! Session data is stored in cookie, it is limited to 4kb
//!
//! [Redis session example](https://github.com/actix/examples/tree/master/redis-session)
//!
//! [User guide](https://actix.rs/book/actix-web/sec-9-middlewares.html#user-sessions)
extern crate actix;
extern crate actix_web;
extern crate actix_web_httpauth;
extern crate env_logger;
extern crate futures;
use actix_web::middleware::session::{self, RequestSession};
use actix_web::middleware::{Middleware, Started};
use actix_web::FromRequest;
use actix_web::{middleware, server, App, HttpRequest, Result};
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
use actix_web_httpauth::extractors::AuthenticationError;
use std::env;
struct Auth;
impl<S> Middleware<S> for Auth {
fn start(&self, req: &HttpRequest<S>) -> Result<Started> {
let mut config = Config::default();
config.realm("WallyWorld");
let auth = BasicAuth::from_request(&req, &config)?;
if auth.username() == "Aladdin" && auth.password() == Some("open sesame") {
Ok(Started::Done)
} else {
Err(AuthenticationError::from(config).into())
}
}
}
/// simple index handler with session
fn index(req: &HttpRequest) -> Result<&'static str> {
println!("{:?}", req);
// RequestSession trait is used for session access
let mut counter = 1;
if let Some(count) = req.session().get::<i32>("counter")? {
println!("SESSION value: {}", count);
counter = count + 1;
req.session().set("counter", counter)?;
} else {
req.session().set("counter", counter)?;
}
Ok("welcome!")
}
fn main() {
env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let sys = actix::System::new("session-example");
server::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
// cookie session middleware
.middleware(session::SessionStorage::new(
session::CookieSessionBackend::signed(&[0; 32]).secure(false),
))
.middleware(Auth)
.resource("/", |r| r.f(index))
})
.bind("127.0.0.1:8080")
.expect("Can not bind to 127.0.0.1:8080")
.start();
println!("Starting http server: 127.0.0.1:8080");
let _ = sys.run();
}
And it works just fine with my change
@jkilpatr great to hear that! I suppose it would be hard to break anything with your patch, therefore I'll try to merge and publish it tomorrow (kinda busy today too :) )
Quick follow-up: 0.2.0 version
By default actix-web pulls in cookie which pulls in Ring which makes it unbuildable for non x86 or ARM architectures.
Since this package doesn't seem to have any need of any features beyond the Actix middleware interface I figured I could toss this upstream.
I'd still like to test it with code using the various default features to see if I'm accidentally breaking anything.