From 75bfb19af889842f899201e1a2869b4cccd99888 Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 20:19:13 -0700 Subject: [PATCH 1/5] QueryByAccount rust type --- crates/types/src/request_response.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/types/src/request_response.rs b/crates/types/src/request_response.rs index 9156d2b2..36ecdcff 100644 --- a/crates/types/src/request_response.rs +++ b/crates/types/src/request_response.rs @@ -38,6 +38,15 @@ pub struct IntraTransactions { pub transactions: Transactions, } +impl IntraTransactions { + pub fn new(auth_account: String, transactions: Transactions) -> Self { + Self { + auth_account: Some(auth_account), + transactions, + } + } +} + #[derive(Eq, PartialEq, Debug, Deserialize, Serialize)] pub struct RequestApprove { pub auth_account: String, @@ -68,3 +77,9 @@ pub struct QueryById { pub account_name: String, pub id: String, } + +#[derive(Debug, Deserialize)] +pub struct QueryByAccount { + pub auth_account: String, + pub account_name: String, +} From 42d974a7ba2de87ed8ee64a8f303f823282a96b4 Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 20:20:23 -0700 Subject: [PATCH 2/5] add requests-by-account service in rust --- Cargo.toml | 1 + docker/dev/requests-by-account.Dockerfile | 38 +++++++--- project.yaml | 4 +- services/requests-by-account/Cargo.toml | 18 +++++ services/requests-by-account/makefile | 21 ++++-- services/requests-by-account/src/main.rs | 88 +++++++++++++++++++++++ 6 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 services/requests-by-account/Cargo.toml create mode 100644 services/requests-by-account/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index bc462951..842c4978 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "services/request-approve", "services/request-by-id", "services/request-create", + "services/requests-by-account", "services/rule", "tests", ] diff --git a/docker/dev/requests-by-account.Dockerfile b/docker/dev/requests-by-account.Dockerfile index b6bfc6a8..99f6e5f4 100644 --- a/docker/dev/requests-by-account.Dockerfile +++ b/docker/dev/requests-by-account.Dockerfile @@ -1,17 +1,39 @@ -FROM mxfactorial/go-base:v1 as builder +FROM rust:latest as builder -COPY . . +WORKDIR /app -WORKDIR /app/services/requests-by-account +COPY . ./ -RUN go build -o requests-by-account ./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=requests-by-account +ENV UID=10006 -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/requests-by-account/Cargo.toml \ + --target x86_64-unknown-linux-musl \ + --release -COPY --from=builder /app/services/requests-by-account/requests-by-account . +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/requests-by-account /usr/local/bin EXPOSE 10006 -CMD ["/app/requests-by-account"] \ No newline at end of file +USER requests-by-account:requests-by-account + +CMD [ "/usr/local/bin/requests-by-account" ] \ No newline at end of file diff --git a/project.yaml b/project.yaml index c81893e7..75c2cbb1 100644 --- a/project.yaml +++ b/project.yaml @@ -467,13 +467,13 @@ services: - PGDATABASE - READINESS_CHECK_PATH requests-by-account: - 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/requests-by-account/Cargo.toml b/services/requests-by-account/Cargo.toml new file mode 100644 index 00000000..a864cbf1 --- /dev/null +++ b/services/requests-by-account/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "requests-by-account" +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/requests-by-account/makefile b/services/requests-by-account/makefile index 60a2d75d..ceaccda7 100644 --- a/services/requests-by-account/makefile +++ b/services/requests-by-account/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 REQUESTS_BY_ACCOUNT_PORT=$(shell yq '.services["$(APP_NAME)"].env_var.set.REQUESTS_BY_ACCOUNT_PORT.default' $(PROJECT_CONF)) REQUESTS_BY_ACCOUNT_URL=$(HOST):$(REQUESTS_BY_ACCOUNT_PORT) @@ -10,14 +10,21 @@ TEST_ACCOUNT=JacobWebb TEST_AUTH_ACCOUNT=$(TEST_ACCOUNT) TEST_EVENT='{"auth_account":"$(TEST_AUTH_ACCOUNT)","account_name":"$(TEST_ACCOUNT)"}' -run: - @$(DOCKER_ENV_VARS) \ - RETURN_RECORD_LIMIT=$(RETURN_RECORD_LIMIT) \ - 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) $(REQUESTS_BY_ACCOUNT_URL) | yq -o=json + @curl -s -H 'Content-Type: application/json' -d $(TEST_EVENT) $(REQUESTS_BY_ACCOUNT_URL) | yq -o=json demo: @printf "*** request to %s at %s\n" $(SUB_PATH) $(REQUESTS_BY_ACCOUNT_URL) diff --git a/services/requests-by-account/src/main.rs b/services/requests-by-account/src/main.rs new file mode 100644 index 00000000..9aaeb859 --- /dev/null +++ b/services/requests-by-account/src/main.rs @@ -0,0 +1,88 @@ +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::{IntraTransactions, QueryByAccount}; + +// 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 account = client_request.account_name; + + let record_limit = env::var("RETURN_RECORD_LIMIT") + .unwrap_or_else(|_| panic!("RETURN_RECORD_LIMIT variable assignment")) + .parse::() + .unwrap(); + + let transaction_requests = svc + .get_last_n_requests(account.clone(), record_limit) + .await + .map_err(|e| { + tracing::error!("error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + if transaction_requests.is_empty() { + tracing::error!("transaction requests not found"); + } + + let response = IntraTransactions::new(account, transaction_requests); + + Ok(axum::Json(response)) +} + +#[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("REQUESTS_BY_ACCOUNT_PORT").unwrap_or("10006".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 027a825874e9b3240dc95779418d6c298884ad56 Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 20:20:41 -0700 Subject: [PATCH 3/5] remove go requests-by-account service --- services/requests-by-account/cmd/main.go | 163 ----------------------- 1 file changed, 163 deletions(-) delete mode 100644 services/requests-by-account/cmd/main.go diff --git a/services/requests-by-account/cmd/main.go b/services/requests-by-account/cmd/main.go deleted file mode 100644 index 463f52fa..00000000 --- a/services/requests-by-account/cmd/main.go +++ /dev/null @@ -1,163 +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 ( - recordLimit string = os.Getenv("RETURN_RECORD_LIMIT") - 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("REQUESTS_BY_ACCOUNT_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.QueryByAccount, - dbConnector func(context.Context, string) (SQLDB, error), - tranactionServiceConstructor func(db SQLDB) (ITransactionService, error), -) (string, error) { - - if e.AuthAccount == "" { - return "", errors.New("missing auth_account. exiting") - } - - if e.AccountName == nil { - return "", errors.New("missing account_name. exiting") - } - - // 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 requests - requests, err := ts.GetLastNRequests(e.AuthAccount, recordLimit) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - - // test for empty request list - if len(requests) == 0 { - - log.Println("0 requests found") - - // send empty response to client - return types.EmptyMarshaledIntraTransaction(e.AuthAccount) - } - - // create for response to client - intraTrs := requests.CreateIntraTransactions(e.AuthAccount) - - // send string or error response to client - return intraTrs.MarshalIntraTransactions() -} - -// wraps run which accepts interfaces for testability -func handleEvent( - ctx context.Context, - e types.QueryByAccount, -) (string, error) { - return run( - ctx, - e, - newIDB, - newTransactionService, - ) -} - -// enables run unit testing -func newIDB(ctx context.Context, dsn string) (SQLDB, error) { - return postgres.NewDB(ctx, dsn) -} - -// enables run 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 queryByAccount types.QueryByAccount - - r.POST("/", func(c *gin.Context) { - - c.BindJSON(&queryByAccount) - - resp, err := handleEvent(c.Request.Context(), queryByAccount) - if err != nil { - c.Status(http.StatusBadRequest) - } - - c.String(http.StatusOK, resp) - }) - - r.Run(fmt.Sprintf(":%s", port)) -} From d2959ef18bcf4018467d896039cf9a80230eed9a Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 20:20:59 -0700 Subject: [PATCH 4/5] add graphql param --- tests/thunder-tests/thunderclient.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/thunder-tests/thunderclient.json b/tests/thunder-tests/thunderclient.json index 5edd8480..9b3dfd68 100644 --- a/tests/thunder-tests/thunderclient.json +++ b/tests/thunder-tests/thunderclient.json @@ -202,7 +202,7 @@ "method": "POST", "sortNum": 70000, "created": "2023-01-22T03:27:31.004Z", - "modified": "2023-06-07T04:44:13.001Z", + "modified": "2024-03-22T02:43:46.234Z", "headers": [ { "name": "Content-Type", @@ -219,8 +219,8 @@ "raw": "", "form": [], "graphql": { - "query": "query getRequestByID($id: String!, $auth_account: String!) {\n requestByID(id: $id, auth_account: $auth_account) {\n id\n rule_instance_id\n author\n author_device_id\n author_device_latlng\n author_role\n transaction_items {\n id\n transaction_id\n item_id\n price\n quantity\n debitor_first\n rule_exec_ids\n rule_instance_id\n unit_of_measurement\n units_measured\n debitor\n creditor\n debitor_profile_id\n creditor_profile_id\n debitor_approval_time\n creditor_approval_time\n debitor_expiration_time\n creditor_expiration_time\n debitor_rejection_time\n creditor_rejection_time\n }\n }\n}", - "variables": "{\n \"auth_account\": \"JacobWebb\",\n \"id\": \"1\"\n}" + "query": "query getRequestByID($id: String!, $account_name: String!, $auth_account: String!) {\n requestByID(id: $id, account_name: $account_name, auth_account: $auth_account) {\n id\n rule_instance_id\n author\n author_device_id\n author_device_latlng\n author_role\n transaction_items {\n id\n transaction_id\n item_id\n price\n quantity\n debitor_first\n rule_exec_ids\n rule_instance_id\n unit_of_measurement\n units_measured\n debitor\n creditor\n debitor_profile_id\n creditor_profile_id\n debitor_approval_time\n creditor_approval_time\n debitor_expiration_time\n creditor_expiration_time\n debitor_rejection_time\n creditor_rejection_time\n }\n }\n}", + "variables": "{\n \"auth_account\": \"JacobWebb\",\n \"account_name\": \"JacobWebb\",\n \"id\": \"1\"\n}" } }, "tests": [] From c7ce3dd1b14082ecc773f051a38a375cb616fc79 Mon Sep 17 00:00:00 2001 From: max funk Date: Thu, 21 Mar 2024 20:21:04 -0700 Subject: [PATCH 5/5] lock --- Cargo.lock | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 2bf3c514..fbf00e50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3214,6 +3214,21 @@ dependencies = [ "types", ] +[[package]] +name = "requests-by-account" +version = "0.1.0" +dependencies = [ + "axum 0.7.4", + "openssl", + "pg", + "service", + "shutdown", + "tokio", + "tracing", + "tracing-subscriber", + "types", +] + [[package]] name = "reqwest" version = "0.11.25"