Skip to content

Commit

Permalink
Worked on cli
Browse files Browse the repository at this point in the history
  • Loading branch information
emilpriver committed Sep 24, 2023
1 parent 4e5cc32 commit 52d4a5e
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 27 deletions.
109 changes: 109 additions & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ edition = "2021"
actix = "0.13.1"
actix-web = "4.4.0"
anyhow = "1.0.75"
cfg-if = "1.0.0"
chrono = { version = "0.4.31", features = ["serde"] }
clap = { version = "4.4.4", features = ["string", "env", "derive", "debug"] }
httptest = "0.15.4"
hyper = { version = "0.14.27", features = ["full"] }
hyper-tls = "0.5.0"
Expand All @@ -23,4 +25,9 @@ tokio = { version = "1.32.0", features = ["full"] }
uuid = { version = "1.4.1", features = ["serde", "v4"] }

[profile.dev]
opt-level = 3
opt-level = 3

[features]
default = ["web"]
web = []
cli = []
22 changes: 22 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Name of the person to greet
#[arg(short, long)]
name: String,

/// Number of times to greet
#[arg(short, long, default_value_t = 1)]
count: u8,
}

pub fn run() {
let args = Args::parse();

for _ in 0..args.count {
println!("Hello {}!", args.name)
}
}
36 changes: 10 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
use actix_web::{web, App, HttpServer};
use routes::{create_test, get_test};
use simple_logger::SimpleLogger;

mod benchmark;
mod cli;
mod database;
mod routes;
mod utils;

mod routes;
mod web;

#[tokio::main(flavor = "multi_thread")]
async fn main() -> std::io::Result<()> {
match SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.init()
{
Err(err) => {
println!("failed to setup logging: {:?}", err);
cfg_if::cfg_if! {
if #[cfg(target_feature = "cli")] {
cli::run();
}else {
web::run_web_app().await;
}
_ => {}
}

let connection = match database::setup().await {
Ok(c) => c,
Err(err) => panic!("failed to setup database: {:?}", err),
};

HttpServer::new(move || {
App::new()
.service(get_test)
.service(create_test)
.app_data(web::Data::new(connection.clone()))
})
.bind(("0.0.0.0", 3000))?
.run()
.await
Ok(())
}
35 changes: 35 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::database;
use crate::routes::{create_test, get_test};
use actix_web::{web, App, HttpServer};
use simple_logger::SimpleLogger;

pub async fn run_web_app() {
match SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.init()
{
Err(err) => {
println!("failed to setup logging: {:?}", err);
}
_ => {}
}

let connection = match database::setup().await {
Ok(c) => c,
Err(err) => panic!("failed to setup database: {:?}", err),
};

let server = match HttpServer::new(move || {
App::new()
.service(get_test)
.service(create_test)
.app_data(web::Data::new(connection.clone()))
})
.bind(("0.0.0.0", 3000))
{
Ok(s) => s,
Err(err) => panic!("failed to create server: {:?}", err),
};

server.run().await.unwrap();
}

0 comments on commit 52d4a5e

Please sign in to comment.