Skip to content

Commit

Permalink
Add config path argument
Browse files Browse the repository at this point in the history
  • Loading branch information
parksb committed Aug 31, 2024
1 parent 63508ae commit ba40a55
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
fs,
path::PathBuf,
sync::Mutex,
};
use std::{fs, path::PathBuf, sync::Mutex};

use collie::{
auth::model::database::keys_table,
Expand All @@ -17,8 +13,8 @@ pub struct AppState {
}

impl AppState {
pub fn new() -> Self {
let config = read_config();
pub fn new(config_path: &Option<PathBuf>) -> Self {
let config = read_config(config_path);
Self {
conn: open_connection(&config),
config,
Expand Down Expand Up @@ -72,9 +68,13 @@ impl Default for Config {
}
}

fn read_config() -> Config {
let config = fs::read_to_string("config.toml")
.unwrap_or(fs::read_to_string("/etc/collied/config.toml").unwrap());
fn read_config(path: &Option<PathBuf>) -> Config {
let config = match path {
Some(path) => fs::read_to_string(path).unwrap(),
None => fs::read_to_string("config.toml")
.unwrap_or(fs::read_to_string("/etc/collied/config.toml").unwrap()),
};

toml::from_str(&config).expect("Failed to parse config file.")
}

Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::OpenOptions, sync::Arc};
use std::{fs::OpenOptions, path::PathBuf, sync::Arc};

use clap::{Parser, Subcommand};
use config::AppState;
Expand All @@ -17,6 +17,9 @@ mod adapter {
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long)]
config: Option<String>,

#[command(subcommand)]
commands: Commands,
}
Expand Down Expand Up @@ -53,6 +56,7 @@ pub enum KeyCommands {

fn main() {
let cli = Cli::parse();
let config_path = cli.config.as_ref().map(PathBuf::from);

match &cli.commands {
Commands::Serve { port, daemon } => {
Expand All @@ -62,7 +66,7 @@ fn main() {
if *daemon { "daemon" } else { "foreground" }
);

let app_state = Arc::new(AppState::new());
let app_state = Arc::new(AppState::new(&config_path));

if *daemon {
let daemonize = Daemonize::new().pid_file(&app_state.config.daemon.pid_file);
Expand All @@ -87,7 +91,8 @@ fn main() {
KeyCommands::New { description } => {
println!("Generating new keys...");
let (access_key, secret_key) =
collie::auth::key::create(AppState::new().conn, description).unwrap();
collie::auth::key::create(AppState::new(&config_path).conn, description)
.unwrap();
println!("Register the following keys with your client. DO NOT share the secret key with anyone.");
println!("Access key: {}", access_key);
println!("Secret key: {}", secret_key);
Expand Down
7 changes: 5 additions & 2 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ pub async fn serve(app_state: Arc<AppState>, addr: &str) {
let AppState { conn, config, .. } = &*app_state;

loop {
let _ = create_new_items(&conn, &config.producer.proxy);
tokio::time::sleep(std::time::Duration::from_secs(config.producer.polling_frequency)).await;
let _ = create_new_items(conn, &config.producer.proxy);
tokio::time::sleep(std::time::Duration::from_secs(
config.producer.polling_frequency,
))
.await;
}
});
}
Expand Down

0 comments on commit ba40a55

Please sign in to comment.