Skip to content

Commit

Permalink
refactor cmm to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
aleeusgr committed Jan 7, 2024
1 parent d61aee2 commit 1763cf6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
26 changes: 0 additions & 26 deletions src/handlers/my.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
use axum::{
Json,
response::{Html, IntoResponse},
http::{StatusCode, Uri, header::{self, HeaderMap, HeaderName}},
};
use std::path::PathBuf;
use std::io::{BufWriter, Cursor};
use image::ImageFormat;

// this is a handler, a function that is used in a Router
// A handler is an async function that accepts zero or more “extractors” as arguments and returns something that can be converted into a response.
pub async fn say_hello() -> String {
// integrate new functionality here:
return "Hello!!!".to_string();
}

pub async fn html() -> Html<&'static str> {
Html("<p>Hello, World!</p>")
}
// stream.
pub async fn get_image() -> impl axum::response::IntoResponse {
let img_path = PathBuf::from("assets/").join("a_logo.png");
let image = image::io::Reader::open(&img_path).unwrap().decode().unwrap();
let mut buffer = BufWriter::new(Cursor::new(Vec::new()));
image.write_to(&mut buffer, ImageFormat::Png).unwrap();
let bytes: Vec<u8> = buffer.into_inner().unwrap().into_inner();
(
axum::response::AppendHeaders([(header::CONTENT_TYPE, "image/png")]),
bytes
// image.into_bytes()
)
}
// roadmap - show webcam on laptop screen.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ pub mod settings;
pub mod tracer;
pub mod tracing_layers;

use axum::{
Json,
response::{Html, IntoResponse},
http::{StatusCode, Uri, header::{self, HeaderMap, HeaderName}},
};
use std::path::PathBuf;
use std::io::{BufWriter, Cursor};
use image::ImageFormat;

/// Test utilities.
#[cfg(any(test, feature = "test_utils"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
Expand All @@ -24,3 +33,21 @@ pub mod test_utils;
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
// stream.
pub async fn get_image() -> impl axum::response::IntoResponse {
let img_path = PathBuf::from("assets/").join("a_logo.png");
let image = image::io::Reader::open(&img_path).unwrap().decode().unwrap();
let mut buffer = BufWriter::new(Cursor::new(Vec::new()));
image.write_to(&mut buffer, ImageFormat::Png).unwrap();
let bytes: Vec<u8> = buffer.into_inner().unwrap().into_inner();
(
axum::response::AppendHeaders([(header::CONTENT_TYPE, "image/png")]),
bytes
// image.into_bytes()
)
}

pub async fn html() -> Html<&'static str> {
Html("<p>Hello, World!</p>")
}
// roadmap - show webcam on laptop screen.
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ use efected_coto_emmory::{
},
};

use efected_coto_emmory::get_image;
use handlers::my;
pub mod handlers; // notice this line
pub mod handlers;

/// Request identifier field.
const REQUEST_ID: &str = "request_id";
Expand Down Expand Up @@ -96,7 +97,6 @@ async fn main() -> Result<()> {
// This returns a `TraceLayer` configured to use
// OpenTelemetry’s conventional span field names.
.layer(opentelemetry_tracing_layer())
// TODO: search ulid
// Set and propagate "request_id" (as a ulid) per request.
.layer(
ServiceBuilder::new()
Expand All @@ -113,7 +113,8 @@ async fn main() -> Result<()> {
.layer(CatchPanicLayer::custom(runtime::catch_panic))
// Mark headers as sensitive on both requests and responses.
.layer(SetSensitiveHeadersLayer::new([header::AUTHORIZATION]))
.route("/say-hello", get(handlers::my::get_image))
.route("/say-hello", get(handlers::my::say_hello))
.route("/test-lib", get(get_image))
.merge(SwaggerUi::new("/swagger-ui").url("/api-doc/openapi.json", ApiDoc::openapi()));

serve("Application", router, settings.server().port).await
Expand Down

0 comments on commit 1763cf6

Please sign in to comment.