1
0
mirror of https://github.com/actix/actix-website synced 2025-01-22 16:15:56 +01:00

Updates front-page examples: powerful-extractor and easy-form-handling.

This commit is contained in:
Cameron Dershem 2019-06-12 19:23:51 -04:00
parent cbd27e7668
commit 118286afb1
6 changed files with 101 additions and 21 deletions

View File

@ -8,10 +8,10 @@ struct Register {
country: String,
}
fn index() -> actix_web::Result<HttpResponse> {
Ok(HttpResponse::Ok()
fn index() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(include_str!("../static/form.html")))
.body(include_str!("../static/form.html"))
}
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {

View File

@ -24,7 +24,5 @@
</form>
<hr>
</body>
</html>

View File

@ -5,3 +5,4 @@ edition = "2018"
[dependencies]
actix-web = "1.0"
serde = "1.0"

View File

@ -1,3 +1,44 @@
fn main() {
println!("Hello, world!");
use actix_web::{web, App, HttpResponse, HttpServer};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct Event {
id: Option<i32>,
timestamp: f64,
kind: String,
tags: Vec<String>,
}
fn store_event_in_db(timestamp: f64, kind: String, tags: Vec<String>) -> Event {
// store item in db and get new_event
// use id to lookup item
Event {
id: Some(1),
timestamp: timestamp,
kind: kind,
tags: tags,
}
}
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> {
let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone());
Ok(HttpResponse::Ok().json(new_event))
}
fn index() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(include_str!("../static/form.html"))
}
fn main() {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/event", web::post().to(capture_event))
})
.bind("127.0.0.1:8000")
.expect("Can not bind to port 8000")
.run()
.unwrap();
}

View File

@ -0,0 +1,36 @@
<!doctype htmtl>
<html>
<head>
<meta charset=utf-8>
<title>Forms</title>
</head>
<body>
<h3>Submit Json</h3>
<button onclick="submitJson()">Submit</button>
<script>
function submitJson() {
fetch('http://localhost:8000/event', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
timestamp: 12345,
kind: "this is a kind",
tags: ['tag1', 'tag2', 'tag3'],
})
})
.then(function (res) {
return res.text();
})
.then(function (data) {
console.log(data);
});
}
</script>
</body>
</html>

View File

@ -91,16 +91,18 @@ fn current_temperature(_req: HttpRequest) -> impl Responder {
your view functions can be synchronous code and still benefit
from asynchronous IO handling.
</p>
{{ highlight `#[derive(Deserialize)]
{{ highlight `#[derive(Deserialize, Serialize)]
struct Event {
id: Option<i32>,
timestamp: f64,
kind: String,
tags: Vec<String>,
}
fn capture_event(evt: Json<Event>) -> impl Responder {
let id = store_event_in_db(evt.timestamp, evt.kind, evt.tags);
format!("got event {}", id)
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> {
let new_event = store_event_in_db(evt.timestamp,
evt.kind.clone(),
evt.tags.clone());
Ok(HttpResponse::Ok().json(new_event))
}` "rust" "" }}
</div>
<div class="actix-feature" id="forms">
@ -116,8 +118,11 @@ struct Register {
country: String,
}
fn register(data: Form<Register>) -> impl Responder {
format!("Hello {} from {}!", data.username, data.country)
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
Ok(HttpResponse::Ok().body(format!(
"Hello {} from {}!",
params.username, params.country
)))
}` "rust" "" }}
</div>
<div class="actix-feature" id="routing">
@ -127,19 +132,18 @@ fn register(data: Form<Register>) -> impl Responder {
URLs and invoke individual handlers. For extra flexibility, scopes
can be used.
</p>
{{ highlight `fn index(req: HttpRequest) -> impl Responder {
"Hello from the index page"
{{ highlight `fn index(_req: HttpRequest) -> HttpResponse {
HttpResponse::Ok().body("Hello from the index page!")
}
fn hello(path: Path<String>) -> impl Responder {
format!("Hello {}!", *path)
fn hello(path: web::Path<String>) -> HttpResponse {
HttpResponse::Ok().body(format!("Hello {}!", &path))
}
fn main() {
App::new()
.resource("/", |r| r.method(Method::GET).with(index))
.resource("/hello/{name}", |r| r.method(Method::GET).with(hello))
.finish();
.route("/", web::get().to(index))
.route("/{name}", web::get().to(hello));
}` "rust" "" }}
</div>
</div>