mirror of
https://github.com/actix/examples
synced 2024-11-30 17:14:35 +01:00
54 lines
1.4 KiB
Rust
54 lines
1.4 KiB
Rust
|
extern crate actix;
|
||
|
extern crate actix_web;
|
||
|
extern crate cookie;
|
||
|
extern crate time;
|
||
|
extern crate futures;
|
||
|
extern crate env_logger;
|
||
|
|
||
|
use actix_web::{server, middleware, App, HttpRequest, HttpResponse};
|
||
|
|
||
|
mod auth;
|
||
|
use auth::{RequestIdentity, IdentityService, CookieIdentityPolicy};
|
||
|
|
||
|
|
||
|
fn index(mut req: HttpRequest) -> String {
|
||
|
format!("Hello {}", req.identity().unwrap_or("Anonymous"))
|
||
|
}
|
||
|
|
||
|
fn login(mut req: HttpRequest) -> HttpResponse {
|
||
|
req.remember("user1".to_owned());
|
||
|
HttpResponse::Found()
|
||
|
.header("location", "/")
|
||
|
.finish()
|
||
|
}
|
||
|
|
||
|
fn logout(mut req: HttpRequest) -> HttpResponse {
|
||
|
req.forget();
|
||
|
HttpResponse::Found()
|
||
|
.header("location", "/")
|
||
|
.finish()
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||
|
env_logger::init();
|
||
|
let sys = actix::System::new("cookie-auth");
|
||
|
|
||
|
server::new(
|
||
|
|| App::new()
|
||
|
.middleware(middleware::Logger::default())
|
||
|
.middleware(IdentityService::new(
|
||
|
CookieIdentityPolicy::new(&[0; 32])
|
||
|
.name("auth-example")
|
||
|
.secure(false)))
|
||
|
|
||
|
.resource("/login", |r| r.f(login))
|
||
|
.resource("/logout", |r| r.f(logout))
|
||
|
.resource("/", |r| r.f(index)))
|
||
|
.bind("127.0.0.1:8080").unwrap()
|
||
|
.start();
|
||
|
|
||
|
println!("Started http server: 127.0.0.1:8080");
|
||
|
let _ = sys.run();
|
||
|
}
|