summaryrefslogtreecommitdiffstats
path: root/rust/src/bin
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2019-01-08 23:18:32 -0800
committerBryan Newbold <bnewbold@robocracy.org>2019-01-08 23:20:53 -0800
commitba7d6a842cb4d61357b588fb2d3ec552c654ae64 (patch)
tree41eb8c465cd1ad0a9f70e18f49905adf7e5c3b40 /rust/src/bin
parenteb6fb8e5fe1efb3fbb927d13075cf5a1b33aa83e (diff)
downloadfatcat-ba7d6a842cb4d61357b588fb2d3ec552c654ae64.tar.gz
fatcat-ba7d6a842cb4d61357b588fb2d3ec552c654ae64.zip
huge refactor of rust modules/files
Taking advantage of new Rust 2018 crate/module path changes, and re-organizing things. Somewhat optimistic this could help with partial rebuild speed also.
Diffstat (limited to 'rust/src/bin')
-rw-r--r--rust/src/bin/fatcat-auth.rs44
-rw-r--r--rust/src/bin/fatcat-export.rs20
-rw-r--r--rust/src/bin/fatcatd.rs40
3 files changed, 38 insertions, 66 deletions
diff --git a/rust/src/bin/fatcat-auth.rs b/rust/src/bin/fatcat-auth.rs
index addd2b66..7e2a7c39 100644
--- a/rust/src/bin/fatcat-auth.rs
+++ b/rust/src/bin/fatcat-auth.rs
@@ -1,32 +1,16 @@
//! JSON Export Helper
-//#[macro_use]
-extern crate clap;
-extern crate diesel;
-extern crate dotenv;
-#[macro_use]
-extern crate error_chain;
-extern crate fatcat;
-//#[macro_use]
-extern crate env_logger;
-extern crate log;
-extern crate serde_json;
-extern crate uuid;
-
use clap::{App, SubCommand};
-use diesel::prelude::*;
-use fatcat::api_helpers::FatCatId;
+use fatcat::auth;
+use fatcat::editing;
use fatcat::errors::*;
+use fatcat::identifiers::FatCatId;
+use fatcat::server::*;
+use std::process;
use std::str::FromStr;
-//use uuid::Uuid;
-
-//use error_chain::ChainedError;
-//use std::io::{Stdout,StdoutLock};
-//use std::io::prelude::*;
-//use std::io::{BufReader, BufWriter};
-fn run() -> Result<()> {
+fn main() -> Result<()> {
let m = App::new("fatcat-auth")
.version(env!("CARGO_PKG_VERSION"))
.author("Bryan Newbold <bnewbold@archive.org>")
@@ -84,16 +68,14 @@ fn run() -> Result<()> {
}
// Then the ones that do
- let db_conn = fatcat::database_worker_pool()?
- .get()
- .expect("database pool");
- let confectionary = fatcat::env_confectionary()?;
+ let db_conn = database_worker_pool()?.get().expect("database pool");
+ let confectionary = auth::env_confectionary()?;
match m.subcommand() {
("list-editors", Some(_subm)) => {
fatcat::auth::print_editors(&db_conn)?;
}
("create-editor", Some(subm)) => {
- let editor = fatcat::api_helpers::create_editor(
+ let editor = editing::create_editor(
&db_conn,
subm.value_of("username").unwrap().to_string(),
subm.is_present("admin"),
@@ -104,10 +86,6 @@ fn run() -> Result<()> {
}
("create-token", Some(subm)) => {
let editor_id = FatCatId::from_str(subm.value_of("editor-id").unwrap())?;
- // check that editor exists
- let _ed: fatcat::database_models::EditorRow = fatcat::database_schema::editor::table
- .find(&editor_id.to_uuid())
- .get_result(&db_conn)?;
println!("{}", confectionary.create_token(editor_id, None)?);
}
("inspect-token", Some(subm)) => {
@@ -125,10 +103,8 @@ fn run() -> Result<()> {
_ => {
println!("Missing or unimplemented command!");
println!("{}", m.usage());
- ::std::process::exit(-1);
+ process::exit(-1);
}
}
Ok(())
}
-
-quick_main!(run);
diff --git a/rust/src/bin/fatcat-export.rs b/rust/src/bin/fatcat-export.rs
index e1b930fc..889d7dff 100644
--- a/rust/src/bin/fatcat-export.rs
+++ b/rust/src/bin/fatcat-export.rs
@@ -2,25 +2,17 @@
#[macro_use]
extern crate clap;
-extern crate diesel;
-extern crate dotenv;
#[macro_use]
extern crate error_chain;
-extern crate fatcat;
-extern crate fatcat_api_spec;
#[macro_use]
extern crate log;
-extern crate crossbeam_channel;
-extern crate env_logger;
-extern crate num_cpus;
-extern crate serde_json;
-extern crate uuid;
use clap::{App, Arg};
-use fatcat::api_entity_crud::*;
-use fatcat::api_helpers::*;
+use fatcat::entity_crud::*;
use fatcat::errors::*;
+use fatcat::identifiers::*;
+use fatcat::server::*;
use fatcat_api_spec::models::*;
use std::str::FromStr;
use uuid::Uuid;
@@ -167,7 +159,7 @@ pub fn do_export(
entity_type: ExportEntityType,
redirects: bool,
) -> Result<()> {
- let db_pool = fatcat::database_worker_pool()?;
+ let db_pool = database_worker_pool()?;
let buf_input = BufReader::new(std::io::stdin());
let (row_sender, row_receiver) = channel::bounded(CHANNEL_BUFFER_LEN);
let (output_sender, output_receiver) = channel::bounded(CHANNEL_BUFFER_LEN);
@@ -232,7 +224,7 @@ pub fn do_export(
Ok(())
}
-fn run() -> Result<()> {
+fn main() -> Result<()> {
let m = App::new("fatcat-export")
.version(env!("CARGO_PKG_VERSION"))
.author("Bryan Newbold <bnewbold@archive.org>")
@@ -273,5 +265,3 @@ fn run() -> Result<()> {
m.is_present("include_redirects"),
)
}
-
-quick_main!(run);
diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs
index 682f5038..34652105 100644
--- a/rust/src/bin/fatcatd.rs
+++ b/rust/src/bin/fatcatd.rs
@@ -1,29 +1,35 @@
#![allow(missing_docs)]
-extern crate chrono;
-extern crate clap;
-extern crate diesel;
-//extern crate dotenv;
-extern crate error_chain;
-extern crate fatcat;
-extern crate fatcat_api_spec;
-extern crate futures;
-extern crate iron;
-extern crate iron_slog;
#[macro_use]
extern crate slog;
-extern crate slog_async;
-extern crate slog_term;
+#[macro_use]
+extern crate hyper;
use clap::{App, Arg};
+use fatcat::errors::*;
+use fatcat::server::*;
+use iron::middleware::AfterMiddleware;
use iron::modifiers::RedirectRaw;
use iron::{status, Chain, Iron, IronResult, Request, Response};
use iron_slog::{DefaultLogFormatter, LoggerMiddleware};
use slog::{Drain, Logger};
+// HTTP header middleware
+header! { (XClacksOverhead, "X-Clacks-Overhead") => [String] }
+
+pub struct XClacksOverheadMiddleware;
+
+impl AfterMiddleware for XClacksOverheadMiddleware {
+ fn after(&self, _req: &mut Request, mut res: Response) -> iron::IronResult<Response> {
+ res.headers
+ .set(XClacksOverhead("GNU aaronsw, jpb".to_owned()));
+ Ok(res)
+ }
+}
+
/// Create custom server, wire it to the autogenerated router,
/// and pass it to the web server.
-fn main() {
+fn main() -> Result<()> {
let matches = App::new("server")
.arg(
Arg::with_name("https")
@@ -38,7 +44,7 @@ fn main() {
let logger = Logger::root(drain, o!());
let formatter = DefaultLogFormatter;
- let server = fatcat::server().unwrap();
+ let server = create_server()?;
info!(
logger,
"using primary auth key: {}", server.auth_confectionary.identifier,
@@ -59,7 +65,6 @@ fn main() {
router.get("/v0/openapi2.yml", yaml_handler, "openapi2-spec-yaml");
fn root_handler(_: &mut Request) -> IronResult<Response> {
- //Ok(Response::with((status::Found, Redirect(Url::parse("/swagger-ui").unwrap()))))
Ok(Response::with((
status::Found,
RedirectRaw("/swagger-ui".to_string()),
@@ -92,7 +97,7 @@ fn main() {
chain.link_before(fatcat_api_spec::server::ExtractAuthData);
chain.link_before(fatcat::auth::MacaroonAuthMiddleware::new());
- chain.link_after(fatcat::XClacksOverheadMiddleware);
+ chain.link_after(XClacksOverheadMiddleware);
if matches.is_present("https") {
unimplemented!()
@@ -100,6 +105,7 @@ fn main() {
// Using HTTP
Iron::new(chain)
.http(host_port)
- .expect("Failed to start HTTP server");
+ .expect("failed to start HTTP server");
}
+ Ok(())
}