mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
create form example
This commit is contained in:
10
form/Cargo.toml
Normal file
10
form/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "form-example"
|
||||
version = "0.1.0"
|
||||
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
actix = "0.5"
|
||||
actix-web = "^0.6"
|
||||
serde = "1.0.69"
|
||||
serde_derive = "1.0.69"
|
80
form/src/main.rs
Normal file
80
form/src/main.rs
Normal file
@ -0,0 +1,80 @@
|
||||
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,
|
||||
};
|
||||
|
||||
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)
|
||||
})
|
||||
}).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> {
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(include_str!("../static/form.html")))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct MyParams {
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// Simple handle POST request
|
||||
fn handle_post_1(params: Form<MyParams>) -> Result<HttpResponse> {
|
||||
Ok(HttpResponse::build(http::StatusCode::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
|
||||
)))
|
||||
}
|
||||
|
||||
/// Request and POST Params
|
||||
fn handle_post_3(
|
||||
(req, params): (HttpRequest<AppState>, Form<MyParams>),
|
||||
) -> Result<HttpResponse> {
|
||||
println!("Handling POST request: {:?}", req);
|
||||
Ok(HttpResponse::build(http::StatusCode::OK)
|
||||
.content_type("text/plain")
|
||||
.body(format!("Your name is {}", params.name)))
|
||||
}
|
39
form/static/form.html
Normal file
39
form/static/form.html
Normal file
@ -0,0 +1,39 @@
|
||||
<!doctype htmtl>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=utf-8>
|
||||
<title>Forms</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Will hit handle_post_1</h3>
|
||||
<form action=/post1 method=POST>
|
||||
<label>
|
||||
Name:
|
||||
<input name="name">
|
||||
</label>
|
||||
<button type=submit>Submit form</button>
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
|
||||
<h3>Will hit handle_post_2</h3>
|
||||
<form action=/post2 method=POST>
|
||||
<label>
|
||||
Name:
|
||||
<input name="name">
|
||||
</label>
|
||||
<button type=submit>Submit form</button>
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
|
||||
<h3>Will hit handle_post_3</h3>
|
||||
<form action=/post3 method=POST>
|
||||
<label>
|
||||
Name:
|
||||
<input name="name">
|
||||
</label>
|
||||
<button type=submit>Submit form</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user