Skip to content

Commit

Permalink
feat: fallback 404 route
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Sørensen <[email protected]>
  • Loading branch information
cafkafk committed Apr 29, 2024
1 parent c921622 commit 678f2c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/api/ha_registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

pub mod v1;
pub mod routes;

mod v1;
12 changes: 12 additions & 0 deletions src/api/ha_registry/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024 Christina Sørensen
// SPDX-FileContributor: Christina Sørensen
//
// SPDX-License-Identifier: AGPL-3.0-only

use axum::{extract::Extension, routing::get, Router};

use super::v1::routes::get_routes as get_v1_routes;

pub fn get_routes() -> Router {
Router::new().nest("/v1", get_v1_routes())
}
2 changes: 1 addition & 1 deletion src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

use super::ha_registry::v1::routes::get_routes as get_ha_routes;
use super::ha_registry::routes::get_routes as get_ha_routes;
use axum::Router;

pub fn get_routes() -> Router {
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#![deny(clippy::unwrap_used)]

use axum::{extract::Extension, response::Redirect, routing::get, Router};
use axum::{http::StatusCode, extract::Extension, response::{Redirect, IntoResponse}, routing::get, Router};
use tokio::net::TcpListener;

extern crate log;
Expand All @@ -20,6 +20,10 @@ use api::routes::get_routes as get_api_routes;
#[allow(unused)]
use log::{debug, error, info, trace, warn};

async fn handler_404() -> impl IntoResponse {
(StatusCode::NOT_FOUND, "404 - not found")
}

#[tokio::main]
async fn main() {
pretty_env_logger::init();
Expand All @@ -42,6 +46,7 @@ async fn main() {
get(|| async { Redirect::to("https://github.com/cafkafk/ha-registry") }),
)
.merge(get_api_routes())
.fallback(handler_404)
.layer(Extension(config.clone()));

let listener = TcpListener::bind(&config.bind_addr())
Expand Down

0 comments on commit 678f2c7

Please sign in to comment.