This commit is contained in:
2022-10-02 12:56:31 +05:30
parent 543ae918f7
commit e624ff4807

View File

@@ -2,14 +2,31 @@ use axum::{response::IntoResponse, routing::get, Json, Router};
use axum_extra::routing::SpaRouter;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
use std::str::FromStr;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
#[derive(Deserialize, Serialize, Debug)]
struct Docs {
key: String,
title: String,
first_publish_year: Option<u32>,
number_of_pages_median: Option<u32>,
isbn: Option<Vec<String>>,
cover_i: Option<u32>,
author_name: Option<Vec<String>>,
person: Option<Vec<String>>,
place: Option<Vec<String>>,
subject: Option<Vec<String>>,
time: Option<Vec<String>>,
}
#[derive(Deserialize, Serialize, Debug)]
struct Books {
num_found: u32,
docs: Vec<Docs>,
}
// Setup the command line interface with clap.
@@ -62,12 +79,31 @@ async fn main() {
.expect("Unable to start server");
}
async fn hello() -> impl IntoResponse {
async fn hello(
axum::extract::Query(params): axum::extract::Query<HashMap<String, String>>,
) -> impl IntoResponse {
//"hello from server!"
let res = reqwest::get("https://openlibrary.org/search.json?q=the+lord+of+the+rings")
.await
.expect("Unable to request");
//let res = reqwest::get("https://openlibrary.org/search.json?q=the+lord+of+the+rings")
// .await
// .expect("Unable to request");
//let resjson = res.json::<Books>().await.expect("Unable to return value");
print!("Get items with query params: {:?}\n", params);
//let search = params.get("search");
//let search = "";
let search = params.get("search").unwrap();
//match search {
// Some(token) => (),
// None => return "None",
//}
//let search = match params.get("search") {
// Some(&token) => token,
// _ => String(""),
//};
let query = format!("https://openlibrary.org/search.json?q={}", search);
let res = reqwest::get(query).await.expect("Unable to request");
let resjson = res.json::<Books>().await.expect("Unable to return value");
print!("Search token {:?}\n", search);
return Json(resjson);
//return "Hello";
}