diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-08-21 18:48:51 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-08-21 18:48:51 +0200 |
commit | 451d1db821b8d445f1bb6f1f3ad6de20f19eb85d (patch) | |
tree | 10316b0c057bd54751a461fc144fd40a80f14a84 /src | |
parent | 0b9e7dff080b3fcf3907da343e17ab73012e7207 (diff) |
Move backend to server folder
Diffstat (limited to 'src')
-rw-r--r-- | src/errors.rs | 24 | ||||
-rw-r--r-- | src/logger.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 66 |
3 files changed, 0 insertions, 101 deletions
diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index 6fd0a61..0000000 --- a/src/errors.rs +++ /dev/null @@ -1,24 +0,0 @@ -use axum::{ - http::StatusCode, - response::{IntoResponse, Response}, - Json, -}; -use serde_json::json; - -pub enum Error { - Generic, -} - -impl IntoResponse for Error { - fn into_response(self) -> Response { - let (status, error_message) = match self { - Error::Generic => (StatusCode::INTERNAL_SERVER_ERROR, "User not found"), - }; - - let body = Json(json!({ - "error": error_message, - })); - - (status, body).into_response() - } -} diff --git a/src/logger.rs b/src/logger.rs deleted file mode 100644 index fa569ee..0000000 --- a/src/logger.rs +++ /dev/null @@ -1,11 +0,0 @@ -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; - -pub fn setup() { - tracing_subscriber::registry() - .with(tracing_subscriber::EnvFilter::new( - std::env::var("RUST_LOG") - .unwrap_or_else(|_| "m6_ie_2022=debug,tower_http=debug".into()), - )) - .with(tracing_subscriber::fmt::layer()) - .init(); -} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 810e202..0000000 --- a/src/main.rs +++ /dev/null @@ -1,66 +0,0 @@ -mod errors; -mod logger; - -use axum::{ - http::{header, Request}, - routing::get, - Json, Router, -}; -use serde::Serialize; -use std::time::Duration; -use tower_http::sensitive_headers::SetSensitiveHeadersLayer; -use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer}; -use tracing::Span; - -#[tokio::main] -async fn main() { - let app = create_app().await; - - let addr: String = - std::env::var("ALLOWED_HOST").unwrap_or_else(|_| "localhost:3000".to_string()); - tracing::info!("Listening on {}", addr); - - axum::Server::bind(&"127.0.0.1:3000".parse().unwrap()) - .serve(app.into_make_service()) - .await - .unwrap(); -} - -async fn create_app() -> Router { - logger::setup(); - - Router::new() - .route("/", get(hej)) - // Mark the `Authorization` request header as sensitive so it doesn't - // show in logs. - .layer(SetSensitiveHeadersLayer::new(std::iter::once( - header::AUTHORIZATION, - ))) - .layer( - TraceLayer::new_for_http() - .on_request(|request: &Request<_>, _span: &Span| { - tracing::info!("{} {}", request.method(), request.uri()); - }) - .on_failure( - |error: ServerErrorsFailureClass, latency: Duration, _span: &Span| { - tracing::error!("{} | {} s", error, latency.as_secs()); - }, - ), - ) -} - -// Example root which says hi -async fn hej() -> Result<Json<Hej>, errors::Error> { - Ok(Json(Hej::new("hej verden".to_string()))) -} - -#[derive(Debug, Serialize)] -struct Hej { - hello: String, -} - -impl Hej { - fn new(hello: String) -> Self { - Self { hello } - } -} |