summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs212
1 files changed, 91 insertions, 121 deletions
diff --git a/src/main.rs b/src/main.rs
index 5fc638b..7afba14 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,14 +2,11 @@
extern crate fern;
#[macro_use]
extern crate log;
-extern crate clap;
#[macro_use]
extern crate diesel_migrations;
use chrono::Utc;
-use clap::App;
-use clap::Arg;
-use clap::SubCommand;
+use structopt::StructOpt;
use diesel::connection::Connection;
use diesel::pg::PgConnection;
use pjournal::db;
@@ -19,6 +16,55 @@ use serde_json::to_value;
embed_migrations!();
+/// Practice Journal
+#[derive(Debug, StructOpt)]
+struct Opt {
+ /// URL to postgresql database
+ #[structopt(long)]
+ database_url: String,
+
+ /// Endpoint for this web server
+ #[structopt(long, default_value = "http://localhost:8080/")]
+ base_url: String,
+
+ /// Path to directory containing templates
+ #[structopt(long, default_value = "./templates")]
+ template_path: String,
+
+ /// Path to directory containing static files
+ #[structopt(long, default_value = "./static")]
+ static_path: String,
+
+ /// Port on which to server HTTP requests
+ #[structopt(long, default_value = "8000")]
+ port: u16,
+
+ #[structopt(subcommand)]
+ cmd: Option<Command>,
+}
+
+#[derive(Debug, StructOpt)]
+enum Command {
+ /// Initialize config table in the database
+ Init {
+ /// Secret passed to rocket for encrypting cookies
+ rocket_secret_key: String,
+
+ /// Client secret for strava authentication
+ strava_client_secret: String,
+
+ /// Client id for strava authentication
+ strava_client_id: String,
+ },
+ /// Add a user account
+ AddUser {
+ username: String,
+ password: String,
+ },
+ /// Create a ProcessAllRawData task
+ ProcessAllData,
+}
+
fn setup_logger() -> Result<(), fern::InitError> {
use fern::colors::{Color, ColoredLevelConfig};
let colors = ColoredLevelConfig::new();
@@ -71,127 +117,51 @@ fn setup_logger() -> Result<(), fern::InitError> {
}
fn main() {
- let matches = App::new("pjournal")
- .version("0.1")
- .author("KJ Ørbekk <kj@orbekk.com>")
- .about("Practice Journaling")
- .arg(
- Arg::with_name("database_url")
- .long("database_url")
- .required(true)
- .takes_value(true)
- .help("URL to postgresql database"),
- )
- .arg(
- Arg::with_name("base_url")
- .long("base_url")
- .takes_value(true)
- .help("Endpoint for this web server"),
- )
- .arg(
- Arg::with_name("template_path")
- .long("template_path")
- .takes_value(true)
- .help("Path to directory containing templates"),
- )
- .arg(
- Arg::with_name("static_path")
- .long("static_path")
- .takes_value(true)
- .help("Directory containing static files"),
- )
- .arg(
- Arg::with_name("port")
- .long("port")
- .takes_value(true)
- .help("Port to serve http user requests"),
- )
- .subcommand(
- SubCommand::with_name("init")
- .about("initialize database config")
- .arg(
- Arg::with_name("rocket_secret_key")
- .long("rocket_secret_key")
- .takes_value(true)
- .required(true)
- .help("Secret passed to rocket for encrypted cookies"),
- )
- .arg(
- Arg::with_name("strava_client_secret")
- .long("strava_client_secret")
- .takes_value(true)
- .required(true)
- .help("Client secret for strava authentication"),
- )
- .arg(
- Arg::with_name("strava_client_id")
- .long("strava_client_id")
- .takes_value(true)
- .required(true)
- .help("Client id for strava authentication"),
- ),
- )
- .subcommand(
- SubCommand::with_name("adduser")
- .about("add a user account")
- .arg(Arg::with_name("USERNAME").required(true).index(1))
- .arg(Arg::with_name("PASSWORD").required(true).index(2)),
- )
- .subcommand(
- SubCommand::with_name("process_all_data").about("create a ProcessAllRawData task"),
- )
- .get_matches();
-
setup_logger().expect("logger");
+ let opt = Opt::from_args();
- let static_path = matches.value_of("static_path").unwrap_or("./static");
- let port: u16 = matches.value_of("port").unwrap_or("8000").parse().unwrap();
-
- let default_base_url = format!("http://localhost:{}", port);
- let base_url = matches
- .value_of("base_url")
- .unwrap_or(&default_base_url);
-
- let template_path = matches
- .value_of("template_path")
- .unwrap_or("./templates/");
-
- let db_url = matches.value_of("database_url").unwrap();
- let conn = PgConnection::establish(db_url).unwrap();
-
+ let conn = PgConnection::establish(&opt.database_url).unwrap();
embedded_migrations::run(&conn).unwrap();
- if let Some(matches) = matches.subcommand_matches("init") {
- let config = models::Config {
- strava_client_id: matches.value_of("strava_client_id").unwrap().to_string(),
- strava_client_secret: matches
- .value_of("strava_client_secret")
- .unwrap()
- .to_string(),
- rocket_secret_key: matches.value_of("rocket_secret_key").unwrap().to_string(),
- singleton: true,
- };
-
- db::create_config(&conn, &config).unwrap();
- } else if let Some(matches) = matches.subcommand_matches("adduser") {
- let user = matches.value_of("USERNAME").unwrap();
- let password = matches.value_of("PASSWORD").unwrap();
- db::adduser(&conn, user, password).unwrap();
- } else if let Some(_matches) = matches.subcommand_matches("process_all_data") {
- let command = importer::Command::ProcessAllRawData;
- db::insert_task(
- &conn,
- &models::NewTask {
- start_at: Utc::now(),
- state: models::TaskState::NEW,
- username: "system",
- payload: &to_value(command).unwrap(),
- },
- )
- .expect("insert");
- } else {
- info!("Start server");
- pjournal::server::start(conn, db_url, base_url, static_path, port, template_path);
+ match opt.cmd {
+ Some(Command::Init { rocket_secret_key, strava_client_secret, strava_client_id }) => {
+ let config = models::Config {
+ strava_client_id,
+ strava_client_secret,
+ rocket_secret_key,
+ singleton: true,
+ };
+
+ db::create_config(&conn, &config).unwrap();
+ info!("config created");
+ },
+ Some(Command::AddUser { username, password }) => {
+ db::adduser(&conn, &username, &password).unwrap();
+ info!("added user {}", username);
+ },
+ Some(Command::ProcessAllData) => {
+ let command = importer::Command::ProcessAllRawData;
+ db::insert_task(
+ &conn,
+ &models::NewTask {
+ start_at: Utc::now(),
+ state: models::TaskState::NEW,
+ username: "system",
+ payload: &to_value(command).unwrap(),
+ },
+ )
+ .expect("insert");
+ info!("ProcessAllRawData: task inserted");
+ },
+ None => {
+ info!("starting server with options {:?}", opt);
+ pjournal::server::start(conn,
+ &opt.database_url,
+ &opt.base_url,
+ &opt.static_path,
+ opt.port,
+ &opt.template_path);
+ }
}
}