diff --git a/rust-containers-k8s/deployment/inventory-service/templates/deployment.yaml b/rust-containers-k8s/deployment/inventory-service/templates/deployment.yaml index e179e78..294d184 100644 --- a/rust-containers-k8s/deployment/inventory-service/templates/deployment.yaml +++ b/rust-containers-k8s/deployment/inventory-service/templates/deployment.yaml @@ -38,7 +38,7 @@ spec: key: inventory_service_database_url readinessProbe: httpGet: - path: /actuator/health + path: /health port: 8082 initialDelaySeconds: 20 - name: cloud-sql-proxy @@ -49,4 +49,3 @@ spec: - {{ .Values.sql.instanceConnectionName }} securityContext: runAsNonRoot: true - diff --git a/rust-containers-k8s/deployment/order-service/templates/deployment.yaml b/rust-containers-k8s/deployment/order-service/templates/deployment.yaml index 030a301..cf29ee2 100644 --- a/rust-containers-k8s/deployment/order-service/templates/deployment.yaml +++ b/rust-containers-k8s/deployment/order-service/templates/deployment.yaml @@ -41,7 +41,7 @@ spec: value: {{ .Values.kafka.url }} readinessProbe: httpGet: - path: /actuator/health + path: /health port: 8081 initialDelaySeconds: 20 - name: cloud-sql-proxy @@ -52,4 +52,3 @@ spec: - {{ .Values.sql.instanceConnectionName }} securityContext: runAsNonRoot: true - diff --git a/rust-containers-k8s/product-service/src/api/handlers.rs b/rust-containers-k8s/product-service/src/api/handlers.rs index 3624568..b6a3e82 100644 --- a/rust-containers-k8s/product-service/src/api/handlers.rs +++ b/rust-containers-k8s/product-service/src/api/handlers.rs @@ -1,4 +1,7 @@ -use super::{server::AppState, types::ProductRequest}; +use super::{ + server::{AppState, COLLECTION_NAME}, + types::ProductRequest, +}; use crate::api::types::{Product, ProductResponse}; use axum::{extract::State, http::StatusCode, response::Result, Json}; use std::sync::Arc; @@ -19,7 +22,7 @@ pub async fn get_all_products( .db .fluent() .select() - .from("products") + .from(COLLECTION_NAME) .limit(1000) .obj() .query() @@ -40,7 +43,7 @@ pub async fn create_product( .db .fluent() .insert() - .into("products") + .into(COLLECTION_NAME) .document_id(&product.id.to_string()) .object(&product) .execute() diff --git a/rust-containers-k8s/product-service/src/api/server.rs b/rust-containers-k8s/product-service/src/api/server.rs index 5d27844..b536acc 100644 --- a/rust-containers-k8s/product-service/src/api/server.rs +++ b/rust-containers-k8s/product-service/src/api/server.rs @@ -1,3 +1,4 @@ +use super::types::Product; use crate::{ api::handlers::{create_product, get_all_products, health, root}, config::Config, @@ -6,7 +7,7 @@ use axum::{ routing::{get, post}, Router, }; -use firestore::FirestoreDb; +use firestore::{FirestoreDb, FirestoreResult}; use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, sync::Arc, @@ -14,11 +15,15 @@ use std::{ use tokio::net::TcpListener; use tower_http::trace::TraceLayer; +pub const COLLECTION_NAME: &str = "products"; + pub struct AppState { pub db: FirestoreDb, } pub async fn create(config: Config, db: FirestoreDb) -> anyhow::Result<()> { + populate_firestore(&db).await?; + let state = Arc::new(AppState { db }); let app = Router::new() @@ -30,9 +35,7 @@ pub async fn create(config: Config, db: FirestoreDb) -> anyhow::Result<()> { .with_state(state); let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), config.port); - let listener = TcpListener::bind(&socket).await.unwrap(); - tracing::info!("listening on {}", socket); axum::serve(listener, app.into_make_service()) .await @@ -40,3 +43,53 @@ pub async fn create(config: Config, db: FirestoreDb) -> anyhow::Result<()> { Ok(()) } + +// populate firestore with some data if it's empty +async fn populate_firestore(db: &FirestoreDb) -> FirestoreResult<()> { + if db + .fluent() + .select() + .from(COLLECTION_NAME) + .limit(1) + .query() + .await? + .len() + == 0 + { + let products = vec![ + Product { + id: uuid::Uuid::new_v4(), + name: "iPhone 13".to_string(), + description: "New iPhone".to_string(), + price: 1000, + sku_code: "iphone_13".to_string(), + }, + Product { + id: uuid::Uuid::new_v4(), + name: "Samsung S23".to_string(), + description: "New Samsung".to_string(), + price: 800, + sku_code: "samsung_s23".to_string(), + }, + Product { + id: uuid::Uuid::new_v4(), + name: "Google Pixel 8".to_string(), + description: "New Pixel".to_string(), + price: 7000, + sku_code: "pixel_8".to_string(), + }, + ]; + + for product in products { + db.fluent() + .insert() + .into(COLLECTION_NAME) + .document_id(&product.id.to_string()) + .object(&product) + .execute() + .await?; + } + } + + Ok(()) +}