Skip to content

Commit

Permalink
Update webserver example to axum
Browse files Browse the repository at this point in the history
  • Loading branch information
conways-glider committed Jan 30, 2024
1 parent 8e464a1 commit 12b8cde
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ sha3 = "0.10"
thiserror = "1.0"

[dev-dependencies]
actix-web = "4"
axum = "0.7"
tokio = { version = "1.0", features = ["full"] }
version-sync = "0.9"
71 changes: 51 additions & 20 deletions examples/webserver.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use std::ffi::OsStr;

use axum::{
extract::Path,
http::{header, StatusCode},
response::IntoResponse,
routing::get,
Router,
};
use identicon_rs::Identicon;

#[get("/{name}.png")]
async fn generate_png(path: web::Path<String>) -> impl Responder {
let identicon_string = path.into_inner();
let identicon = Identicon::new(identicon_string);
let file = identicon.export_png_data().unwrap();
#[tokio::main]
async fn main() {
let app = Router::new().route("/:input", get(root));

HttpResponse::Ok().content_type("image/png").body(file)
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
}

#[get("/{name}.jpg")]
async fn generate_jpeg(path: web::Path<String>) -> impl Responder {
let identicon_string = path.into_inner();
let identicon = Identicon::new(identicon_string);
let file = identicon.export_jpeg_data().unwrap();

HttpResponse::Ok().content_type("image/jpeg").body(file)
// basic handler that responds with a static string
async fn root(Path(input): Path<String>) -> impl IntoResponse {
let file_path = std::path::Path::new(&input);
match file_path.file_stem() {
None => StatusCode::BAD_REQUEST.into_response(),
Some(input) => match input.to_str() {
None => StatusCode::BAD_REQUEST.into_response(),
Some(name) => {
let identicon: Identicon = Identicon::new(name);
generate_image(file_path.extension(), &identicon).into_response()
}
},
}
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(generate_png).service(generate_jpeg))
.bind(("127.0.0.1", 8080))?
.run()
.await
fn generate_image(extention: Option<&OsStr>, identicon: &Identicon) -> impl IntoResponse {
match extention {
Some(os_str) => match os_str.to_str() {
Some("jpeg") | Some("jpg") => (
StatusCode::OK,
[(header::CONTENT_TYPE, "image/jpeg")],
identicon.export_jpeg_data().unwrap(),
)
.into_response(),
Some("png") => (
StatusCode::OK,
[(header::CONTENT_TYPE, "image/png")],
identicon.export_png_data().unwrap(),
)
.into_response(),
_ => StatusCode::BAD_REQUEST.into_response(),
},
None => (
StatusCode::OK,
[(header::CONTENT_TYPE, "image/png")],
identicon.export_png_data().unwrap(),
)
.into_response(),
}
}

0 comments on commit 12b8cde

Please sign in to comment.