mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
migrate to docusaurus (v2) (#266)
Co-authored-by: ibraheemdev <ibrah1440@gmail.com>
This commit is contained in:
@ -57,7 +57,7 @@ async fn index(pool: web::Data<DbPool>, name: web::Path<(String)>) -> impl Respo
|
||||
eprintln!("{}", e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
})?;
|
||||
|
||||
|
||||
Ok(HttpResponse::Ok().json(user))
|
||||
}
|
||||
// </index>
|
||||
|
@ -1,6 +1,7 @@
|
||||
// <easy-form-handling>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
// <easy-form-handling>
|
||||
use actix_web::web::{Either, Json, Form};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Register {
|
||||
@ -8,25 +9,33 @@ struct Register {
|
||||
country: String,
|
||||
}
|
||||
|
||||
// register form is JSON
|
||||
async fn json_register(form: web::Json<Register>) -> impl Responder {
|
||||
format!("Hello {} from {}!", form.username, form.country)
|
||||
}
|
||||
|
||||
// register form can be either JSON or URL-encoded
|
||||
async fn register(form: Either<Json<Register>, Form<Register>>) -> impl Responder {
|
||||
let Register { username, country } = form.into_inner();
|
||||
format!("Hello {username} from {country}!")
|
||||
}
|
||||
// </easy-form-handling>
|
||||
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(include_str!("../static/form.html"))
|
||||
}
|
||||
|
||||
async fn register(form: web::Form<Register>) -> impl Responder {
|
||||
format!("Hello {} from {}!", form.username, form.country)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/register", web::post().to(register))
|
||||
.route("/json_register", web::post().to(json_register))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </easy-form-handling>
|
||||
|
@ -14,6 +14,7 @@ async fn hello_world() -> impl Responder {
|
||||
async fn current_temperature() -> impl Responder {
|
||||
web::Json(Measurement { temperature: 42.3 })
|
||||
}
|
||||
// </flexible-responders>
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@ -26,4 +27,3 @@ async fn main() -> std::io::Result<()> {
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </flexible-responders>
|
||||
|
@ -1,6 +1,7 @@
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// <powerful-extractors>
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct Event {
|
||||
id: Option<i32>,
|
||||
@ -9,6 +10,12 @@ struct Event {
|
||||
tags: Vec<String>,
|
||||
}
|
||||
|
||||
async fn capture_event(evt: web::Json<Event>) -> impl Responder {
|
||||
let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
|
||||
format!("got event {}", new_event.id.unwrap())
|
||||
}
|
||||
// </powerful-extractors>
|
||||
|
||||
fn store_in_db(timestamp: f64, kind: &str, tags: &[String]) -> Event {
|
||||
// store item in db and get new_event
|
||||
// use id to lookup item
|
||||
@ -20,10 +27,6 @@ fn store_in_db(timestamp: f64, kind: &str, tags: &[String]) -> Event {
|
||||
}
|
||||
}
|
||||
|
||||
async fn capture_event(evt: web::Json<Event>) -> impl Responder {
|
||||
let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
|
||||
format!("got event {}", new_event.id.unwrap())
|
||||
}
|
||||
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
|
@ -1,6 +1,7 @@
|
||||
// <request-routing>
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use actix_web::{get, web, App, HttpRequest, HttpServer, Responder};
|
||||
|
||||
#[get("/")]
|
||||
async fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Hello from the index page."
|
||||
}
|
||||
@ -13,8 +14,8 @@ async fn hello(path: web::Path<String>) -> impl Responder {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(web::resource("/").to(index))
|
||||
.service(web::resource("/{name}").to(hello))
|
||||
.service(index)
|
||||
.route("/{name}", web::get().to(hello))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
|
1
examples/server/rustfmt.toml
Normal file
1
examples/server/rustfmt.toml
Normal file
@ -0,0 +1 @@
|
||||
max_width = 80
|
14
examples/server/src/keep_alive_tp.rs
Normal file
14
examples/server/src/keep_alive_tp.rs
Normal file
@ -0,0 +1,14 @@
|
||||
// <example>
|
||||
use actix_web::{http, HttpRequest, HttpResponse};
|
||||
|
||||
async fn index(req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.connection_type(http::ConnectionType::Close) // <- Close connection
|
||||
.force_close() // <- Alternative method
|
||||
.finish()
|
||||
}
|
||||
// </example>
|
||||
|
||||
// ConnectionType::Close
|
||||
// ConnectionType::KeepAlive
|
||||
// ConnectionType::Upgrade
|
18
examples/url-dispatch/src/pbuf.rs
Normal file
18
examples/url-dispatch/src/pbuf.rs
Normal file
@ -0,0 +1,18 @@
|
||||
// <pbuf>
|
||||
use actix_web::{get, App, HttpRequest, HttpServer, Result};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[get("/a/{tail:.*}")]
|
||||
async fn index(req: HttpRequest) -> Result<String> {
|
||||
let path: PathBuf = req.match_info().query("tail").parse().unwrap();
|
||||
Ok(format!("Path {:?}", path))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </pbuf>
|
Reference in New Issue
Block a user