Skip to content

Instantly share code, notes, and snippets.

View mrGlasses's full-sized avatar

Nickolas mrGlasses

View GitHub Profile
@mrGlasses
mrGlasses / simple_handler.rs
Last active July 15, 2025 06:00
Excelsior's Series 6 - 01
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
..........
pub async fn call_external_service() -> Response {
info!("call_external_service called");
let base_url = std::env::var("EXTERNAL_SERVICE_URL") //catching the url to be used
@mrGlasses
mrGlasses / simple_handler_test.rs
Created July 14, 2025 08:21
Excelsior's Series 5 - 09
#[tokio::test]
async fn test_post_body_data() {
let body_data = Message { //creating the body
code: 777,
message_text: "Received body data successfully!".to_string(),
};
let response = post_body_data(Json(body_data)).await; //transofrming to JSON and sending
let body = response.into_body().collect().await.unwrap().to_bytes();
@mrGlasses
mrGlasses / simple_handler_test.rs
Created July 14, 2025 08:10
Excelsior's Series 5 - 08
#[tokio::test]
async fn test_get_question() {
let query = Query(FilterParams { //creating the query using FilterParams
name: Option::from("Jack".to_string()),
age: Option::from(25),
active: Option::from(true),
});
let response = get_question(query).await;
let body = response.into_body().collect().await.unwrap().to_bytes();
@mrGlasses
mrGlasses / simple_handler_test.rs
Created July 14, 2025 08:01
Excelsior's Series 5 - 07
#[tokio::test]
async fn test_get_params() {
let param_in = Path(Params { //creating the parameters using Params type as the guide
param_1: 1,
param_2: "test2".to_string(),
});
let response = get_params(param_in).await;
let body = response.into_body().collect().await.unwrap().to_bytes(); //getting the body in a hard way
assert_eq!(body, "Parameter 1: 1, Parameter 2: test2");
@mrGlasses
mrGlasses / simple_handler_test.rs
Created July 14, 2025 07:57
Excelsior's Series 5 - 06
............
#[tokio::test]
async fn test_protected_route_no_auth() { //failure route
let headers = HeaderMap::new(); //create empty headers
let response = protected_route(headers).await;
assert_eq!(response.into_response().status(), StatusCode::UNAUTHORIZED); //verify status code
}
@mrGlasses
mrGlasses / simple_handler.rs
Created July 13, 2025 09:19
Excelsior's Series 5 - 05
use axum::Json;
........
pub async fn post_body_data(Json(payload): Json<Message>) -> Response {
info!("Received payload: {:?}", payload);
let response = format!(
"Received message with code: {}, text: {}",
payload.code, payload.message_text
);
@mrGlasses
mrGlasses / simple_handler.rs
Created July 13, 2025 09:07
Excelsior's Series 5 - 04
use axum::extract::Query;
.........
pub async fn get_question(Query(params): Query<FilterParams>) -> Response {
let response = format!(
"Filters: name={}, age={}, active={}",
params.name.unwrap_or_default(), //as all parameters are Optional<>
params.age.unwrap_or_default(), //the parameters need a default method
params.active.unwrap_or_default()
@mrGlasses
mrGlasses / simple_handler.rs
Created July 13, 2025 08:38
Excelsior's Series 5 - 03
use axum::{
extract::Path,
http::StatusCode,
};
........
pub async fn get_params(Path(Params { param_1, param_2 }): Path<Params>) -> Response {
(
StatusCode::OK,
@mrGlasses
mrGlasses / simple_handler.rs
Created July 13, 2025 08:18
Excelsior's Series 5 - 02
use axum::http::{HeaderMap, StatusCode};
.........
pub async fn protected_route(headers: HeaderMap) -> Response {
// Verifies if the specific header exists and have the correct value
match headers.get("X-Custom-Header") { //get the specific header
Some(header_value) if header_value == "secret-value" => { //test the header value
(StatusCode::OK, "Access Granted!").into_response() //OK -> GO
}
@mrGlasses
mrGlasses / general.rs
Created July 9, 2025 08:49
Excelsior's Series 5 - 01
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Serialize, Deserialize, FromRow, Debug, Default)]
pub struct Message {
pub code: i32,
pub message_text: String,
}
#[derive(Deserialize)]