mirror of
https://github.com/actix/examples
synced 2025-02-25 10:32:49 +01:00
post cookie-auth and form examples
This commit is contained in:
parent
e2945b9b39
commit
b6929b47b1
@ -2,8 +2,9 @@
|
|||||||
name = "cookie-auth"
|
name = "cookie-auth"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
workspace = "../"
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
env_logger = "0.5"
|
env_logger = "0.6"
|
||||||
|
@ -1,31 +1,26 @@
|
|||||||
extern crate actix;
|
use actix_web::middleware::identity::Identity;
|
||||||
extern crate actix_web;
|
|
||||||
extern crate env_logger;
|
|
||||||
|
|
||||||
use actix_web::middleware::identity::RequestIdentity;
|
|
||||||
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
||||||
use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
|
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
|
||||||
|
|
||||||
fn index(req: &HttpRequest) -> String {
|
fn index(id: Identity) -> String {
|
||||||
format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned()))
|
format!("Hello {}", id.identity().unwrap_or("Anonymous".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn login(req: &HttpRequest) -> HttpResponse {
|
fn login(id: Identity) -> HttpResponse {
|
||||||
req.remember("user1".to_owned());
|
id.remember("user1".to_owned());
|
||||||
HttpResponse::Found().header("location", "/").finish()
|
HttpResponse::Found().header("location", "/").finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn logout(req: &HttpRequest) -> HttpResponse {
|
fn logout(id: Identity) -> HttpResponse {
|
||||||
req.forget();
|
id.forget();
|
||||||
HttpResponse::Found().header("location", "/").finish()
|
HttpResponse::Found().header("location", "/").finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("cookie-auth");
|
|
||||||
|
|
||||||
server::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.middleware(IdentityService::new(
|
.middleware(IdentityService::new(
|
||||||
@ -33,14 +28,10 @@ fn main() {
|
|||||||
.name("auth-example")
|
.name("auth-example")
|
||||||
.secure(false),
|
.secure(false),
|
||||||
))
|
))
|
||||||
.resource("/login", |r| r.f(login))
|
.service(web::resource("/login").route(web::post().to(login)))
|
||||||
.resource("/logout", |r| r.f(logout))
|
.service(web::resource("/logout").to(logout))
|
||||||
.resource("/", |r| r.f(index))
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")
|
.bind("127.0.0.1:8080")?
|
||||||
.unwrap()
|
.run()
|
||||||
.start();
|
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8080");
|
|
||||||
let _ = sys.run();
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
name = "form-example"
|
name = "form-example"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.7"
|
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
actix-web = "0.7"
|
|
||||||
|
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
@ -1,48 +1,32 @@
|
|||||||
extern crate actix;
|
|
||||||
extern crate actix_web;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http, middleware, server, App, Form, HttpRequest, HttpResponse, Result, State,
|
middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
foo: String,
|
foo: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
let sys = actix::System::new("form-example");
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
let _addr = server::new(|| {
|
.state(AppState {
|
||||||
App::with_state(AppState {
|
|
||||||
foo: "bar".to_string(),
|
foo: "bar".to_string(),
|
||||||
})
|
})
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.resource("/", |r| {
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
r.method(http::Method::GET).with(index);
|
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
||||||
|
.service(web::resource("/post2").route(web::post().to(handle_post_2)))
|
||||||
|
.service(web::resource("/post3").route(web::post().to(handle_post_3)))
|
||||||
})
|
})
|
||||||
.resource("/post1", |r| {
|
.bind("127.0.0.1:8080")?
|
||||||
r.method(http::Method::POST).with(handle_post_1)
|
.run()
|
||||||
})
|
|
||||||
.resource("/post2", |r| {
|
|
||||||
r.method(http::Method::POST).with(handle_post_2)
|
|
||||||
})
|
|
||||||
.resource("/post3", |r| {
|
|
||||||
r.method(http::Method::POST).with(handle_post_3)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(_req: HttpRequest<AppState>) -> Result<HttpResponse> {
|
fn index() -> Result<HttpResponse> {
|
||||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html; charset=utf-8")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(include_str!("../static/form.html")))
|
.body(include_str!("../static/form.html")))
|
||||||
}
|
}
|
||||||
@ -53,30 +37,28 @@ pub struct MyParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Simple handle POST request
|
/// Simple handle POST request
|
||||||
fn handle_post_1(params: Form<MyParams>) -> Result<HttpResponse> {
|
fn handle_post_1(params: web::Form<MyParams>) -> Result<HttpResponse> {
|
||||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(format!("Your name is {}", params.name)))
|
.body(format!("Your name is {}", params.name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// State and POST Params
|
/// State and POST Params
|
||||||
fn handle_post_2(
|
fn handle_post_2(
|
||||||
(state, params): (State<AppState>, Form<MyParams>),
|
state: web::State<AppState>,
|
||||||
) -> Result<HttpResponse> {
|
params: web::Form<MyParams>,
|
||||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
) -> HttpResponse {
|
||||||
.content_type("text/plain")
|
HttpResponse::Ok().content_type("text/plain").body(format!(
|
||||||
.body(format!(
|
|
||||||
"Your name is {}, and in AppState I have foo: {}",
|
"Your name is {}, and in AppState I have foo: {}",
|
||||||
params.name, state.foo
|
params.name, state.foo
|
||||||
)))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request and POST Params
|
/// Request and POST Params
|
||||||
fn handle_post_3(
|
fn handle_post_3(req: HttpRequest, params: web::Form<MyParams>) -> impl Responder {
|
||||||
(req, params): (HttpRequest<AppState>, Form<MyParams>),
|
|
||||||
) -> Result<HttpResponse> {
|
|
||||||
println!("Handling POST request: {:?}", req);
|
println!("Handling POST request: {:?}", req);
|
||||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
||||||
|
HttpResponse::Ok()
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(format!("Your name is {}", params.name)))
|
.body(format!("Your name is {}", params.name))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user