mirror of
https://github.com/actix/actix-website
synced 2025-02-02 04:09:06 +01:00
Updates front-page examples: powerful-extractor and easy-form-handling.
This commit is contained in:
parent
cbd27e7668
commit
118286afb1
@ -8,10 +8,10 @@ struct Register {
|
|||||||
country: String,
|
country: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index() -> actix_web::Result<HttpResponse> {
|
fn index() -> HttpResponse {
|
||||||
Ok(HttpResponse::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"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
|
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
|
||||||
|
@ -24,7 +24,5 @@
|
|||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -5,3 +5,4 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0"
|
actix-web = "1.0"
|
||||||
|
serde = "1.0"
|
||||||
|
@ -1,3 +1,44 @@
|
|||||||
fn main() {
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
println!("Hello, world!");
|
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();
|
||||||
}
|
}
|
||||||
|
36
examples/front-page/powerful-extractors/static/form.html
Normal file
36
examples/front-page/powerful-extractors/static/form.html
Normal 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>
|
@ -91,16 +91,18 @@ fn current_temperature(_req: HttpRequest) -> impl Responder {
|
|||||||
your view functions can be synchronous code and still benefit
|
your view functions can be synchronous code and still benefit
|
||||||
from asynchronous IO handling.
|
from asynchronous IO handling.
|
||||||
</p>
|
</p>
|
||||||
{{ highlight `#[derive(Deserialize)]
|
{{ highlight `#[derive(Deserialize, Serialize)]
|
||||||
struct Event {
|
struct Event {
|
||||||
|
id: Option<i32>,
|
||||||
timestamp: f64,
|
timestamp: f64,
|
||||||
kind: String,
|
kind: String,
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
}
|
}
|
||||||
|
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> {
|
||||||
fn capture_event(evt: Json<Event>) -> impl Responder {
|
let new_event = store_event_in_db(evt.timestamp,
|
||||||
let id = store_event_in_db(evt.timestamp, evt.kind, evt.tags);
|
evt.kind.clone(),
|
||||||
format!("got event {}", id)
|
evt.tags.clone());
|
||||||
|
Ok(HttpResponse::Ok().json(new_event))
|
||||||
}` "rust" "" }}
|
}` "rust" "" }}
|
||||||
</div>
|
</div>
|
||||||
<div class="actix-feature" id="forms">
|
<div class="actix-feature" id="forms">
|
||||||
@ -116,8 +118,11 @@ struct Register {
|
|||||||
country: String,
|
country: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register(data: Form<Register>) -> impl Responder {
|
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
|
||||||
format!("Hello {} from {}!", data.username, data.country)
|
Ok(HttpResponse::Ok().body(format!(
|
||||||
|
"Hello {} from {}!",
|
||||||
|
params.username, params.country
|
||||||
|
)))
|
||||||
}` "rust" "" }}
|
}` "rust" "" }}
|
||||||
</div>
|
</div>
|
||||||
<div class="actix-feature" id="routing">
|
<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
|
URLs and invoke individual handlers. For extra flexibility, scopes
|
||||||
can be used.
|
can be used.
|
||||||
</p>
|
</p>
|
||||||
{{ highlight `fn index(req: HttpRequest) -> impl Responder {
|
{{ highlight `fn index(_req: HttpRequest) -> HttpResponse {
|
||||||
"Hello from the index page"
|
HttpResponse::Ok().body("Hello from the index page!")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hello(path: Path<String>) -> impl Responder {
|
fn hello(path: web::Path<String>) -> HttpResponse {
|
||||||
format!("Hello {}!", *path)
|
HttpResponse::Ok().body(format!("Hello {}!", &path))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.resource("/", |r| r.method(Method::GET).with(index))
|
.route("/", web::get().to(index))
|
||||||
.resource("/hello/{name}", |r| r.method(Method::GET).with(hello))
|
.route("/{name}", web::get().to(hello));
|
||||||
.finish();
|
|
||||||
}` "rust" "" }}
|
}` "rust" "" }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user