aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md51
1 files changed, 27 insertions, 24 deletions
diff --git a/README.md b/README.md
index 7d88c25..f19d3b5 100644
--- a/README.md
+++ b/README.md
@@ -35,14 +35,14 @@ header matches your domain name (especially if you have unauthenticated POST end
## Hyper Example
```rust
-use std::convert::Infallible;
-use hyper::service::{service_fn, make_service_fn};
-use hyper::{Method, Server, StatusCode, Body};
use hyper::http::request::Parts;
use hyper::http::response::Builder;
+use hyper::service::{make_service_fn, service_fn};
+use hyper::{Body, Method, Server, StatusCode};
use serde::Deserialize;
+use sputnik::hyper_body::{FormError, SputnikBody};
use sputnik::{html_escape, mime, request::SputnikParts, response::SputnikBuilder};
-use sputnik::hyper_body::{SputnikBody, FormError};
+use std::convert::Infallible;
type Response = hyper::Response<Body>;
@@ -51,7 +51,7 @@ enum Error {
#[error("page not found")]
NotFound(String),
#[error("{0}")]
- FormError(#[from] FormError)
+ FormError(#[from] FormError),
}
fn render_error(err: Error) -> (StatusCode, String) {
@@ -65,33 +65,37 @@ async fn route(req: &mut Parts, body: Body) -> Result<Response, Error> {
match (&req.method, req.uri.path()) {
(&Method::GET, "/form") => Ok(get_form(req)),
(&Method::POST, "/form") => post_form(req, body).await,
- _ => return Err(Error::NotFound("page not found".to_owned()))
+ _ => return Err(Error::NotFound("page not found".to_owned())),
}
}
fn get_form(_req: &mut Parts) -> Response {
Builder::new()
- .content_type(mime::TEXT_HTML)
- .body(
- "<form method=post><input name=text> <button>Submit</button></form>".into()
- ).unwrap()
+ .content_type(mime::TEXT_HTML)
+ .body("<form method=post><input name=text> <button>Submit</button></form>".into())
+ .unwrap()
}
#[derive(Deserialize)]
-struct FormData {text: String}
+struct FormData {
+ text: String,
+}
async fn post_form(_req: &mut Parts, body: Body) -> Result<Response, Error> {
let msg: FormData = body.into_form().await?;
- Ok(Builder::new().content_type(mime::TEXT_HTML).body(
- format!("hello <em>{}</em>", html_escape(msg.text)).into()
- ).unwrap())
+ Ok(Builder::new()
+ .content_type(mime::TEXT_HTML)
+ .body(format!("hello <em>{}</em>", html_escape(msg.text)).into())
+ .unwrap())
}
-async fn service(req: hyper::Request<hyper::Body>) -> Result<hyper::Response<hyper::Body>, Infallible> {
+async fn service(
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, Infallible> {
let (mut parts, body) = req.into_parts();
match route(&mut parts, body).await {
Ok(mut res) => {
- for (k,v) in parts.response_headers().iter() {
+ for (k, v) in parts.response_headers().iter() {
res.headers_mut().append(k, v.clone());
}
Ok(res)
@@ -99,19 +103,18 @@ async fn service(req: hyper::Request<hyper::Body>) -> Result<hyper::Response<hyp
Err(err) => {
let (code, message) = render_error(err);
// you can easily wrap or log errors here
- Ok(hyper::Response::builder().status(code).body(message.into()).unwrap())
+ Ok(hyper::Response::builder()
+ .status(code)
+ .body(message.into())
+ .unwrap())
}
}
}
#[tokio::main]
async fn main() {
- let service = make_service_fn(move |_| {
- async move {
- Ok::<_, hyper::Error>(service_fn(move |req| {
- service(req)
- }))
- }
+ let service = make_service_fn(move |_| async move {
+ Ok::<_, hyper::Error>(service_fn(move |req| service(req)))
});
let addr = ([127, 0, 0, 1], 8000).into();
@@ -155,4 +158,4 @@ let userid = req.cookies().find(|(name, _value)| *name == "userid")
Tip: If you want to store multiple claims in the cookie, you can
(de)serialize a struct with [serde_json](https://docs.serde.rs/serde_json/).
This approach can pose a lightweight alternative to JWT, if you don't care
-about the standardization aspect. \ No newline at end of file
+about the standardization aspect.