Skip to content

Commit

Permalink
fix: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan2172001 committed Mar 28, 2024
1 parent 96ddae9 commit fb3aab7
Show file tree
Hide file tree
Showing 35 changed files with 151 additions and 237 deletions.
19 changes: 5 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["src/public", "src/grpc_server"]
members = ["src/public", "src/gpt_answer_service"]
resolver = "2"
8 changes: 4 additions & 4 deletions src/adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ features = ["postgres", "postgres_backend", "uuid"]
[dependencies.rust_core]
path = "../core"

[dependencies.common]
path = "../common"

[dependencies.serde]
version = "1.0"
features = ["derive"]
Expand All @@ -45,7 +48,4 @@ features = ["full"]
version = "1.0.80"

[dependencies.tonic]
version = "0.11.0"

[dependencies.grpc_interface]
path = "../grpc"
version = "0.11.0"
15 changes: 0 additions & 15 deletions src/adapter/src/repositories/grpc/config.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/adapter/src/repositories/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod config;
pub mod models;
18 changes: 10 additions & 8 deletions src/adapter/src/repositories/grpc/models/gpt_answer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rust_core::common::errors::CoreError;
use tonic::transport::Channel;

use grpc_interface::interfaces::gpt_answer::gpt_answer::{
use common::grpc::gpt_answer::gpt_answer::{
gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload,
};
use rust_core::common::errors::CoreError;

pub struct GptAnswerGrpcClient {
client: GptAnswerServiceClient<Channel>,
Expand All @@ -15,11 +15,13 @@ impl GptAnswerGrpcClient {
Self { client }
}

pub async fn get_instance(uri: &'static str) -> Result<Self, CoreError> {
let channel = Channel::from_static(uri).connect().await.map_err(|err| {
eprintln!("Error connecting to GPT: {:?}", err);
CoreError::InternalError
})?;
pub async fn connect(uri: &'static str) -> Result<Self, CoreError> {
let channel: Channel = match Channel::from_static(uri).connect().await {
Ok(channel) => channel,
Err(err) => {
return Err(CoreError::InternalError(err.into()));
}
};

let client = Self::new(channel);
Ok(client)
Expand All @@ -32,7 +34,7 @@ impl GptAnswerGrpcClient {

let response = self.client.get_answer(request).await.map_err(|err| {
eprintln!("Error getting answer from GPT: {:?}", err);
CoreError::InternalError
CoreError::InternalError(err.into())
})?;

Ok(response.into_inner().answer)
Expand Down
9 changes: 4 additions & 5 deletions src/adapter/src/repositories/postgres/models/question.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::{
io::{Error, ErrorKind},
time::SystemTime,
};
use std::io::{Error, ErrorKind};
use std::time::SystemTime;

use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use rust_core::entities::question::{QuestionEntity, QuestionId};
use serde::Serialize;

use rust_core::entities::question::{QuestionEntity, QuestionId};

#[derive(Debug, Queryable, Serialize, Selectable, Insertable, AsChangeset, Identifiable)]
#[diesel(table_name = super::super::schema::questions)]
#[cfg_attr(feature = "postgres", derive(diesel::pg::Pg))]
Expand Down
18 changes: 9 additions & 9 deletions src/adapter/src/repositories/postgres/question_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question =
QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?;
let question = QuestionModel::try_from(question)
.map_err(|err| CoreError::InternalError(err.into()))?;
let response = insert_into(questions)
.values(&question)
.get_result::<QuestionModel>(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
_ => CoreError::InternalError(err.into()),
})
.unwrap();
Ok(response.into())
Expand All @@ -59,14 +59,14 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question =
QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?;
let question = QuestionModel::try_from(question)
.map_err(|err| CoreError::InternalError(err.into()))?;
let response = update(questions.filter(id.eq(question.id)))
.set(&question)
.get_result::<QuestionModel>(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
_ => CoreError::InternalError(err.into()),
})?
.into();

Expand All @@ -87,7 +87,7 @@ impl QuestionPort for QuestionDBRepository {
.execute(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
_ => CoreError::InternalError(err.into()),
})?;

Ok(())
Expand All @@ -109,7 +109,7 @@ impl QuestionPort for QuestionDBRepository {
.first(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
_ => CoreError::InternalError(err.into()),
})?
.into();

Expand All @@ -133,7 +133,7 @@ impl QuestionPort for QuestionDBRepository {
.load(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
_ => CoreError::InternalError(err.into()),
})?;

Ok(question_list
Expand Down
11 changes: 11 additions & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ features = ["derive"]
[dependencies.tracing-subscriber]
version = "0.3.18"
features = ["env-filter"]


[dependencies.tonic]
version = "0.11.0"

[dependencies.prost]
version = "0.12.3"

[build-dependencies]
tonic-build = "0.11.0"
glob = "0.3.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod grpc;
pub mod loggers;
pub mod options;
2 changes: 1 addition & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ features = ["derive"]
version = "1.0.57"

[dependencies]
anyhow = "1.0.80"
anyhow = "1.0.80"
9 changes: 7 additions & 2 deletions src/core/src/common/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::Error;

#[derive(thiserror::Error, Debug)]
pub enum CoreError {
#[error("parse error {0}")]
Expand All @@ -8,10 +10,13 @@ pub enum CoreError {

#[error("missing parameters")]
MissingParameters,

#[error("not found")]
NotFound,
#[error("transparent")]
InternalError,

#[error("internal error {0}")]
InternalError(#[from] Error),

#[error("unknown data store error")]
Unknown,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test = []
example = []

[package]
name = "grpc_server"
name = "gpt_answer_service"
edition = "2021"
version = "0.0.1"
autobins = true
Expand Down Expand Up @@ -44,16 +44,13 @@ tracing = "0.1"
[dependencies.common]
path = "../common"

[dependencies.grpc_interface]
path = "../grpc"

[build-dependencies]
tonic-build = "0.11.0"


[lib]
path = "src/lib.rs"
name = "grpc_server"
name = "gpt_answer_service"
test = true
doctest = true
bench = true
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
service_name = "rust-grpc-server"
exporter_endpoint = "http://localhost:7281"

[servers.gpt_answer_service]
url = "0.0.0.0:50051"
server_endpoint = "0.0.0.0:50051"
23 changes: 23 additions & 0 deletions src/gpt_answer_service/src/controllers/gpt_answer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use tonic::{Request, Response, Status};

use common::grpc::gpt_answer::gpt_answer::{
gpt_answer_service_server::GptAnswerService, GetAnswerPayload, GetAnswerResponse,
};

#[derive(Debug, Default)]
pub struct GptAnswerServer;

#[tonic::async_trait]
impl GptAnswerService for GptAnswerServer {
async fn get_answer(
&self,
request: Request<GetAnswerPayload>,
) -> Result<Response<GetAnswerResponse>, Status> {
let payload = request.into_inner();
// TODO: Implement your logic to generate an answer based on the question.
let answer = format!("Answer to: {}", payload.question);

let response = GetAnswerResponse { answer };
Ok(Response::new(response))
}
}
File renamed without changes.
File renamed without changes.
23 changes: 19 additions & 4 deletions src/grpc_server/src/main.rs → src/gpt_answer_service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use clap::{Parser, Subcommand};
use grpc_server::{controllers, options};
use opentelemetry::global;
use tonic::transport::Server;

use common::grpc::gpt_answer::gpt_answer::gpt_answer_service_server::GptAnswerServiceServer;
use common::loggers::telemetry::init_telemetry;
use common::options::parse_options;
use controllers::gpt_answer::init_gpt_answer_server;
use options::Options;
use gpt_answer_service::controllers::gpt_answer::GptAnswerServer;
use gpt_answer_service::options::Options;

pub async fn init_grpc_server(options: Options) {
let server_endpoint = options.server_endpoint.clone();
let gpt_answer_server = GptAnswerServer::default();
let address = server_endpoint.parse().unwrap();

Server::builder()
.add_service(GptAnswerServiceServer::new(gpt_answer_server))
.serve(address)
.await
.unwrap();

println!("GPT Answer server started at {}", server_endpoint);
}

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -34,7 +49,7 @@ async fn main() {
options.log.level.as_str(),
);

let gpt_answer_server = tokio::spawn(init_gpt_answer_server(options));
let gpt_answer_server = tokio::spawn(init_grpc_server(options));

tokio::try_join!(gpt_answer_server).expect("Failed to run servers");

Expand Down
Loading

0 comments on commit fb3aab7

Please sign in to comment.