simplify logging, create handlers
This commit is contained in:
parent
c99bc1119f
commit
5c8f6c1ba9
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<String>,
|
||||
pub async_impl: bool,
|
||||
}
|
||||
|
||||
#[instrument(skip(conn), err)]
|
||||
#[post("/handler", format = "json", data = "<input>")]
|
||||
pub fn create(
|
||||
user: models::User,
|
||||
input: Json<New>,
|
||||
conn: MainDatabase,
|
||||
) -> Result<Json<models::Handler>> {
|
||||
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::<models::Handler>(&*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<Json<Vec<models::Handler>>> {
|
||||
use schema::handlers::dsl::*;
|
||||
|
@ -17,13 +48,9 @@ pub fn list(user: models::User, conn: MainDatabase) -> Result<Json<Vec<models::H
|
|||
))
|
||||
}
|
||||
|
||||
#[instrument(skip(conn))]
|
||||
#[instrument(skip(conn), err)]
|
||||
#[get("/handler/<uuid>")]
|
||||
pub fn get(
|
||||
user: models::User,
|
||||
uuid: Uuid,
|
||||
conn: MainDatabase,
|
||||
) -> Result<Json<models::Handler>> {
|
||||
pub fn get(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result<Json<models::Handler>> {
|
||||
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/<uuid>")]
|
||||
pub fn delete(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result {
|
||||
use schema::handlers::dsl::*;
|
||||
|
|
|
@ -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<Json<Vec<models::Token>>> {
|
||||
use schema::tokens::dsl::*;
|
||||
|
@ -17,7 +17,7 @@ pub fn list(user: models::User, conn: MainDatabase) -> Result<Json<Vec<models::T
|
|||
))
|
||||
}
|
||||
|
||||
#[instrument(skip(conn))]
|
||||
#[instrument(skip(conn), err)]
|
||||
#[delete("/token/<uuid>")]
|
||||
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<String> {
|
||||
use schema::tokens;
|
||||
|
|
|
@ -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/<uuid>")]
|
||||
pub fn get(user: models::User, uuid: Uuid) -> Result<Json<models::User>> {
|
||||
if uuid != user.id {
|
||||
|
|
|
@ -39,7 +39,7 @@ pub fn login(oauth2: OAuth2<Gitea>, 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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue