mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
post cookie-auth and form examples
This commit is contained in:
@ -1,48 +1,32 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use actix_web::{
|
||||
http, middleware, server, App, Form, HttpRequest, HttpResponse, Result, State,
|
||||
middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result,
|
||||
};
|
||||
|
||||
struct AppState {
|
||||
foo: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("form-example");
|
||||
|
||||
let _addr = server::new(|| {
|
||||
App::with_state(AppState {
|
||||
foo: "bar".to_string(),
|
||||
})
|
||||
.middleware(middleware::Logger::default())
|
||||
.resource("/", |r| {
|
||||
r.method(http::Method::GET).with(index);
|
||||
})
|
||||
.resource("/post1", |r| {
|
||||
r.method(http::Method::POST).with(handle_post_1)
|
||||
})
|
||||
.resource("/post2", |r| {
|
||||
r.method(http::Method::POST).with(handle_post_2)
|
||||
})
|
||||
.resource("/post3", |r| {
|
||||
r.method(http::Method::POST).with(handle_post_3)
|
||||
})
|
||||
fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.state(AppState {
|
||||
foo: "bar".to_string(),
|
||||
})
|
||||
.middleware(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(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)))
|
||||
})
|
||||
.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();
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
}
|
||||
|
||||
fn index(_req: HttpRequest<AppState>) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
fn index() -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(include_str!("../static/form.html")))
|
||||
}
|
||||
@ -53,30 +37,28 @@ pub struct MyParams {
|
||||
}
|
||||
|
||||
/// Simple handle POST request
|
||||
fn handle_post_1(params: Form<MyParams>) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
fn handle_post_1(params: web::Form<MyParams>) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/plain")
|
||||
.body(format!("Your name is {}", params.name)))
|
||||
}
|
||||
|
||||
/// State and POST Params
|
||||
fn handle_post_2(
|
||||
(state, params): (State<AppState>, Form<MyParams>),
|
||||
) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
.content_type("text/plain")
|
||||
.body(format!(
|
||||
"Your name is {}, and in AppState I have foo: {}",
|
||||
params.name, state.foo
|
||||
)))
|
||||
state: web::State<AppState>,
|
||||
params: web::Form<MyParams>,
|
||||
) -> HttpResponse {
|
||||
HttpResponse::Ok().content_type("text/plain").body(format!(
|
||||
"Your name is {}, and in AppState I have foo: {}",
|
||||
params.name, state.foo
|
||||
))
|
||||
}
|
||||
|
||||
/// Request and POST Params
|
||||
fn handle_post_3(
|
||||
(req, params): (HttpRequest<AppState>, Form<MyParams>),
|
||||
) -> Result<HttpResponse> {
|
||||
fn handle_post_3(req: HttpRequest, params: web::Form<MyParams>) -> impl Responder {
|
||||
println!("Handling POST request: {:?}", req);
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/plain")
|
||||
.body(format!("Your name is {}", params.name)))
|
||||
.body(format!("Your name is {}", params.name))
|
||||
}
|
||||
|
Reference in New Issue
Block a user