Skip to main content

laurus_server/
gateway.rs

1//! HTTP Gateway module.
2//!
3//! Provides HTTP/JSON endpoints that act as a proxy to the gRPC server.
4//! `User Request (HTTP/JSON) → gRPC Gateway (axum) → gRPC Server (tonic) → Engine`
5
6mod convert;
7mod document;
8mod error;
9mod health;
10mod index;
11mod search;
12
13use axum::Router;
14use axum::routing::{get, post, put};
15use tonic::transport::Channel;
16
17use crate::proto::laurus::v1::document_service_client::DocumentServiceClient;
18use crate::proto::laurus::v1::health_service_client::HealthServiceClient;
19use crate::proto::laurus::v1::index_service_client::IndexServiceClient;
20use crate::proto::laurus::v1::search_service_client::SearchServiceClient;
21
22/// Shared state for the Gateway. Holds each gRPC client.
23#[derive(Clone)]
24pub struct GatewayState {
25    health_client: HealthServiceClient<Channel>,
26    index_client: IndexServiceClient<Channel>,
27    document_client: DocumentServiceClient<Channel>,
28    search_client: SearchServiceClient<Channel>,
29}
30
31impl GatewayState {
32    /// Creates a `GatewayState` from a gRPC channel.
33    pub fn new(channel: Channel) -> Self {
34        Self {
35            health_client: HealthServiceClient::new(channel.clone()),
36            index_client: IndexServiceClient::new(channel.clone()),
37            document_client: DocumentServiceClient::new(channel.clone()),
38            search_client: SearchServiceClient::new(channel),
39        }
40    }
41}
42
43/// Creates the axum router for the Gateway.
44pub fn create_router(state: GatewayState) -> Router {
45    Router::new()
46        .route("/v1/health", get(health::check))
47        .route("/v1/index", post(index::create).get(index::get_index))
48        .route("/v1/schema", get(index::get_schema))
49        .route(
50            "/v1/documents/{id}",
51            put(document::put_document)
52                .post(document::add_document)
53                .get(document::get_documents)
54                .delete(document::delete_documents),
55        )
56        .route("/v1/commit", post(document::commit))
57        .route("/v1/search", post(search::search))
58        .route("/v1/search/stream", post(search::search_stream))
59        .with_state(state)
60}