Skip to content

Commit

Permalink
Merge pull request #339 from systemaccounting/338-request-by-id-rust
Browse files Browse the repository at this point in the history
338 rewrite go request-by-id service in rust
  • Loading branch information
mxfactorial committed Mar 21, 2024
2 parents 06423dc + 9b3f03f commit 0f06ede
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
- 'services/transaction-by-id/**'
- 'services/transactions-by-account/**'
- 'migrations/schema/**'
- 'test/**'
- 'tests/**'
branches-ignore:
- 'master'

Expand Down
15 changes: 15 additions & 0 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 @@ -10,6 +10,7 @@ members = [
"crates/types",
"services/graphql",
"services/request-approve",
"services/request-by-id",
"services/request-create",
"services/rule",
"tests",
Expand Down
14 changes: 7 additions & 7 deletions client/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ test('request detail screen pairs transaction items with rule added items', asyn
await page.getByText('Requests').click();
await page.locator('css=[data-id-req="1"]').click();
await expect(await getFirstNTransactionItems(page, 6)).toEqual(
// migrations/testseed/000003_request.up.sql
[
// migrations/testseed/000003_request.up.sql
{
item_id: "eggs",
quantity: "1",
price: "3",
price: "3.000",
},
{
item_id: salesTax,
quantity: "1",
price: "0.27",
price: "0.270",
},
{
item_id: "bread",
quantity: "2",
price: "2",
price: "2.000",
},
{
item_id: salesTax,
quantity: "2",
price: "0.18",
price: "0.180",
},
{
item_id: "milk",
quantity: "1",
price: "2",
price: "2.000",
},
{
item_id: salesTax,
quantity: "1",
price: "0.18",
price: "0.180",
},
]
);
Expand Down
9 changes: 9 additions & 0 deletions crates/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ impl<T: ModelTrait> Service<T> {
pub async fn get_transaction_by_id(
&self,
transaction_id: i32,
) -> Result<Transaction, Box<dyn Error>> {
self.conn
.select_transaction_by_id_query(transaction_id)
.await
}

pub async fn get_full_transaction_by_id(
&self,
transaction_id: i32,
) -> Result<Transaction, Box<dyn Error>> {
let transaction = self
.get_transaction_with_transaction_items_and_approvals_by_id(transaction_id)
Expand Down
7 changes: 7 additions & 0 deletions crates/types/src/request_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ impl RequestApprove {
}
}
}

#[derive(Debug, Deserialize)]
pub struct QueryById {
pub auth_account: String,
pub account_name: String,
pub id: String,
}
38 changes: 30 additions & 8 deletions docker/dev/request-by-id.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
FROM mxfactorial/go-base:v1 as builder
FROM rust:latest as builder

COPY . .
WORKDIR /app

WORKDIR /app/services/request-by-id
COPY . ./

RUN go build -o request-by-id ./cmd
RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && \
apt install -y musl-tools perl make
RUN update-ca-certificates

FROM golang:alpine
ENV USER=request-by-id
ENV UID=10005

WORKDIR /app
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"

RUN USER=root cargo build \
--manifest-path=services/request-by-id/Cargo.toml \
--target x86_64-unknown-linux-musl \
--release

COPY --from=builder /app/services/request-by-id/request-by-id .
FROM alpine

COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/request-by-id /usr/local/bin

EXPOSE 10005

CMD ["/app/request-by-id"]
USER request-by-id:request-by-id

CMD [ "/usr/local/bin/request-by-id" ]
6 changes: 3 additions & 3 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ scripts:
ssm: null
default:
- 'go run '
- 'debug/rule'
- 'target/debug'
CLIENT_PROCESS:
ssm: null
default: 'bin/vite dev'
Expand Down Expand Up @@ -444,13 +444,13 @@ services:
- RUST_LOG
- RUST_BACKTRACE
request-by-id:
runtime: go1.x
runtime: rust1.x
min_code_cov: null
type: app
local_dev: true
params: []
deploy: true
build_src_path: cmd
build_src_path: null
dependents: []
env_var:
set:
Expand Down
11 changes: 7 additions & 4 deletions services/request-approve/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ async fn handle_event(

let request_id = client_request.id.parse::<i32>().unwrap();

let transaction_request = svc.get_transaction_by_id(request_id).await.map_err(|e| {
tracing::error!("error: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
let transaction_request = svc
.get_full_transaction_by_id(request_id)
.await
.map_err(|e| {
tracing::error!("error: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;

if transaction_request.equilibrium_time.is_some() {
println!("transaction previously approved");
Expand Down
18 changes: 18 additions & 0 deletions services/request-by-id/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "request-by-id"
version = "0.1.0"
edition = "2021"
rust-version.workspace = true

[dependencies]
axum = "0.7.4"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] }
shutdown = { path = "../../crates/shutdown" }
pg = { path = "../../crates/pg" }
service = { path = "../../crates/service" }
types = { path = "../../crates/types" }

[target.x86_64-unknown-linux-musl.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
Loading

0 comments on commit 0f06ede

Please sign in to comment.