From 8f23902bec3600a7ada9b6d054d697315dbd4abc Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 22:23:30 -0700 Subject: [PATCH 1/4] add transaction-by-id service in rust --- Cargo.toml | 1 + docker/dev/transaction-by-id.Dockerfile | 38 ++++++-- project.yaml | 4 +- services/transaction-by-id/Cargo.toml | 18 ++++ services/transaction-by-id/makefile | 20 ++-- services/transaction-by-id/src/main.rs | 123 ++++++++++++++++++++++++ 6 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 services/transaction-by-id/Cargo.toml create mode 100644 services/transaction-by-id/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 842c4978..60355208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "services/request-create", "services/requests-by-account", "services/rule", + "services/transaction-by-id", "tests", ] diff --git a/docker/dev/transaction-by-id.Dockerfile b/docker/dev/transaction-by-id.Dockerfile index b803e14a..8867048f 100644 --- a/docker/dev/transaction-by-id.Dockerfile +++ b/docker/dev/transaction-by-id.Dockerfile @@ -1,17 +1,39 @@ -FROM mxfactorial/go-base:v1 as builder +FROM rust:latest as builder -COPY . . +WORKDIR /app -WORKDIR /app/services/transaction-by-id +COPY . ./ -RUN go build -o transaction-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=transaction-by-id +ENV UID=10007 -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/transaction-by-id/Cargo.toml \ + --target x86_64-unknown-linux-musl \ + --release -COPY --from=builder /app/services/transaction-by-id/transaction-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/transaction-by-id /usr/local/bin EXPOSE 10007 -CMD ["/app/transaction-by-id"] \ No newline at end of file +USER transaction-by-id:transaction-by-id + +CMD [ "/usr/local/bin/transaction-by-id" ] \ No newline at end of file diff --git a/project.yaml b/project.yaml index 75c2cbb1..88bdac50 100644 --- a/project.yaml +++ b/project.yaml @@ -491,13 +491,13 @@ services: - READINESS_CHECK_PATH - RETURN_RECORD_LIMIT transaction-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: diff --git a/services/transaction-by-id/Cargo.toml b/services/transaction-by-id/Cargo.toml new file mode 100644 index 00000000..00099f5e --- /dev/null +++ b/services/transaction-by-id/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "transaction-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"] } diff --git a/services/transaction-by-id/makefile b/services/transaction-by-id/makefile index b202fa35..001cb6f3 100644 --- a/services/transaction-by-id/makefile +++ b/services/transaction-by-id/makefile @@ -1,6 +1,6 @@ RELATIVE_PROJECT_ROOT_PATH=$(shell REL_PATH="."; while [ $$(ls "$$REL_PATH" | grep project.yaml | wc -l | xargs) -eq 0 ]; do REL_PATH="$$REL_PATH./.."; done; printf '%s' "$$REL_PATH") include $(RELATIVE_PROJECT_ROOT_PATH)/make/shared.mk -include $(RELATIVE_PROJECT_ROOT_PATH)/make/go.mk +include $(RELATIVE_PROJECT_ROOT_PATH)/make/rust.mk TRANSACTION_BY_ID_PORT=$(shell yq '.services["$(APP_NAME)"].env_var.set.TRANSACTION_BY_ID_PORT.default' $(PROJECT_CONF)) TRANSACTION_BY_ID_URL=$(HOST):$(TRANSACTION_BY_ID_PORT) @@ -11,13 +11,21 @@ TEST_TRANSACTION_ID=2 TEST_EVENT='{"auth_account":"$(TEST_AUTH_ACCOUNT)","account_name":"$(TEST_AUTH_ACCOUNT)","id":"$(TEST_TRANSACTION_ID)"}' TEST_SENDER_ACCOUNT=$(TEST_ACCOUNT) -run: - @$(DOCKER_ENV_VARS) \ - TEST_EVENT=$(TEST_EVENT) \ - go run ./cmd/main.go +start: + @$(MAKE) get-secrets ENV=local + nohup cargo watch --env-file $(ENV_FILE) -w src -w $(RELATIVE_PROJECT_ROOT_PATH)/crates -x run >> $(NOHUP_LOG) & + +start-alone: + rm -f $(NOHUP_LOG) + $(MAKE) -C $(MIGRATIONS_DIR) run + $(MAKE) start + tail -F $(NOHUP_LOG) + +stop: + $(MAKE) -C $(RELATIVE_PROJECT_ROOT_PATH) stop invoke-local: - @curl -s -d $(TEST_EVENT) $(TRANSACTION_BY_ID_URL) | yq -o=json + @curl -s -H 'Content-Type: application/json' -d $(TEST_EVENT) $(TRANSACTION_BY_ID_URL) | yq -o=json demo: @printf "*** request to %s at %s\n" $(SUB_PATH) $(TRANSACTION_BY_ID_URL) diff --git a/services/transaction-by-id/src/main.rs b/services/transaction-by-id/src/main.rs new file mode 100644 index 00000000..83bff1b0 --- /dev/null +++ b/services/transaction-by-id/src/main.rs @@ -0,0 +1,123 @@ +use axum::{ + extract::{Json, State}, + http::StatusCode, + routing::{get, post}, + Router, +}; +use pg::postgres::{ConnectionPool, DB}; +use service::Service; +use shutdown::shutdown_signal; +use std::{env, net::ToSocketAddrs}; +use tokio::net::TcpListener; +use types::request_response::{IntraTransaction, QueryById}; + +// used by lambda to test for service availability +const READINESS_CHECK_PATH: &str = "READINESS_CHECK_PATH"; + +async fn handle_event( + State(pool): State, + event: Json, +) -> Result, StatusCode> { + let client_request = event.0; + + let svc = Service::new(pool.get_conn().await); + + let transaction_id = client_request.id.parse::().unwrap(); + + let approvals = svc + .get_approvals_by_transaction_id(transaction_id) + .await + .map_err(|e| { + tracing::error!("error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + // test for account in approvals + if approvals + .clone() + .0 + .into_iter() + .filter(|a| a.account_name == client_request.auth_account) + .count() + == 0 + { + tracing::error!("transaction not found"); + return Err(StatusCode::BAD_REQUEST); + } + + // test for missing approval times + if approvals + .clone() + .0 + .into_iter() + .filter(|a| a.approval_time.is_none()) + .count() + > 0 + { + tracing::error!("transaction not found"); + return Err(StatusCode::BAD_REQUEST); + } + + let transaction_items = svc + .get_transaction_items_by_transaction_id(transaction_id) + .await + .map_err(|e| { + tracing::error!("error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + let mut transaction = svc + .get_transaction_by_id(transaction_id) + .await + .map_err(|e| { + tracing::error!("error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + transaction.build(transaction_items, approvals).unwrap(); + + let intra_transaction_request = IntraTransaction::new(client_request.auth_account, transaction); + + Ok(axum::Json(intra_transaction_request)) +} + +#[tokio::main] +async fn main() { + tracing_subscriber::fmt::init(); + + let readiness_check_path = env::var(READINESS_CHECK_PATH) + .unwrap_or_else(|_| panic!("{READINESS_CHECK_PATH} variable assignment")); + + let conn_uri = DB::create_conn_uri_from_env_vars(); + + let pool = DB::new_pool(&conn_uri).await; + + let app = Router::new() + .route("/", post(handle_event)) + .route( + readiness_check_path.as_str(), + get(|| async { StatusCode::OK }), + ) + .with_state(pool); + + let hostname_or_ip = env::var("HOSTNAME_OR_IP").unwrap_or("0.0.0.0".to_string()); + + let port = env::var("TRANSACTION_BY_ID_PORT").unwrap_or("10007".to_string()); + + let serve_addr = format!("{hostname_or_ip}:{port}"); + + let mut addrs_iter = serve_addr.to_socket_addrs().unwrap_or( + format!("{hostname_or_ip}:{port}") + .to_socket_addrs() + .unwrap(), + ); + + let addr = addrs_iter.next().unwrap(); + + tracing::info!("listening on {}", addr); + + axum::serve(TcpListener::bind(addr).await.unwrap(), app) + .with_graceful_shutdown(shutdown_signal()) + .await + .unwrap(); +} From 4be66bace6810157c1eca9e8d2d161af2244f1d3 Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 22:23:56 -0700 Subject: [PATCH 2/4] add decimal precision --- client/tests/test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/tests/test.ts b/client/tests/test.ts index 0e0c50d3..aa1a9568 100644 --- a/client/tests/test.ts +++ b/client/tests/test.ts @@ -94,13 +94,13 @@ test('history detail screen pairs transaction items with rule added items', asyn [ { item_id: "bread", - quantity: "3", - price: "3", + quantity: "3.000", + price: "3.000", }, { item_id: salesTax, - quantity: "3", - price: "0.27", + quantity: "3.000", + price: "0.270", } ] ); From 8419a6f1a5a78e4e524ea41f9ad16a84e66af5cb Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 22:24:18 -0700 Subject: [PATCH 3/4] remove go transaction-by-id service --- services/transaction-by-id/cmd/main.go | 193 ------------------------- 1 file changed, 193 deletions(-) delete mode 100644 services/transaction-by-id/cmd/main.go diff --git a/services/transaction-by-id/cmd/main.go b/services/transaction-by-id/cmd/main.go deleted file mode 100644 index e50decab..00000000 --- a/services/transaction-by-id/cmd/main.go +++ /dev/null @@ -1,193 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "log" - "net/http" - "os" - - "github.com/gin-gonic/gin" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" - "github.com/systemaccounting/mxfactorial/pkg/logger" - "github.com/systemaccounting/mxfactorial/pkg/postgres" - "github.com/systemaccounting/mxfactorial/pkg/service" - "github.com/systemaccounting/mxfactorial/pkg/types" -) - -var ( - pgConn string = fmt.Sprintf( - "host=%s port=%s user=%s password=%s dbname=%s", - os.Getenv("PGHOST"), - os.Getenv("PGPORT"), - os.Getenv("PGUSER"), - os.Getenv("PGPASSWORD"), - os.Getenv("PGDATABASE")) - readinessCheckPath = os.Getenv("READINESS_CHECK_PATH") - port = os.Getenv("TRANSACTION_BY_ID_PORT") -) - -type SQLDB interface { - Query(context.Context, string, ...interface{}) (pgx.Rows, error) - QueryRow(context.Context, string, ...interface{}) pgx.Row - Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) - Begin(context.Context) (pgx.Tx, error) - Close(context.Context) error - IsClosed() bool -} - -type ITransactionService interface { - GetTransactionByID(ID types.ID) (*types.Transaction, error) - InsertTransactionTx(ruleTestedTransaction *types.Transaction) (*types.ID, error) - GetTransactionWithTrItemsAndApprovalsByID(trID types.ID) (*types.Transaction, error) - GetTransactionsWithTrItemsAndApprovalsByID(trIDs types.IDs) (types.Transactions, error) - GetLastNTransactions(accountName string, recordLimit string) (types.Transactions, error) - GetLastNRequests(accountName string, recordLimit string) (types.Transactions, error) - GetTrItemsAndApprovalsByTransactionIDs(trIDs types.IDs) (types.TransactionItems, types.Approvals, error) - GetTrItemsByTransactionID(ID types.ID) (types.TransactionItems, error) - GetTrItemsByTrIDs(IDs types.IDs) (types.TransactionItems, error) - GetApprovalsByTransactionID(ID types.ID) (types.Approvals, error) - GetApprovalsByTransactionIDs(IDs types.IDs) (types.Approvals, error) - AddApprovalTimesByAccountAndRole(trID types.ID, accountName string, accountRole types.Role) (pgtype.Timestamptz, error) -} - -func run( - ctx context.Context, - e types.QueryByID, - dbConnector func(context.Context, string) (SQLDB, error), - tranactionServiceConstructor func(db SQLDB) (ITransactionService, error), -) (string, error) { - - if e.AuthAccount == "" { - err := errors.New("missing auth_account. exiting") - logger.Log(logger.Trace(), err) - return "", err - } - - if e.AccountName == nil || *e.AccountName == "" { - err := errors.New("missing account_name. exiting") - logger.Log(logger.Trace(), err) - return "", err - } - - if e.ID == nil || *e.ID == "" { - err := errors.New("missing id. exiting") - logger.Log(logger.Trace(), err) - return "", err - } - - // connect to db - db, err := dbConnector(context.Background(), pgConn) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - defer db.Close(context.Background()) - - // create transaction service - ts, err := tranactionServiceConstructor(db) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - - // get approvals - apprvs, err := ts.GetApprovalsByTransactionID(*e.ID) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - - accountOccurrence := 0 - pendingApprovalCount := 0 - for _, v := range apprvs { - // count account occurrence in approvals - if *v.AccountName == e.AuthAccount { - accountOccurrence++ - } - // also, count pending approvals - if v.ApprovalTime == nil { - pendingApprovalCount++ - } - } - - // return empty response if account or - // pending approvals found in approvals - if accountOccurrence == 0 || pendingApprovalCount > 0 { - return types.EmptyMarshaledIntraTransaction(e.AuthAccount) - } - - // get transaction items - trItems, err := ts.GetTrItemsByTransactionID(*e.ID) - if err != nil { - log.Print(err) - return "", err - } - - // get transaction - tr, err := ts.GetTransactionByID(*e.ID) - if err != nil { - return "", err - } - - trWithTrItemsAndApprvs := service.BuildTransaction(tr, trItems, apprvs) - - // create transaction for response to client - intraTr := trWithTrItemsAndApprvs.CreateIntraTransaction(e.AuthAccount) - - // send string response to client - return intraTr.MarshalIntraTransaction() -} - -// handleEvent wraps run with interface args for testability -func handleEvent(ctx context.Context, e types.QueryByID) (string, error) { - return run( - ctx, - e, - newIDB, - newTransactionService, - ) -} - -// enables run fn unit testing -func newIDB(ctx context.Context, dsn string) (SQLDB, error) { - return postgres.NewDB(ctx, dsn) -} - -// enables run fn unit testing -func newTransactionService(idb SQLDB) (ITransactionService, error) { - db, ok := idb.(*postgres.DB) - if !ok { - return nil, errors.New("newTransactionService: failed to assert *postgres.DB") - } - return service.NewTransactionService(db), nil -} - -func main() { - - r := gin.Default() - - // aws-lambda-web-adapter READINESS_CHECK_* - r.GET(readinessCheckPath, func(c *gin.Context) { - c.Status(http.StatusOK) - }) - - var queryByID types.QueryByID - - r.POST("/", func(c *gin.Context) { - - c.BindJSON(&queryByID) - - resp, err := handleEvent(c.Request.Context(), queryByID) - if err != nil { - c.Status(http.StatusBadRequest) - } - - c.String(http.StatusOK, resp) - }) - - r.Run(fmt.Sprintf(":%s", port)) -} From 5a74e262ea208b7293fc5b992a298314f84d4faa Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 22:24:23 -0700 Subject: [PATCH 4/4] lock --- Cargo.lock | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fbf00e50..73d400c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4324,6 +4324,21 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "transaction-by-id" +version = "0.1.0" +dependencies = [ + "axum 0.7.4", + "openssl", + "pg", + "service", + "shutdown", + "tokio", + "tracing", + "tracing-subscriber", + "types", +] + [[package]] name = "try-lock" version = "0.2.4"