-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added initial version of search APIs
- Loading branch information
1 parent
4607ba3
commit e4ae334
Showing
14 changed files
with
266 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
db = "postgresql://postgres:postgres@localhost/curieo" | ||
cache = "redis://127.0.0.1/" | ||
|
||
[log] | ||
level = "info" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
create table search_history ( | ||
search_history_id uuid primary key default uuid_generate_v1mc(), | ||
search_text text not null, | ||
response_text text not null, | ||
response_sources text[] not null, | ||
created_at timestamptz not null default now(), | ||
updated_at timestamptz not null default now() | ||
); | ||
|
||
-- And applying our `updated_at` trigger is as easy as this. | ||
SELECT trigger_updated_at('search_history'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub use models::*; | ||
pub use routes::*; | ||
pub use services::*; | ||
|
||
pub mod models; | ||
pub mod routes; | ||
pub mod services; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::types::time; | ||
use sqlx::FromRow; | ||
use std::fmt::Debug; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchQueryRequest { | ||
pub query: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchHistoryRequest { | ||
pub limit: Option<u8>, | ||
pub offset: Option<u8>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SearchResponse { | ||
pub response_text: String, | ||
pub response_sources: Vec<String>, | ||
} | ||
|
||
#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] | ||
pub struct SearchHistory { | ||
pub search_history_id: uuid::Uuid, | ||
// pub user_id: uuid::Uuid, | ||
pub search_text: String, | ||
pub response_text: String, | ||
pub response_sources: Vec<String>, | ||
|
||
pub created_at: time::OffsetDateTime, | ||
pub updated_at: time::OffsetDateTime, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::err::AppError; | ||
use crate::search::services; | ||
use crate::search::{SearchHistory, SearchHistoryRequest, SearchQueryRequest}; | ||
use crate::startup::AppState; | ||
use axum::extract::{Query, State}; | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
use axum::routing::get; | ||
use axum::{Json, Router}; | ||
use redis::Client as RedisClient; | ||
use sqlx::PgPool; | ||
|
||
#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))] | ||
async fn get_search_handler( | ||
State(pool): State<PgPool>, | ||
State(cache): State<RedisClient>, | ||
Query(search_query): Query<SearchQueryRequest>, | ||
) -> crate::Result<impl IntoResponse> { | ||
let mut connection = cache | ||
.get_multiplexed_async_connection() | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
let search_response = services::search(&mut connection, &search_query).await?; | ||
services::insert_search_history(&pool, &mut connection, &search_query, &search_response) | ||
.await?; | ||
|
||
Ok((StatusCode::OK, Json(search_response))) | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all, ret, err(Debug))] | ||
async fn get_search_history_handler( | ||
State(pool): State<PgPool>, | ||
Query(search_history_request): Query<SearchHistoryRequest>, | ||
) -> crate::Result<impl IntoResponse> { | ||
let search_history = sqlx::query_as!( | ||
SearchHistory, | ||
"select * from search_history order by created_at desc limit $1 offset $2", | ||
search_history_request.limit.unwrap_or(10) as i64, | ||
search_history_request.offset.unwrap_or(0) as i64 | ||
) | ||
.fetch_all(&pool) | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
Ok((StatusCode::OK, Json(search_history))) | ||
} | ||
|
||
pub fn routes() -> Router<AppState> { | ||
Router::new() | ||
.route("/", get(get_search_handler)) | ||
.route("/history", get(get_search_history_handler)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::err::AppError; | ||
use crate::search::{SearchHistory, SearchQueryRequest, SearchResponse}; | ||
use color_eyre::eyre::eyre; | ||
use redis::aio::MultiplexedConnection; | ||
use redis::AsyncCommands; | ||
use sqlx::PgPool; | ||
|
||
#[tracing::instrument(level = "debug", ret, err)] | ||
pub async fn search( | ||
cache: &mut MultiplexedConnection, | ||
search_query: &SearchQueryRequest, | ||
) -> crate::Result<SearchResponse> { | ||
let cache_response: Option<String> = cache | ||
.get(search_query.query.clone()) | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
let cache_response = match cache_response { | ||
Some(response) => response, | ||
None => String::new(), | ||
}; | ||
|
||
let cache_response: Option<SearchResponse> = | ||
serde_json::from_str(&cache_response).unwrap_or(None); | ||
|
||
if let Some(response) = cache_response { | ||
return Ok(response); | ||
} | ||
|
||
// sleep for 3 seconds to simulate a slow search | ||
// TODO: replace this with actual search logic using GRPC calls with backend services | ||
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; | ||
|
||
let response = SearchResponse { | ||
response_text: "sample response".to_string(), | ||
response_sources: vec!["www.source1.com".to_string(), "www.source2.com".to_string()], | ||
}; | ||
|
||
return Ok(response); | ||
} | ||
|
||
#[tracing::instrument(level = "debug", ret, err)] | ||
pub async fn insert_search_history( | ||
pool: &PgPool, | ||
cache: &mut MultiplexedConnection, | ||
search_query: &SearchQueryRequest, | ||
search_response: &SearchResponse, | ||
) -> crate::Result<SearchHistory> { | ||
cache | ||
.set( | ||
&search_query.query, | ||
serde_json::to_string(&search_response) | ||
.map_err(|_| eyre!("unable to convert string to json"))?, | ||
) | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
let search_history = sqlx::query_as!( | ||
SearchHistory, | ||
"insert into search_history (search_text, response_text, response_sources) values ($1, $2, $3) returning *", | ||
&search_query.query, | ||
&search_response.response_text, | ||
&search_response.response_sources | ||
) | ||
.fetch_one(pool) | ||
.await | ||
.map_err(|e| AppError::from(e))?; | ||
|
||
return Ok(search_history); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.