Skip to content

Commit

Permalink
Set redis url from arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ja573 committed Nov 15, 2024
1 parent 12cf4bc commit 6c009f0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_test_and_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ env:
CARGO_TERM_COLOR: always
THOTH_GRAPHQL_API: https://api.thoth.pub
THOTH_EXPORT_API: https://export.thoth.pub
REDIS_URL: redis://localhost:6379
TEST_REDIS_URL: redis://localhost:6379

jobs:
build:
Expand Down
27 changes: 25 additions & 2 deletions src/bin/thoth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ fn database_argument() -> Arg {
.num_args(1)
}

fn redis_argument() -> Arg {
Arg::new("redis")
.short('R')
.long("redis-url")
.value_name("REDIS_URL")
.env("REDIS_URL")
.help("Full redis url, e.g. redis://localhost:6379")
.num_args(1)
}

fn host_argument(env_value: &'static str) -> Arg {
Arg::new("host")
.short('H')
Expand Down Expand Up @@ -179,6 +189,7 @@ fn thoth_commands() -> Command {
.subcommand(
Command::new("export-api")
.about("Start the thoth metadata export API")
.arg(redis_argument())
.arg(host_argument("EXPORT_API_HOST"))
.arg(port_argument("8181", "EXPORT_API_PORT"))
.arg(threads_argument("EXPORT_API_THREADS"))
Expand Down Expand Up @@ -248,6 +259,10 @@ fn main() -> ThothResult<()> {
app_server(host, port, threads, keep_alive).map_err(|e| e.into())
}
Some(("export-api", client_matches)) => {
let redis_url = client_matches
.get_one::<String>("redis")
.unwrap()
.to_owned();
let host = client_matches.get_one::<String>("host").unwrap().to_owned();
let port = client_matches.get_one::<String>("port").unwrap().to_owned();
let threads = *client_matches.get_one::<usize>("threads").unwrap();
Expand All @@ -260,8 +275,16 @@ fn main() -> ThothResult<()> {
.get_one::<String>("gql-endpoint")
.unwrap()
.to_owned();
export_server(host, port, threads, keep_alive, url, gql_endpoint)
.map_err(|e| e.into())
export_server(
redis_url,
host,
port,
threads,
keep_alive,
url,
gql_endpoint,
)
.map_err(|e| e.into())
}
_ => unreachable!(),
},
Expand Down
20 changes: 12 additions & 8 deletions thoth-api/src/redis.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use deadpool_redis::{redis::AsyncCommands, Config, Connection, Pool};
use dotenv::dotenv;
use std::env;
use thoth_errors::ThothResult;

pub type RedisPool = Pool;
type RedisConnection = Connection;

pub fn init_pool() -> RedisPool {
dotenv().ok();
let redis_url = env::var("REDIS_URL").expect("REDIS_URL must be set");
pub fn init_pool(redis_url: &str) -> RedisPool {
Config::from_url(redis_url)
.builder()
.expect("Failed to create redis pool.")
Expand All @@ -33,17 +29,25 @@ pub async fn get(pool: &RedisPool, key: &str) -> ThothResult<String> {
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
use std::env;

async fn get_pool() -> RedisPool {
dotenv().ok();
let redis_url = env::var("TEST_REDIS_URL").expect("TEST_REDIS_URL must be set");
init_pool(&redis_url)
}

#[tokio::test]
async fn test_init_pool() {
// Ensure that the pool initializes successfully
let pool = init_pool();
let pool = get_pool().await;
assert!(pool.get().await.is_ok());
}

#[tokio::test]
async fn test_set_and_get() {
let pool = init_pool();
let pool = get_pool().await;

let test_key = "test_key";
let test_value = "test_value";
Expand All @@ -58,7 +62,7 @@ mod tests {

#[tokio::test]
async fn test_get_nonexistent_key() {
let pool = init_pool();
let pool = get_pool().await;

let test_key = "nonexistent_key";
let get_result = get(&pool, test_key).await;
Expand Down
3 changes: 2 additions & 1 deletion thoth-export-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async fn index(config: web::Data<ApiConfig>) -> HttpResponse {

#[actix_web::main]
pub async fn start_server(
redis_url: String,
host: String,
port: String,
threads: usize,
Expand Down Expand Up @@ -115,7 +116,7 @@ pub async fn start_server(
.wrap(Cors::default().allowed_methods(vec!["GET", "OPTIONS"]))
.app_data(Data::new(ThothClient::new(gql_endpoint.clone())))
.app_data(Data::new(ApiConfig::new(public_url.clone())))
.app_data(Data::new(init_pool()))
.app_data(Data::new(init_pool(&redis_url)))
.service(actix_web::web::resource("/").route(actix_web::web::get().to(index)))
.wrap_api_with_spec(spec)
.configure(format::route)
Expand Down

0 comments on commit 6c009f0

Please sign in to comment.