1
0
mirror of https://github.com/actix/actix-website synced 2025-02-21 11:54:48 +01:00

simplifies the front-page examples.

This commit is contained in:
Cameron Dershem 2019-06-19 14:24:31 -04:00
parent 2011f100a5
commit da969fed1f
4 changed files with 15 additions and 16 deletions

View File

@ -14,8 +14,8 @@ fn index() -> HttpResponse {
.body(include_str!("../static/form.html")) .body(include_str!("../static/form.html"))
} }
fn register(params: web::Form<Register>) -> impl Responder { fn register(form: web::Form<Register>) -> impl Responder {
format!("Hello {} from {}!", params.username, params.country) format!("Hello {} from {}!", form.username, form.country)
} }
fn main() { fn main() {

View File

@ -9,8 +9,8 @@ fn greet(req: HttpRequest) -> impl Responder {
fn main() { fn main() {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.service(web::resource("/").to(greet)) .route("/", web::get().to(greet))
.service(web::resource("/{name}").to(greet)) .route("/{name}", web::get().to(greet))
}) })
.bind("127.0.0.1:8088") .bind("127.0.0.1:8088")
.unwrap() .unwrap()

View File

@ -9,19 +9,19 @@ struct Event {
tags: Vec<String>, tags: Vec<String>,
} }
fn store_event_in_db(timestamp: f64, kind: String, tags: Vec<String>) -> Event { fn store_in_db(timestamp: f64, kind: &String, tags: &Vec<String>) -> Event {
// store item in db and get new_event // store item in db and get new_event
// use id to lookup item // use id to lookup item
Event { Event {
id: Some(1), id: Some(1),
timestamp: timestamp, timestamp: timestamp,
kind: kind, kind: kind.to_string(),
tags: tags, tags: tags.to_vec(),
} }
} }
fn capture_event(evt: web::Json<Event>) -> impl Responder { fn capture_event(evt: web::Json<Event>) -> impl Responder {
let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone()); let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
format!("got event {}", new_event.id.unwrap()) format!("got event {}", new_event.id.unwrap())
} }

View File

@ -39,7 +39,7 @@
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<div class="actix-content"> <div class="actix-content">
{{ highlight `use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; {{ highlight `use actix_web::{web, App, HttpRequest, HttpServer};
fn greet(req: HttpRequest) -> impl Responder { fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World"); let name = req.match_info().get("name").unwrap_or("World");
@ -49,8 +49,8 @@ fn greet(req: HttpRequest) -> impl Responder {
fn main() { fn main() {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.service(web::resource("/").to(greet)) .route("/", web::get().to(greet))
.service(web::resource("/{name}").to(greet)) .route("/{name}", web::get().to(greet))
}) })
.bind("127.0.0.1:8000") .bind("127.0.0.1:8000")
.expect("Can not bind to port 8000") .expect("Can not bind to port 8000")
@ -98,10 +98,9 @@ struct Event {
kind: String, kind: String,
tags: Vec<String>, tags: Vec<String>,
} }
fn capture_event(evt: web::Json<Event>) -> impl Responder { fn capture_event(evt: web::Json<Event>) -> impl Responder {
let new_event = store_event_in_db(evt.timestamp, let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
evt.kind.clone(),
evt.tags.clone());
format!("got event {}", new_event.id.unwrap()) format!("got event {}", new_event.id.unwrap())
}` "rust" "" }} }` "rust" "" }}
</div> </div>
@ -118,8 +117,8 @@ struct Register {
country: String, country: String,
} }
fn register(params: web::Form<Register>) -> impl Responder { fn register(form: web::Form<Register>) -> impl Responder {
format!("Hello {} from {}!", params.username, params.country) format!("Hello {} from {}!", form.username, form.country)
}` "rust" "" }} }` "rust" "" }}
</div> </div>
<div class="actix-feature" id="routing"> <div class="actix-feature" id="routing">