This commit is contained in:
parent
f9f094b1bf
commit
b4c1e550d5
6 changed files with 418 additions and 20 deletions
56
thesandwich-backend/src/error.rs
Normal file
56
thesandwich-backend/src/error.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
//! # yaaxum-error
|
||||
//! Yet Another Axum Error Handler
|
||||
//!
|
||||
//! This crate uses `eyre` to capture the error,
|
||||
//! the error is then returned to the browser or
|
||||
//! whatever it is, it's then nicely formatted to
|
||||
//! a webpage using `ansi_to_html`
|
||||
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub struct Error(pub StatusCode, pub color_eyre::eyre::Report);
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.1.handler().display(self.1.as_ref(), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.1.handler().debug(self.1.as_ref(), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let ansi_string = format!("{:?}", self);
|
||||
let error = ansi_to_html::convert(&ansi_string).unwrap();
|
||||
|
||||
(
|
||||
self.0,
|
||||
Html(format!(
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf8\"></head><body><pre><code>{}</code></pre></body></html>",
|
||||
error
|
||||
)),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WithStatusCode<T> {
|
||||
fn with_status_code(self, code: StatusCode) -> Result<T>;
|
||||
}
|
||||
|
||||
impl<T> WithStatusCode<T> for std::result::Result<T, color_eyre::eyre::Report> {
|
||||
fn with_status_code(self, code: StatusCode) -> Result<T> {
|
||||
self.map_err(|e| Error(code, e))
|
||||
}
|
||||
}
|
|
@ -1,3 +1,67 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use axum::{Router, extract::Form, http::StatusCode, routing::post};
|
||||
use color_eyre::eyre::{self, Context};
|
||||
use dotenvy_macro::dotenv;
|
||||
use error::WithStatusCode;
|
||||
use serde::Deserialize;
|
||||
use serenity::all::{ExecuteWebhook, Http, Webhook};
|
||||
use tracing::instrument;
|
||||
use tracing_error::ErrorLayer;
|
||||
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod error;
|
||||
|
||||
const WEBHOOK: &'static str = dotenv!("WEBHOOK");
|
||||
const LISTEN_ADDR: &'static str = dotenv!("LISTEN_ADDR");
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(ErrorLayer::default())
|
||||
.with(
|
||||
EnvFilter::try_from_default_env()
|
||||
.or_else(|_| EnvFilter::try_new("info"))
|
||||
.unwrap(),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let app = Router::new().route("/api/speculate", post(speculate));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(LISTEN_ADDR)
|
||||
.await
|
||||
.wrap_err("Failed to listen on that port")?;
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.wrap_err("Failed to run axum service")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Speculation {
|
||||
speculation: String,
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn speculate(Form(speculation): Form<Speculation>) -> Result<maud::Markup, error::Error> {
|
||||
let discord_http = Http::new("");
|
||||
|
||||
let webhook = Webhook::from_url(&discord_http, WEBHOOK)
|
||||
.await
|
||||
.wrap_err("Failed to initialize webhook")
|
||||
.with_status_code(StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
let builder = ExecuteWebhook::new()
|
||||
.content(speculation.speculation)
|
||||
.username("Anonymous speculator");
|
||||
webhook
|
||||
.execute(&discord_http, false, builder)
|
||||
.await
|
||||
.wrap_err("Could not execute webhook")
|
||||
.with_status_code(StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
Ok(maud::html! {
|
||||
p { "Speculation launched at high speed directly into our DMs" }
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue