From 5c8f6c1ba933663cf5fcb93d941bbae0c3a40b5b Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 30 Oct 2020 11:01:36 -0400 Subject: [PATCH] simplify logging, create handlers --- Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/api/handler.rs | 45 ++++++++++++++++++++++++++++++++++++--------- src/api/token.rs | 8 ++++---- src/api/user.rs | 4 ++-- src/gitea.rs | 2 +- src/main.rs | 3 ++- src/models.rs | 11 +++++++++-- 8 files changed, 97 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bed4760..8c434c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,6 +461,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "elfs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b667de562ec5e68bc9cb569b1e0c8a9af996637d30138c81e616339f504920f" +dependencies = [ + "names", +] + [[package]] name = "encoding" version = "0.2.33" @@ -1012,6 +1021,15 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "names" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef320dab323286b50fb5cdda23f61c796a72a89998ab565ca32525c5c556f2da" +dependencies = [ + "rand 0.3.23", +] + [[package]] name = "net2" version = "0.2.35" @@ -1266,6 +1284,29 @@ dependencies = [ "scheduled-thread-pool", ] +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi 0.3.9", +] + [[package]] name = "rand" version = "0.6.5" @@ -2270,6 +2311,7 @@ dependencies = [ "chrono", "color-eyre", "diesel", + "elfs", "hmac 0.9.0", "jwt", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 67777fc..19cf272 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" chrono = { version = "0.4", features = ["serde"] } color-eyre = "0.5" diesel = { version = "1", features = ["postgres", "r2d2", "uuidv07", "chrono"] } +elfs = "0" lazy_static = "1.4" jwt = "0.11" hmac = "0.9" diff --git a/src/api/handler.rs b/src/api/handler.rs index 8cc6c98..9501987 100644 --- a/src/api/handler.rs +++ b/src/api/handler.rs @@ -1,10 +1,41 @@ -use crate::{models, schema, MainDatabase}; use super::{Error, Result}; +use crate::{models, schema, MainDatabase}; use chrono::prelude::*; use diesel::prelude::*; use rocket_contrib::{json::Json, uuid::Uuid}; +use serde::Deserialize; -#[instrument(skip(conn))] +#[derive(Debug, Eq, PartialEq, Deserialize)] +pub struct New { + pub name: Option, + pub async_impl: bool, +} + +#[instrument(skip(conn), err)] +#[post("/handler", format = "json", data = "")] +pub fn create( + user: models::User, + input: Json, + conn: MainDatabase, +) -> Result> { + let input = input.into_inner(); + let name = input.name.unwrap_or(elfs::next().to_lowercase()); + let hdl = diesel::insert_into(schema::handlers::table) + .values(&models::NewHandler { + user_id: user.id.clone(), + human_name: name, + current_version: None, + async_impl: input.async_impl, + }) + .get_result::(&*conn) + .map_err(Error::Database)?; + + info!("created handler {} with id {}", hdl.human_name, hdl.id); + + Ok(Json(hdl)) +} + +#[instrument(skip(conn), err)] #[get("/handler")] pub fn list(user: models::User, conn: MainDatabase) -> Result>> { use schema::handlers::dsl::*; @@ -17,13 +48,9 @@ pub fn list(user: models::User, conn: MainDatabase) -> Result")] -pub fn get( - user: models::User, - uuid: Uuid, - conn: MainDatabase, -) -> Result> { +pub fn get(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result> { use schema::handlers::dsl::*; let uuid = uuid.into_inner(); let handler = handlers @@ -38,7 +65,7 @@ pub fn get( } } -#[instrument(skip(conn))] +#[instrument(skip(conn), err)] #[delete("/handler/")] pub fn delete(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result { use schema::handlers::dsl::*; diff --git a/src/api/token.rs b/src/api/token.rs index 38b6694..83e4ac6 100644 --- a/src/api/token.rs +++ b/src/api/token.rs @@ -1,10 +1,10 @@ -use crate::{jwt, models, schema, MainDatabase}; use super::{Error, Result}; +use crate::{jwt, models, schema, MainDatabase}; use chrono::prelude::*; use diesel::prelude::*; use rocket_contrib::{json::Json, uuid::Uuid}; -#[instrument(skip(conn))] +#[instrument(skip(conn), err)] #[get("/token")] pub fn list(user: models::User, conn: MainDatabase) -> Result>> { use schema::tokens::dsl::*; @@ -17,7 +17,7 @@ pub fn list(user: models::User, conn: MainDatabase) -> Result")] pub fn delete(user: models::User, conn: MainDatabase, uuid: Uuid) -> Result { use schema::tokens::dsl::*; @@ -39,7 +39,7 @@ pub fn delete(user: models::User, conn: MainDatabase, uuid: Uuid) -> Result { Ok(()) } -#[instrument(skip(conn))] +#[instrument(skip(conn), err)] #[post("/token")] pub fn create(user: models::User, conn: MainDatabase) -> Result { use schema::tokens; diff --git a/src/api/user.rs b/src/api/user.rs index d40ed5e..ac03a6d 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -1,8 +1,8 @@ -use crate::models; use super::{Error, Result}; +use crate::models; use rocket_contrib::{json::Json, uuid::Uuid}; -#[instrument] +#[instrument(err)] #[get("/user/")] pub fn get(user: models::User, uuid: Uuid) -> Result> { if uuid != user.id { diff --git a/src/gitea.rs b/src/gitea.rs index fcdd4ea..501c87f 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -39,7 +39,7 @@ pub fn login(oauth2: OAuth2, mut cookies: Cookies<'_>) -> Redirect { oauth2.get_redirect(&mut cookies, &[""]).unwrap() } -#[instrument(skip(conn, token, cookies))] +#[instrument(skip(conn, token, cookies), err)] #[get("/auth/gitea")] pub fn callback( conn: MainDatabase, diff --git a/src/main.rs b/src/main.rs index 8eb3608..3868979 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ extern crate tracing; use color_eyre::eyre::Result; use diesel::pg::PgConnection; use rocket_contrib::helmet::SpaceHelmet; -use rocket_oauth2::{OAuth2}; +use rocket_oauth2::OAuth2; pub mod api; pub mod gitea; @@ -40,6 +40,7 @@ fn main() -> Result<()> { .mount( "/api", routes![ + api::handler::create, api::handler::list, api::handler::get, api::handler::delete, diff --git a/src/models.rs b/src/models.rs index da28910..ea1081f 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,6 +1,7 @@ -use crate::schema::{gitea_tokens, tokens, users, handlers}; +use crate::schema::{gitea_tokens, handlers, tokens, users}; use chrono::NaiveDateTime; use serde::Serialize; +use std::fmt; use uuid::Uuid; #[derive(Insertable)] @@ -13,7 +14,7 @@ pub struct NewUser { pub tier: i32, } -#[derive(Queryable, Serialize, Debug, Clone)] +#[derive(Queryable, Serialize, Clone)] pub struct User { pub id: Uuid, pub email: String, @@ -25,6 +26,12 @@ pub struct User { pub updated_at: NaiveDateTime, } +impl fmt::Debug for User { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.id) + } +} + #[derive(Insertable)] #[table_name = "gitea_tokens"] pub struct NewGiteaToken {