summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/errors.rs24
-rw-r--r--src/logger.rs11
-rw-r--r--src/main.rs48
3 files changed, 83 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs
new file mode 100644
index 0000000..6fd0a61
--- /dev/null
+++ b/src/errors.rs
@@ -0,0 +1,24 @@
+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
new file mode 100644
index 0000000..fa569ee
--- /dev/null
+++ b/src/logger.rs
@@ -0,0 +1,11 @@
+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
new file mode 100644
index 0000000..ab463b8
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,48 @@
+mod errors;
+mod logger;
+
+use axum::{http::header, routing::get, Json, Router};
+use serde::Serialize;
+use tower_http::sensitive_headers::SetSensitiveHeadersLayer;
+
+#[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,
+ )))
+}
+
+// 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 }
+ }
+}