From 0820a05293b9d09335e13054121028c9764285de Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Sun, 7 Apr 2024 13:35:59 -0400 Subject: Add main page redirect --- src/get.rs | 19 ++++++++++++------- src/main.rs | 13 ++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/get.rs b/src/get.rs index e5268ff..d96e05f 100644 --- a/src/get.rs +++ b/src/get.rs @@ -3,7 +3,7 @@ use std::net::SocketAddr; use axum::extract::{ConnectInfo, Path}; use axum::http::HeaderMap; use axum::http::StatusCode; -use axum::response::{Html, IntoResponse}; +use axum::response::{Html, IntoResponse, Redirect}; use axum::Extension; use info_utils::prelude::*; @@ -11,7 +11,11 @@ use info_utils::prelude::*; use crate::ServerState; use crate::UrlRow; -pub async fn index(Extension(state): Extension) -> Html { +pub async fn index(Extension(state): Extension) -> impl IntoResponse { + if let Some(redirect) = state.main_page_redirect { + return Redirect::temporary(redirect.as_str()).into_response(); + } + Html(format!( r#" @@ -27,6 +31,7 @@ pub async fn index(Extension(state): Extension) -> Html { "#, state.host, state.host )) + .into_response() } /// # Panics @@ -47,7 +52,7 @@ pub async fn id( let item: Result = sqlx::query_as("SELECT * FROM chela.urls WHERE id = $1") - .bind(use_id) + .bind(use_id.clone()) .fetch_one(&state.db_pool) .await; if let Ok(it) = item { @@ -74,11 +79,11 @@ pub async fn id( ) .into_response(); } - } else if let Err(err) = item { - warn!("{}", err); + } else { + warn!("'{}' not found.", use_id); return ( - StatusCode::INTERNAL_SERVER_ERROR, - Html(format!("
Internal error: {err}.
")), + StatusCode::NOT_FOUND, + Html("
Not found.
".to_string()), ) .into_response(); } diff --git a/src/main.rs b/src/main.rs index 22750a6..ac16eb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use std::net::SocketAddr; +use url::Url; + use axum::routing::{get, post}; use axum::Router; @@ -20,6 +22,7 @@ pub struct ServerState { pub db_pool: Pool, pub host: String, pub sqids: Sqids, + pub main_page_redirect: Option, } #[derive(Debug, Clone, sqlx::FromRow, PartialEq, Eq)] @@ -40,9 +43,7 @@ async fn main() -> eyre::Result<()> { color_eyre::install()?; let db_pool = init_db().await?; - let host = std::env::var("CHELA_HOST").unwrap_or("localhost".to_string()); - let sqids = Sqids::builder() .alphabet( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -51,10 +52,12 @@ async fn main() -> eyre::Result<()> { ) .blocklist(["create".to_string()].into()) .build()?; + let main_page_redirect = std::env::var("CHELA_MAIN_PAGE_REDIRECT").unwrap_or_default(); let server_state = ServerState { db_pool, host, sqids, + main_page_redirect: Url::parse(&main_page_redirect).ok(), }; let address = std::env::var("CHELA_LISTEN_ADDRESS").unwrap_or("0.0.0.0".to_string()); @@ -74,7 +77,11 @@ async fn main() -> eyre::Result<()> { async fn init_db() -> eyre::Result> { let db_pool = PgPoolOptions::new() .max_connections(15) - .connect(std::env::var("DATABASE_URL")?.as_str()) + .connect( + std::env::var("DATABASE_URL") + .expect("DATABASE_URL must be set") + .as_str(), + ) .await?; log!("Successfully connected to database"); -- cgit v1.2.3