lasaris winter
school
Pokračování lekcí Rustu
dnes vás provází Lukáš Grolig
Na co se dnes podíváme?
- HTTP protokol a alternativy
- REST API
- Setup Actix
- Routing
- Handlery
- Extractory
- Middleware
HTTP
Browser
Webserver
HTTP
HTTP
GET / HTTP/1.1
Host: www.example.com
Method
Location
Protocol version
Host name
HTTP
příklady requestů
GET styles.css HTTP/1.1
GET /api/tasks HTTP/1.1
GET /page.html HTTP/1.1
HTTP Response
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 138
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
ETag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
<p>Hello World, this is a very simple HTML document.</p>
</body>
</html>
HTTP Verbs
GET
HEAD
OPTION
PUT
DELETE
POST
}
Idepotent verbs
non-idepotent
{
safe
Stavové kódy
200
204
301
400
401
403
404
500
OK
No Content
Moved Permanently
Bad Request
Unauthorized
Forbidden
Not Found
Internal Server Error
vracím data
něco se stalo, ale bez obsahu
vždy přesměrovat na novou URL
špatné typy, rozsahy, apod
je potřeba se přihlásit
přihlášený nemá přístup
stránka neexistuje
nějaká chyba u nás
Alternativy
HTTP 2
- binární protokol
- šifrování pomocí TLS
- lze přenést více částí přes jedno spojení
Web Sockets
- obousměrná binální komunikace
- vhodné pro realtime systémy
- nemusí být šifrované
REST API
- dnešní způsob jak budujeme rozhraní k backendové části aplikace
- primárně využívá JSON
- rozhraní vychází z doméný nebo prezentačních požadavků
JSON
{
"teams": [
{
"name": "Seven Deadly Sins",
"heroes": [
{
"name": "Meliodas",
"powerLevel": 60000,
"race": "Demon"
},
{
"name": "Elizabeth",
"powerLevel": 60000,
"race": "Goddess"
},
{
"name": "Bane",
"powerLevel": 30000,
"race": "Human"
}
]
}
]
}
Endpointy
Vazby
List nebo
vytvoření
/devices /configurations
Jeden záznam, update, smazání
/devices/{id} /configurations/{id}
/devices/{id}/configurations /devices/{id}/configurations/{id}
Podstatná jména,
ne slovesa!
Takto ne
GET /getAllEmployees
GET /getEmployeeWithId
POST /createEmployee
Používané metody
GET
PUT
DELETE
POST
}
Idepotent verbs
non-idepotent
safe ->
Actix
Cargo.toml
[dependencies]
actix-web = "3"
Vytvoření enpointů
use actix_web::{get, post, web, App,
HttpResponse, HttpServer, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
}
A integrace
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Form input
use actix_web::{post, web, HttpResponse};
use serde::Deserialize;
#[derive(Deserialize)]
struct FormData {
username: String,
}
#[post("/")]
async fn index(form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().body(
format!("username: {}", form.username)
)
}
JSON v Requestu
use actix_web::{web, App, HttpServer, Result};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract `Info` using serde
async fn index(info: web::Json<Info>) -> Result<String> {
Ok(format!("Welcome {}!", info.username))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8080")?
.run()
.await
}
JSON Response
use actix_web::{get, web, HttpResponse, Result};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyObj {
name: String,
}
#[get("/a/{name}")]
async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json(MyObj {
name: obj.name.to_string(),
}))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// as usual
}
Middleware si řekneme podle dokumentace
To je pro dnešek vše.
Dotazy?
Děkuji za pozornost a budu se těšit na příští setkání
RSS: Actix
By Lukáš Grolig
RSS: Actix
- 296