Skip to content

Commit

Permalink
Temp commit: Add Tapi
Browse files Browse the repository at this point in the history
  • Loading branch information
oeb25 committed Mar 2, 2024
1 parent d30cb51 commit 7a8ec56
Show file tree
Hide file tree
Showing 29 changed files with 1,107 additions and 193 deletions.
150 changes: 134 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ serde = {version = "1.0.137", features = ["rc", "derive"]}
serde_json = "1.0.81"
serde_urlencoded = "0.7.1"
tantivy = {git = "https://github.com/quickwit-oss/tantivy", rev = "182f58cea"}
tapi = {path = "../tapi/crates/tapi/", package = "tapi", features = ["openapi"]}
thiserror = "1.0.31"
tikv-jemallocator = "0.5"
tokenizers = "0.13.2"
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ serde = {workspace = true}
serde_json = {workspace = true}
serde_urlencoded = {workspace = true}
tantivy = {workspace = true}
tapi = {workspace = true}
thiserror = {workspace = true}
tokenizers = {workspace = true}
tokio = {workspace = true}
Expand Down
31 changes: 15 additions & 16 deletions crates/core/src/api/autosuggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{collections::HashMap, sync::Arc};

use axum::{extract, response::IntoResponse, Json};
use axum::{extract, Json};
use serde::Serialize;
use utoipa::{IntoParams, ToSchema};

use super::State;
use super::AppState;

const HIGHLIGHTED_PREFIX: &str = "<b style=\"font-weight: 500;\">";
const HIGHLIGHTED_POSTFIX: &str = "</b>";
Expand All @@ -39,17 +37,17 @@ fn highlight(query: &str, suggestion: &str) -> String {
new_suggestion
}

#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, tapi::Tapi)]
#[serde(rename_all = "camelCase")]
pub struct Suggestion {
highlighted: String,
raw: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, IntoParams)]
#[derive(Debug, serde::Serialize, serde::Deserialize, IntoParams, tapi::Tapi)]
#[serde(rename_all = "camelCase")]
pub struct AutosuggestQuery {
q: String,
q: Option<String>,
}

#[utoipa::path(
Expand All @@ -60,12 +58,12 @@ pub struct AutosuggestQuery {
(status = 200, description = "Autosuggest", body = Vec<Suggestion>),
)
)]

#[tapi::tapi(path = "/autosuggest", method = Post, state = AppState)]
pub async fn route(
extract::State(state): extract::State<Arc<State>>,
extract::Query(params): extract::Query<HashMap<String, String>>,
) -> impl IntoResponse {
if let Some(query) = params.get("q") {
extract::State(state): extract::State<AppState>,
extract::Query(params): extract::Query<AutosuggestQuery>,
) -> Json<Vec<Suggestion>> {
if let Some(query) = &params.q {
let mut suggestions = Vec::new();

for suggestion in state.autosuggest.suggestions(query).unwrap() {
Expand All @@ -82,11 +80,12 @@ pub async fn route(
}
}

#[tapi::tapi(path = "/autosuggest/browser", method = Post, state = AppState)]
pub async fn browser(
extract::State(state): extract::State<Arc<State>>,
extract::Query(params): extract::Query<HashMap<String, String>>,
) -> impl IntoResponse {
if let Some(query) = params.get("q") {
extract::State(state): extract::State<AppState>,
extract::Query(params): extract::Query<AutosuggestQuery>,
) -> Json<(String, Vec<String>)> {
if let Some(query) = &params.q {
Json((query.clone(), state.autosuggest.suggestions(query).unwrap()))
} else {
Json((String::new(), Vec::new()))
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/api/explore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use axum::extract;
use http::StatusCode;
use optics::{HostRankings, Optic};
use utoipa::ToSchema;

#[derive(serde::Deserialize, ToSchema)]
#[derive(serde::Deserialize, ToSchema, tapi::Tapi)]
#[serde(rename_all = "camelCase")]
pub struct ExploreExportOpticParams {
chosen_hosts: Vec<String>,
Expand All @@ -34,12 +33,13 @@ pub struct ExploreExportOpticParams {
(status = 200, description = "Export explored sites as an optic", body = String),
)
)]
#[tapi::tapi(path = "/expore/export", method = Post)]
pub async fn explore_export_optic(
extract::Json(ExploreExportOpticParams {
chosen_hosts,
similar_hosts,
}): extract::Json<ExploreExportOpticParams>,
) -> Result<String, StatusCode> {
) -> String {
let matches = similar_hosts
.into_iter()
.chain(chosen_hosts.clone().into_iter())
Expand Down Expand Up @@ -69,5 +69,5 @@ pub async fn explore_export_optic(
..Default::default()
};

Ok(optic.to_string())
optic.to_string()
}
Loading

0 comments on commit 7a8ec56

Please sign in to comment.