diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-12-27 00:43:31 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-12-27 00:43:31 -0800 |
commit | 946c98593cb5346fff3d1aa72c4992376ec20471 (patch) | |
tree | bc0c2613f95203c7c79903cc754f3db8a450df17 /rust/src/bin/fatcat-auth.rs | |
parent | 3e591d8d7f64b1092f68737ebd464be69f7e2490 (diff) | |
download | fatcat-946c98593cb5346fff3d1aa72c4992376ec20471.tar.gz fatcat-946c98593cb5346fff3d1aa72c4992376ec20471.zip |
sql codegen and WIP on auth command
Diffstat (limited to 'rust/src/bin/fatcat-auth.rs')
-rw-r--r-- | rust/src/bin/fatcat-auth.rs | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/rust/src/bin/fatcat-auth.rs b/rust/src/bin/fatcat-auth.rs index 7cb8af8e..a5fedc1f 100644 --- a/rust/src/bin/fatcat-auth.rs +++ b/rust/src/bin/fatcat-auth.rs @@ -2,6 +2,7 @@ #[macro_use] extern crate clap; +extern crate diesel; extern crate dotenv; #[macro_use] extern crate error_chain; @@ -16,6 +17,9 @@ use clap::{App, Arg, SubCommand}; use dotenv::dotenv; use std::env; +use diesel::prelude::*; +use diesel::r2d2::ConnectionManager; +use fatcat::ConnectionPool; use fatcat::errors::*; use fatcat::api_helpers::FatCatId; use std::str::FromStr; @@ -27,6 +31,18 @@ use std::io::prelude::*; use std::io::{BufReader, BufWriter}; +/// Instantiate a new API server with a pooled database connection +// TODO: copypasta from fatcat-export +pub fn database_worker_pool() -> Result<ConnectionPool> { + dotenv().ok(); + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let manager = ConnectionManager::<PgConnection>::new(database_url); + let pool = diesel::r2d2::Pool::builder() + .build(manager) + .expect("Failed to create database pool."); + Ok(pool) +} + fn run() -> Result<()> { let m = App::new("fatcat-auth") .version(env!("CARGO_PKG_VERSION")) @@ -67,35 +83,37 @@ fn run() -> Result<()> { ) .get_matches(); -/* - value_t_or_exit!(subm, "magic", u32) - .after_help("Reads a ident table TSV dump from stdin (aka, ident_id, rev_id, redirect_id), \ - and outputs JSON (one entity per line). Database connection info read from environment \ - (DATABASE_URL, same as fatcatd).") -*/ match m.subcommand() { ("list-editors", Some(_subm)) => { - fatcat::auth::print_editors()?; + let db_conn = database_worker_pool()?.get().expect("database pool"); + fatcat::auth::print_editors(&db_conn)?; }, ("create-editor", Some(subm)) => { - fatcat::auth::create_editor( + let db_conn = database_worker_pool()?.get().expect("database pool"); + let editor = fatcat::auth::create_editor( + &db_conn, subm.value_of("username").unwrap().to_string(), subm.is_present("admin"), subm.is_present("bot"))?; + //println!("{:?}", editor); + println!("{}", FatCatId::from_uuid(&editor.id).to_string()); }, ("create-token", Some(subm)) => { + let db_conn = database_worker_pool()?.get().expect("database pool"); let editor_id = FatCatId::from_str(subm.value_of("editor").unwrap())?; - fatcat::auth::create_token(editor_id, None)?; + fatcat::auth::create_token(&db_conn, editor_id, None)?; }, ("inspect-token", Some(subm)) => { fatcat::auth::inspect_token(subm.value_of("token").unwrap())?; }, ("revoke-tokens", Some(subm)) => { + let db_conn = database_worker_pool()?.get().expect("database pool"); let editor_id = FatCatId::from_str(subm.value_of("editor").unwrap())?; - fatcat::auth::revoke_tokens(editor_id)?; + fatcat::auth::revoke_tokens(&db_conn, editor_id)?; }, ("revoke-tokens-everyone", Some(_subm)) => { - fatcat::auth::revoke_tokens_everyone()?; + let db_conn = database_worker_pool()?.get().expect("database pool"); + fatcat::auth::revoke_tokens_everyone(&db_conn)?; }, _ => { println!("Missing or unimplemented command!"); |