Skip to content

Commit

Permalink
Working deserialization for return data
Browse files Browse the repository at this point in the history
  • Loading branch information
BHouwens committed Jun 20, 2024
1 parent 2ddfe45 commit 75d65a2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
8 changes: 4 additions & 4 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This is a sample config TOML for Valence
debug = false
extern_port = 3030
cache_url = "redis://cache"
cache_url = "redis://0.0.0.0"
cache_port = "6379"
cache_password = ""
db_protocol = "mongodb://"
db_url = "db"
db_protocol = "mongodb://0.0.0.0"
db_url = ""
db_port = "27017"
db_user = "root"
db_user = ""
db_password = ""
body_limit = 4096
cache_ttl = 600 # cache lifetime in seconds
Expand Down
36 changes: 18 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ services:
# MONGODB_ROOT_USER: root
# volumes:
# - db:/data/db
valence:
build: .
image: valence
networks:
- app-tier
ports:
- 3030:3030
expose:
- 3030
restart: always
depends_on:
- cache
- db
# links:
# - cache
# - db
volumes:
- ${PWD}/logs:/app/logs
# valence:
# build: .
# image: valence
# networks:
# - app-tier
# ports:
# - 3030:3030
# expose:
# - 3030
# restart: always
# depends_on:
# - cache
# - db
# # links:
# # - cache
# # - db
# volumes:
# - ${PWD}/logs:/app/logs
volumes:
cache:
driver: local
Expand Down
7 changes: 5 additions & 2 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ pub async fn get_data_handler<

// Check cache first
let mut cache_lock_result = cache.lock().await;
let cache_result: Result<Option<Vec<Value>>, _> = cache_lock_result.get_data(address).await;
let cache_result: Result<Option<Vec<String>>, _> = cache_lock_result.get_data::<String>(address).await;

info!("Cache result: {:?}", cache_result);

match cache_result {
Ok(value) => {
match value {
Some(value) => {
info!("Data retrieved from cache");
return r.into_ok("Data retrieved successfully", json_serialize_embed(value));
let data = value.iter().map(|v| serde_json::from_str(v).unwrap()).collect::<Vec<Value>>();
return r.into_ok("Data retrieved successfully", json_serialize_embed(data));
}
None => {
// Default to checking from DB if cache is empty
Expand Down
19 changes: 17 additions & 2 deletions src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ pub async fn retrieve_from_db<D: KvStoreConnection + Clone + Send + 'static>(
let r = CallResponse::new("get_data");
info!("RETRIEVE_FROM_DB requested with address: {:?}", address);

let db_result: Result<Option<Vec<Value>>, _> = db.lock().await.get_data(&address).await;
let db_result: Result<Option<Vec<String>>, _> = db.lock().await.get_data(&address).await;

match db_result {
Ok(data) => r.into_ok("Data retrieved successfully", json_serialize_embed(data)),
Ok(data) => {
match data {
Some(value) => {
info!("Data retrieved from DB");
let data = value
.iter()
.map(|v| serde_json::from_str(v).unwrap())
.collect::<Vec<Value>>();
return r.into_ok("Data retrieved successfully", json_serialize_embed(data));
}
None => {
info!("Data not found in DB");
return r.into_err_internal(ApiErrorType::Generic("Data not found".to_string()));
}
}
}
Err(_) => r.into_err_internal(ApiErrorType::Generic(
"Full Valence chain retrieval failed".to_string(),
)),
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ async fn main() {

let config = load_config();
let cache_addr = format!("{}:{}", config.cache_url, config.cache_port);
let db_addr = format!(
"{}{}:{}@{}:{}",
config.db_protocol, config.db_user, config.db_password, config.db_url, config.db_port
);
// let db_addr = format!(
// "{}{}:{}@{}:{}",
// config.db_protocol, config.db_user, config.db_password, config.db_url, config.db_port
// );
let db_addr = format!("{}{}:{}", config.db_protocol, config.db_url, config.db_port);

info!("Connecting to Redis at {}", cache_addr);
info!("Connecting to MongoDB at {}", db_addr);
Expand Down

0 comments on commit 75d65a2

Please sign in to comment.