#![allow(unused_variables)]
extern crate actix;
extern crate actix_web;
extern crate openssl;

use actix_web::*;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use actix_web::middleware::identity::RequestIdentity;
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};

fn index(req: HttpRequest) -> Result<String> {
    // access request identity
    if let Some(id) = req.identity() {
        Ok(format!("Welcome! {}", id))
    } else {
        Ok("Welcome Anonymous!".to_owned())
    }
}

fn login(mut req: HttpRequest) -> HttpResponse {
    req.remember("NeoUser".to_owned());
    HttpResponse::Found().header("location", "/").finish()
}

fn logout(mut req: HttpRequest) -> HttpResponse {
    req.forget();
    HttpResponse::Found().header("location", "/").finish()
}

fn main() {
    let sys = actix::System::new("http2_api-test");

    let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
    builder.set_private_key_file("key.pem", SslFiletype::PEM).unwrap();
    builder.set_certificate_chain_file("cert.pem").unwrap();

    server::new(|| {
        App::new()
            .middleware(IdentityService::new(
            // <- create identity middleware
            CookieIdentityPolicy::new(&[0; 32])    // <- create cookie session backend
                .name("auth-cookie")
                .secure(false),
            ))
            .route("/", http::Method::GET, index)
            .resource("/login", |r|{r.route().filter(pred::Any(pred::Get()).or(pred::Post())).f(login);})
            .route("/logout",http::Method::GET,logout)
//    }).bind_ssl("127.0.0.1:8443", builder).unwrap().start();
    }).bind("127.0.0.1:8443").unwrap().start();
    println!("Started http server: 127.0.0.1:8443");
    let _ = sys.run();
}
04-28 07:53