From c026a40248acfb40082cc4a560127d7d0def7f26 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 23 Mar 2024 18:16:42 -0700 Subject: [PATCH 1/3] add balance-by-account service in rust --- Cargo.toml | 1 + docker/dev/balance-by-account.Dockerfile | 38 +++++++++--- project.yaml | 4 +- services/balance-by-account/Cargo.toml | 18 ++++++ services/balance-by-account/makefile | 20 +++++-- services/balance-by-account/src/main.rs | 74 ++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 16 deletions(-) create mode 100644 services/balance-by-account/Cargo.toml create mode 100644 services/balance-by-account/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 66e247d4..d38d0641 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "crates/service", "crates/shutdown", "crates/types", + "services/balance-by-account", "services/graphql", "services/request-approve", "services/request-by-id", diff --git a/docker/dev/balance-by-account.Dockerfile b/docker/dev/balance-by-account.Dockerfile index 856e599c..b52f6152 100644 --- a/docker/dev/balance-by-account.Dockerfile +++ b/docker/dev/balance-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/balance-by-account +COPY . ./ -RUN go build -o balance-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=balance-by-account +ENV UID=10004 -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/balance-by-account/Cargo.toml \ + --target x86_64-unknown-linux-musl \ + --release -COPY --from=builder /app/services/balance-by-account/balance-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/balance-by-account /usr/local/bin EXPOSE 10004 -CMD ["/app/balance-by-account"] \ No newline at end of file +USER balance-by-account:balance-by-account + +CMD [ "/usr/local/bin/balance-by-account" ] \ No newline at end of file diff --git a/project.yaml b/project.yaml index e28d3412..1dcc4d4d 100644 --- a/project.yaml +++ b/project.yaml @@ -598,13 +598,13 @@ services: - PGPASSWORD - PGDATABASE balance-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/balance-by-account/Cargo.toml b/services/balance-by-account/Cargo.toml new file mode 100644 index 00000000..06b0c3fb --- /dev/null +++ b/services/balance-by-account/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "balance-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/balance-by-account/makefile b/services/balance-by-account/makefile index 3fe65cf9..47224868 100644 --- a/services/balance-by-account/makefile +++ b/services/balance-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 BALANCE_BY_ACCOUNT_PORT=$(shell yq '.services["$(APP_NAME)"].env_var.set.BALANCE_BY_ACCOUNT_PORT.default' $(PROJECT_CONF)) BALANCE_BY_ACCOUNT_URL=$(HOST):$(BALANCE_BY_ACCOUNT_PORT) @@ -10,13 +10,21 @@ TEST_AUTH_ACCOUNT=$(TEST_ACCOUNT) TEST_EVENT='{"auth_account":"$(TEST_AUTH_ACCOUNT)","account_name":"$(TEST_ACCOUNT)"}' 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) $(BALANCE_BY_ACCOUNT_URL) + @curl -s -H 'Content-Type: application/json' -d $(TEST_EVENT) $(BALANCE_BY_ACCOUNT_URL) demo: @printf "*** request to %s at %s\n" $(SUB_PATH) $(BALANCE_BY_ACCOUNT_URL) diff --git a/services/balance-by-account/src/main.rs b/services/balance-by-account/src/main.rs new file mode 100644 index 00000000..e7c843cc --- /dev/null +++ b/services/balance-by-account/src/main.rs @@ -0,0 +1,74 @@ +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::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 { + let client_request = event.0; + + let svc = Service::new(pool.get_conn().await); + + let account = client_request.account_name; + + let account_balance = svc.get_account_balance(account).await.map_err(|e| { + tracing::error!("error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + Ok(account_balance) +} + +#[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("BALANCE_BY_ACCOUNT_PORT").unwrap_or("10004".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 2a980a5da2c8aef2bfe5c0f4a9dad0677c2fe3f2 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 23 Mar 2024 18:17:01 -0700 Subject: [PATCH 2/3] remove go balance-by-account service --- services/balance-by-account/cmd/main.go | 114 ------------------------ 1 file changed, 114 deletions(-) delete mode 100644 services/balance-by-account/cmd/main.go diff --git a/services/balance-by-account/cmd/main.go b/services/balance-by-account/cmd/main.go deleted file mode 100644 index 1aa4f879..00000000 --- a/services/balance-by-account/cmd/main.go +++ /dev/null @@ -1,114 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "net/http" - "os" - - "github.com/gin-gonic/gin" - "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("BALANCE_BY_ACCOUNT_PORT") -) - -func run( - ctx context.Context, - e types.QueryByAccount, - dbConnector func(context.Context, string) (*postgres.DB, error), - balanceServiceConstructor func(db postgres.SQLDB) (service.IBalanceService, error), -) (string, error) { - - // test required values - 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 account balance service - bs, err := balanceServiceConstructor(db) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - - // get account balance - accountBalance, err := bs.GetAccountBalance(*e.AccountName) - if err != nil { - logger.Log(logger.Trace(), err) - return "", err - } - - // return balance - return accountBalance.CurrentBalance.StringFixed(3), nil -} - -// wraps run accepting services satisfying interfaces for testability -func handleEvent(ctx context.Context, e types.QueryByAccount) (string, error) { - return run( - ctx, - e, - postgres.NewDB, - newBalanceService, - ) -} - -// enables run fn unit testing -func newBalanceService(idb postgres.SQLDB) (service.IBalanceService, error) { - db, ok := idb.(*postgres.DB) - if !ok { - return nil, errors.New("newBalanceService: failed to assert *postgres.DB") - } - return service.NewAccountBalanceService(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 13def1f2775431a3830ecafe911c9931cac1f999 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 23 Mar 2024 18:17:11 -0700 Subject: [PATCH 3/3] cargo lock --- Cargo.lock | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6ac63af6..0a1b28ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -902,6 +902,21 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "balance-by-account" +version = "0.1.0" +dependencies = [ + "axum 0.7.4", + "openssl", + "pg", + "service", + "shutdown", + "tokio", + "tracing", + "tracing-subscriber", + "types", +] + [[package]] name = "base64" version = "0.13.1"