Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ axum = { version = "0.8.1", features = []}
serde = { version = "1.0.219", features = ["derive"]}
serde_json = "1.0.140"
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
reqwest = { version = "0.11", features = ["json"] }
14 changes: 12 additions & 2 deletions src/backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use axum::{
routing::{get, post},
Router,
response::Json
};
use serde_json::Value;
mod routes;

use routes::{
handle_create_name::create_name, handle_formatted_name::formatted_name, handle_name::say_name,
handle_create_name::create_name, handle_formatted_name::formatted_name, handle_name::say_name, handle_fetch_weather::handle_request
};
const BASE_URL: &str = "0.0.0.0:5000"; // base url for server

Expand All @@ -16,11 +18,19 @@ async fn main() {
axum::serve(listener, server()).await.unwrap();
}

async fn weather_handler() -> Json<Value> {
match handle_request().await {
Ok(data) => Json(data), // Return JSON response
Err(_) => Json("failed to fetch data".into()),
}
}

// Router
pub fn server() -> Router {
return Router::new()
.route("/", get(|| async { "Hello, World!" }))
.route("/say-name", get(|| say_name()))
.route("/json-name", get(|| formatted_name()))
.route("/create-name", post(create_name));
.route("/create-name", post(create_name))
.route("/weather", get(weather_handler));
}
46 changes: 46 additions & 0 deletions src/backend/src/routes/handle_fetch_weather.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// use axum::extract::Request as OtherRequest;
// use std::thread::Builder;
// use tokio::runtime::Builder;
// use axum::body::Body;

// pub type Request<T = Body> = Request<T>;

// pub fn handle_request<T>(uri: T) -> Builder {
// let request = Request::get(uri)
// .body(())
// .unwrap();

// println!("this is the data from the request {}", request)
// }


use reqwest::Error;
use serde_json::Value;

// #[derive(Debug)]
// pub struct WeatherDetails {
// name: String,
// country: String,
// region: String,
// lat: String,
// lon: String,
// timezone_id: String,
// localtime: String,
// localtime_epoch: u32,
// }

pub async fn handle_request() -> Result<Value, Error> {
let weather = reqwest::get("https://api.weatherstack.com/current?access_key=ee70f0af3c7ac89490f38705bbfb87c0&query=kaduna")
.await?
.json::<Value>()
.await?;

println!("data from api_______ {:#?}", weather.get("location"));
if let Some(data) = weather.get("location") {
println!("__________ {:#?}", data);
Ok(data.clone())
} else {
Ok(Value::Null)
}
// let weatherDet =
}
2 changes: 1 addition & 1 deletion src/backend/src/routes/handle_name.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub async fn say_name() -> &'static str {
"My name here:!"
"My name here:! martin"
}
1 change: 1 addition & 0 deletions src/backend/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod handle_create_name;
pub mod handle_formatted_name;
pub mod handle_name;
pub mod handle_fetch_weather;