Skip to content

Commit

Permalink
populate firestore if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Dec 22, 2023
1 parent 4e2e392 commit 46ef508
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,4 +49,3 @@ spec:
- {{ .Values.sql.instanceConnectionName }}
securityContext:
runAsNonRoot: true

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spec:
value: {{ .Values.kafka.url }}
readinessProbe:
httpGet:
path: /actuator/health
path: /health
port: 8081
initialDelaySeconds: 20
- name: cloud-sql-proxy
Expand All @@ -52,4 +52,3 @@ spec:
- {{ .Values.sql.instanceConnectionName }}
securityContext:
runAsNonRoot: true

9 changes: 6 additions & 3 deletions rust-containers-k8s/product-service/src/api/handlers.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +22,7 @@ pub async fn get_all_products(
.db
.fluent()
.select()
.from("products")
.from(COLLECTION_NAME)
.limit(1000)
.obj()
.query()
Expand All @@ -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()
Expand Down
59 changes: 56 additions & 3 deletions rust-containers-k8s/product-service/src/api/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::types::Product;
use crate::{
api::handlers::{create_product, get_all_products, health, root},
config::Config,
Expand All @@ -6,19 +7,23 @@ use axum::{
routing::{get, post},
Router,
};
use firestore::FirestoreDb;
use firestore::{FirestoreDb, FirestoreResult};
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::Arc,
};
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()
Expand All @@ -30,13 +35,61 @@ 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
.unwrap();

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(())
}

0 comments on commit 46ef508

Please sign in to comment.