From 9a83067d04c9648416b92fc8d7b8a542b0b9aa96 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 10 Jan 2019 21:39:25 -0800 Subject: code docs/comments and rustfmt --- rust/src/auth.rs | 4 ++++ rust/src/bin/fatcat-auth.rs | 6 ++++-- rust/src/bin/fatcat-export.rs | 4 ++-- rust/src/bin/fatcatd.rs | 12 +++++++----- rust/src/editing.rs | 9 +++++++-- rust/src/endpoint_handlers.rs | 3 +++ rust/src/endpoints.rs | 4 ++-- rust/src/entity_crud.rs | 14 +++++++++++--- rust/src/errors.rs | 11 +++++++---- rust/src/server.rs | 4 ++-- 10 files changed, 49 insertions(+), 22 deletions(-) (limited to 'rust/src') diff --git a/rust/src/auth.rs b/rust/src/auth.rs index e7526389..8e7160bb 100644 --- a/rust/src/auth.rs +++ b/rust/src/auth.rs @@ -1,4 +1,8 @@ //! Editor bearer token authentication +//! +//! Code for dealing with API bearer tokens (macaroons), which encode the editor identifier as well +//! as additional constraints (eg, to a particular endpoint or time span). Includes helpers for +//! role-based authentication (RBAC). use data_encoding::BASE64; use macaroon::{Format, Macaroon, Verifier}; diff --git a/rust/src/bin/fatcat-auth.rs b/rust/src/bin/fatcat-auth.rs index 85132023..0ba543b4 100644 --- a/rust/src/bin/fatcat-auth.rs +++ b/rust/src/bin/fatcat-auth.rs @@ -2,9 +2,9 @@ use clap::{App, SubCommand}; -use fatcat::{auth, editing, server}; use fatcat::errors::Result; use fatcat::identifiers::FatcatId; +use fatcat::{auth, editing, server}; use std::process; use std::str::FromStr; @@ -66,7 +66,9 @@ fn main() -> Result<()> { } // Then the ones that do - let db_conn = server::database_worker_pool()?.get().expect("database pool"); + let db_conn = server::database_worker_pool()? + .get() + .expect("database pool"); let confectionary = auth::env_confectionary()?; match m.subcommand() { ("list-editors", Some(_subm)) => { diff --git a/rust/src/bin/fatcat-export.rs b/rust/src/bin/fatcat-export.rs index d6764dc6..0f5d04ac 100644 --- a/rust/src/bin/fatcat-export.rs +++ b/rust/src/bin/fatcat-export.rs @@ -15,15 +15,15 @@ use clap::{App, Arg}; use fatcat::entity_crud::*; use fatcat::errors::Result; use fatcat::identifiers::FatcatId; -use fatcat::server::{DbConn, self}; +use fatcat::server::{self, DbConn}; use fatcat_api_spec::models::*; use std::str::FromStr; use uuid::Uuid; -use std::thread; use crossbeam_channel as channel; use std::io::prelude::*; use std::io::{BufReader, BufWriter}; +use std::thread; const CHANNEL_BUFFER_LEN: usize = 200; diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs index 816ea1f0..75a6f000 100644 --- a/rust/src/bin/fatcatd.rs +++ b/rust/src/bin/fatcatd.rs @@ -5,6 +5,8 @@ extern crate slog; #[macro_use] extern crate hyper; +use cadence::prelude::*; +use cadence::{BufferedUdpMetricSink, QueuingMetricSink, StatsdClient}; use clap::{App, Arg}; use fatcat::errors::Result; use fatcat::server; @@ -15,8 +17,6 @@ use iron_slog::{DefaultLogFormatter, LoggerMiddleware}; use sentry::integrations::panic; use slog::{Drain, Logger}; use std::env; -use cadence::{StatsdClient, QueuingMetricSink, BufferedUdpMetricSink}; -use cadence::prelude::*; use std::net::UdpSocket; // HTTP header middleware @@ -32,7 +32,6 @@ impl AfterMiddleware for XClacksOverheadMiddleware { } } - /// Create custom server, wire it to the autogenerated router, /// and pass it to the web server. fn main() -> Result<()> { @@ -70,7 +69,7 @@ fn main() -> Result<()> { match env::var("FATCAT_STATSD_HOST") { Err(_) => { info!(logger, "no metrics recipient configured"); - }, + } Ok(host) => { let port: u16 = match env::var("FATCAT_STATSD_PORT") { Err(_) => cadence::DEFAULT_PORT, @@ -80,7 +79,10 @@ fn main() -> Result<()> { socket.set_nonblocking(true).unwrap(); let udp_sink = BufferedUdpMetricSink::from((host.as_ref(), port), socket).unwrap(); let queuing_sink = QueuingMetricSink::from(udp_sink); - info!(logger, "sending statsd metrics via UDP to: {}:{}", host, port); + info!( + logger, + "sending statsd metrics via UDP to: {}:{}", host, port + ); server.metrics = StatsdClient::from_sink("fatcat.api", queuing_sink); //server.metrics = StatsdClient::from_udp_host("fatcat.api", (host.as_ref(), port))?; server.metrics.incr("restart").unwrap(); diff --git a/rust/src/editing.rs b/rust/src/editing.rs index 9dac30ac..4fca30d6 100644 --- a/rust/src/editing.rs +++ b/rust/src/editing.rs @@ -1,8 +1,13 @@ +//! Helpers and types for dealing with the edit lifecycle. +//! +//! Does not contain the core code for creating/updating/reading/deleting the Editor, Annotation, +//! and Changelog objects, which lives under `editing_crud`. + use crate::database_models::*; use crate::database_schema::*; use crate::entity_crud::EntityCrud; -use crate::errors::{Result, FatcatError}; -use crate::identifiers::{FatcatId, check_username}; +use crate::errors::{FatcatError, Result}; +use crate::identifiers::{check_username, FatcatId}; use crate::server::DbConn; use diesel; use diesel::prelude::*; diff --git a/rust/src/endpoint_handlers.rs b/rust/src/endpoint_handlers.rs index 78044054..01b5eb5c 100644 --- a/rust/src/endpoint_handlers.rs +++ b/rust/src/endpoint_handlers.rs @@ -1,6 +1,9 @@ //! API endpoint handlers //! //! This module contains actual implementations of endpoints with rust-style type signatures. +//! +//! The remaining functions here should probably be refactored away. The 'get_release_files' style +//! functions should go in entity_crud (or some new use crate::database_models::*; use crate::database_schema::*; diff --git a/rust/src/endpoints.rs b/rust/src/endpoints.rs index 8c7700f1..3a95f746 100644 --- a/rust/src/endpoints.rs +++ b/rust/src/endpoints.rs @@ -14,6 +14,7 @@ use crate::entity_crud::{EntityCrud, ExpandFlags, HideFlags}; use crate::errors::*; use crate::identifiers::FatcatId; use crate::server::*; +use cadence::prelude::*; use diesel::Connection; use fatcat_api_spec::models; use fatcat_api_spec::models::*; @@ -22,7 +23,6 @@ use futures::{self, Future}; use sentry::integrations::failure::capture_fail; use std::str::FromStr; use uuid::{self, Uuid}; -use cadence::prelude::*; // This makes response matching below *much* more terse use crate::errors::FatcatError::*; @@ -764,7 +764,7 @@ impl Api for Server { success: true, message: "horray!".to_string(), }) - }, + } Err(fe) => generic_auth_err_responses!(fe, AcceptEditgroupResponse), }; Box::new(futures::done(Ok(ret))) diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index a0101de8..3d9b5165 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -1,3 +1,10 @@ +//! Create/Update/Read/Delete methods for all entity types +//! +//! This module is very large (perhaps it should be split into per-entity files?), but it basically +//! comes down to implementing the EntityCrud trait for each of the entity types. *All* entity +//! reads and mutations should go through this file, which defines the mechanics of translation +//! between the SQL schema and API (swagger) JSON models. + use crate::database_models::*; use crate::database_schema::*; use crate::editing::EditContext; @@ -5,7 +12,6 @@ use crate::endpoint_handlers::get_release_files; use crate::errors::*; use crate::identifiers::*; use crate::server::DbConn; -use chrono; use diesel::prelude::*; use diesel::{self, insert_into}; use fatcat_api_spec::models::*; @@ -295,8 +301,10 @@ macro_rules! generic_db_get_rev { fn db_get_rev(conn: &DbConn, rev_id: Uuid, hide: HideFlags) -> Result { let rev = match $rev_table::table.find(rev_id).first(conn) { Ok(rev) => rev, - Err(diesel::result::Error::NotFound) => - Err(FatcatError::NotFound(stringify!($rev_table).to_string(), rev_id.to_string()))?, + Err(diesel::result::Error::NotFound) => Err(FatcatError::NotFound( + stringify!($rev_table).to_string(), + rev_id.to_string(), + ))?, Err(e) => Err(e)?, }; diff --git a/rust/src/errors.rs b/rust/src/errors.rs index 19ba2f82..ec2b949b 100644 --- a/rust/src/errors.rs +++ b/rust/src/errors.rs @@ -116,9 +116,13 @@ impl Into for FatcatError { impl From for FatcatError { fn from(inner: diesel::result::Error) -> FatcatError { match inner { - diesel::result::Error::NotFound => FatcatError::NotFound("unknown".to_string(), "N/A".to_string()), - diesel::result::Error::DatabaseError(_, _) => FatcatError::ConstraintViolation(inner.to_string()), - _ => FatcatError::InternalError(inner.to_string()) + diesel::result::Error::NotFound => { + FatcatError::NotFound("unknown".to_string(), "N/A".to_string()) + } + diesel::result::Error::DatabaseError(_, _) => { + FatcatError::ConstraintViolation(inner.to_string()) + } + _ => FatcatError::InternalError(inner.to_string()), } } } @@ -178,4 +182,3 @@ impl From for FatcatError { FatcatError::InternalError(error.to_string()) } } - diff --git a/rust/src/server.rs b/rust/src/server.rs index c7cfa363..42ae9a07 100644 --- a/rust/src/server.rs +++ b/rust/src/server.rs @@ -1,14 +1,14 @@ //! API endpoint handlers -use crate::auth::{AuthConfectionary, self}; +use crate::auth::{self, AuthConfectionary}; use crate::errors::Result; +use cadence::{NopMetricSink, StatsdClient}; use chrono; use diesel; use diesel::pg::PgConnection; use diesel::r2d2::ConnectionManager; use dotenv::dotenv; use std::env; -use cadence::{StatsdClient, NopMetricSink}; #[cfg(feature = "postgres")] embed_migrations!("../migrations/"); -- cgit v1.2.3