Skip to content

Commit

Permalink
fix: docker flow
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan2172001 committed Mar 23, 2024
1 parent fe70414 commit 96ddae9
Show file tree
Hide file tree
Showing 32 changed files with 453 additions and 123 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [cargo-check, fmt-check, test-and-coverage]
# needs: [fmt-check]
steps:
- name: Check out from Git
uses: actions/checkout@v4
Expand All @@ -89,7 +88,7 @@ jobs:
- uses: arduino/setup-protoc@v3

- name: Release Build
run: cargo build --release --bin cli
run: cargo build --release --all

- name: "Upload Artifact"
uses: actions/upload-artifact@v4
Expand All @@ -103,7 +102,6 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [cargo-check, fmt-check, test-and-coverage]
# needs: [fmt-check]
steps:
- name: Check out from Git
uses: actions/checkout@v4
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Docker build

on:
pull_request:
branches:
- main

jobs:
build-docker-image:
runs-on: ubuntu-latest
steps:
- name: Check out from Git
uses: actions/checkout@v4

- name: Build docker image
run: |
docker build .
74 changes: 37 additions & 37 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"]
members = ["src/public", "src/grpc_server"]
resolver = "2"
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FROM clux/muslrust:stable AS chef
USER root
RUN cargo install cargo-chef

WORKDIR /app

FROM clux/muslrust:stable AS bunyan
Expand All @@ -16,7 +17,7 @@ FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release --bin cli
RUN cargo build --release --all
RUN mv target/${CARGO_BUILD_TARGET}/release /out

FROM scratch AS prod
Expand Down
8 changes: 7 additions & 1 deletion src/adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ version = "1.36.0"
features = ["full"]

[dependencies.anyhow]
version = "1.0.80"
version = "1.0.80"

[dependencies.tonic]
version = "0.11.0"

[dependencies.grpc_interface]
path = "../grpc"
15 changes: 15 additions & 0 deletions src/adapter/src/repositories/grpc/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::Deserialize;

/// Represents servers configuration options.
#[derive(Deserialize, Debug)]
pub struct GrpcServers {
/// Configuration for using in-memory database.
pub gpt_answer_service: Option<ServiceServer>,
}

/// Represents service server configuration.
#[derive(Debug, Deserialize, Clone)]
pub struct ServiceServer {
/// URL for the server.
pub url: String,
}
2 changes: 2 additions & 0 deletions src/adapter/src/repositories/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod config;
pub mod models;
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use rust_core::common::errors::CoreError;
use tonic::transport::Channel;

use gpt_answer::gpt_answer_service_client::GptAnswerServiceClient;

mod gpt_answer {
tonic::include_proto!("gpt_answer");
}
use grpc_interface::interfaces::gpt_answer::gpt_answer::{
gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload,
};

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

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

Expand All @@ -29,12 +26,12 @@ impl GptAnswerGrpcClient {
}

pub async fn get_answer(&mut self, question: &str) -> Result<String, CoreError> {
let request = tonic::Request::new(gpt_answer::GetAnswerPayload {
let request = tonic::Request::new(GetAnswerPayload {
question: question.to_string(),
});

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

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/adapter/src/repositories/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod grpc;
pub mod in_memory;
pub mod postgres;
pub mod repository_test;
32 changes: 22 additions & 10 deletions src/adapter/src/repositories/postgres/models/question.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::time::SystemTime;
use std::{
io::{Error, ErrorKind},
time::SystemTime,
};

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

#[derive(Debug, Queryable, Serialize, Selectable, Insertable, AsChangeset, Identifiable)]
Expand All @@ -20,23 +22,33 @@ pub struct QuestionModel {
pub created_on: SystemTime,
}

impl QuestionModel {
pub fn from(entity: QuestionEntity) -> Result<Self, CoreError> {
impl TryFrom<QuestionEntity> for QuestionModel {
type Error = Error;

fn try_from(entity: QuestionEntity) -> Result<QuestionModel, Self::Error> {
let id = entity
.id
.0
.parse()
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Invalid ID"))?;

Ok(QuestionModel {
id: entity.id.to_string().parse()?,
id,
title: entity.title,
content: entity.content,
tags: entity.tags.map(|v| v.into_iter().map(Some).collect()),
created_on: SystemTime::now(),
})
}
}

pub fn to_entity(self) -> Result<QuestionEntity, CoreError> {
Ok(QuestionEntity {
id: self.id.to_string().parse()?,
impl Into<QuestionEntity> for QuestionModel {
fn into(self) -> QuestionEntity {
QuestionEntity {
id: QuestionId(self.id.to_string()),
title: self.title,
content: self.content,
tags: self.tags.map(|v| v.into_iter().flatten().collect()),
})
}
}
}
Loading

0 comments on commit 96ddae9

Please sign in to comment.