Skip to content

Commit

Permalink
Sasta uses ENV var for Redis URL
Browse files Browse the repository at this point in the history
  • Loading branch information
stagrim committed Nov 11, 2023
1 parent e736d65 commit 91114a2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion casta/src/sasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Sasta {
return None
},
};

let msg = match res {
Ok(m) => m,
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions sasta/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REDIS_URL="<URL to Redis db>" # Example for localhost: "redis://127.0.0.1:6381"
3 changes: 2 additions & 1 deletion sasta/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
content.json
redis-data/
/file_server/
/file_server/
.env
7 changes: 7 additions & 0 deletions sasta/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sasta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ utoipa-swagger-ui = { version = "4.0.0", features = ["axum"] }
utoipa = { version = "4.0.0", features = ["axum_extras", "uuid"] }
utoipa-redoc = { version = "1.0.0", features = ["axum"] }
utoipa-rapidoc = { version = "1.0.0", features = ["axum"] }
dotenv = "0.15.0"
6 changes: 3 additions & 3 deletions sasta/src/file_server/file_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ pub struct FileServer {
}

impl FileServer {
pub async fn new() -> Self {
let client = Client::open("redis://127.0.0.1:6379").unwrap();
pub async fn new(redis_url: &str) -> Self {
let client = Client::open(redis_url).unwrap();
let mut con = client.get_async_connection().await.unwrap();

let root = match con.json_get::<_, _, String>("files", ".").await {
Expand Down Expand Up @@ -309,4 +309,4 @@ impl FileServer {
// error_span!("Logging current state instead", ?self.content);
}
}
}
}
10 changes: 7 additions & 3 deletions sasta/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{net::SocketAddr, sync::Arc, vec, collections::HashSet};
use std::{net::SocketAddr, sync::Arc, vec, collections::HashSet, env};

use axum::{Router, routing::{get, post, put, delete}, extract::{WebSocketUpgrade, ConnectInfo, State, Path}, response::IntoResponse, Json, ServiceExt};
use axum_macros::debug_handler;
use dotenv::dotenv;
use hyper::StatusCode;
use store::store::Store;
use tokio::sync::{oneshot, Mutex};
Expand Down Expand Up @@ -62,14 +63,17 @@ struct ApiDoc;

#[tokio::main]
async fn main() {
dotenv().ok();
let redis_url = env::var("REDIS_URL").expect("REDIS_URL variable must be set");

let subscriber = FmtSubscriber::builder()
.with_span_events(FmtSpan::NEW)
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

let store = Arc::new(Store::new().await);
let file_server = Arc::new(Mutex::new(FileServer::new().await));
let store = Arc::new(Store::new(&redis_url).await);
let file_server = Arc::new(Mutex::new(FileServer::new(&redis_url).await));

let store_copy = store.clone();
let (tx, rx) = oneshot::channel::<()>();
Expand Down
4 changes: 2 additions & 2 deletions sasta/src/store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ pub struct Store {
}

impl Store {
pub async fn new() -> Self {
let client = Client::open("redis://127.0.0.1:6379").unwrap();
pub async fn new(redis_url: &str) -> Self {
let client = Client::open(redis_url).unwrap();
let mut con = client.get_async_connection().await.unwrap();
let (sender, _) = broadcast::channel(5);
let content = RwLock::new(Self::read_file(&mut con).await);
Expand Down

0 comments on commit 91114a2

Please sign in to comment.