mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-06 01:16:36 -06:00
chore: reformatting files, remove parse module, move split_on_extension
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -1,19 +1,19 @@
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use axum::{Router, routing::get};
|
use crate::routes::{
|
||||||
use dotenvy::dotenv;
|
absolute_handler, fallback_handler, implicit_handler, index_handler, relative_handler,
|
||||||
|
};
|
||||||
|
use axum::{routing::get, Router};
|
||||||
use config::Configuration;
|
use config::Configuration;
|
||||||
use crate::routes::{relative_handler, implicit_handler, absolute_handler, index_handler, fallback_handler};
|
use dotenvy::dotenv;
|
||||||
|
|
||||||
mod config;
|
|
||||||
mod raster;
|
|
||||||
mod abbr;
|
mod abbr;
|
||||||
mod routes;
|
mod config;
|
||||||
mod parse;
|
|
||||||
mod template;
|
|
||||||
mod error;
|
mod error;
|
||||||
|
mod raster;
|
||||||
mod relative;
|
mod relative;
|
||||||
|
mod routes;
|
||||||
|
mod template;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|||||||
26
src/parse.rs
26
src/parse.rs
@@ -1,26 +0,0 @@
|
|||||||
use chrono::{DateTime, FixedOffset, Utc};
|
|
||||||
|
|
||||||
/// Split a path into a tuple of the preceding path and the extension.
|
|
||||||
/// Can handle paths with multiple dots (period characters).
|
|
||||||
/// Returns None if there is no extension.
|
|
||||||
/// Returns None if the preceding path is empty (for example, dotfiles like ".env").
|
|
||||||
pub fn split_on_extension(path: &str) -> Option<(&str, &str)> {
|
|
||||||
let split = path.rsplit_once('.');
|
|
||||||
if split.is_none() { return None; }
|
|
||||||
|
|
||||||
// Check that the file is not a dotfile (.env)
|
|
||||||
if split.unwrap().0.len() == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(split.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_absolute(raw: String) -> Result<(DateTime<Utc>, FixedOffset), String> {
|
|
||||||
let datetime_with_offset = DateTime::parse_from_rfc3339(&raw);
|
|
||||||
if datetime_with_offset.is_err() {
|
|
||||||
return Err("Failed to parse datetime".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((datetime_with_offset.unwrap().with_timezone(&Utc), *(datetime_with_offset.unwrap().offset())))
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,27 @@
|
|||||||
use axum::{http::StatusCode, response::IntoResponse};
|
|
||||||
use axum::body::{Bytes, Full};
|
|
||||||
use axum::extract::{Path};
|
|
||||||
use axum::http::{header};
|
|
||||||
use axum::response::{Redirect, Response};
|
|
||||||
use chrono::{DateTime, NaiveDateTime, Offset, Utc};
|
|
||||||
use crate::error::{get_error_response, TimeBannerError};
|
use crate::error::{get_error_response, TimeBannerError};
|
||||||
|
use axum::body::{Bytes, Full};
|
||||||
|
use axum::extract::Path;
|
||||||
|
use axum::http::header;
|
||||||
|
use axum::response::{Redirect, Response};
|
||||||
|
use axum::{http::StatusCode, response::IntoResponse};
|
||||||
|
use chrono::{DateTime, NaiveDateTime, Offset, Utc};
|
||||||
|
|
||||||
|
|
||||||
use crate::parse::split_on_extension;
|
|
||||||
use crate::raster::Rasterizer;
|
use crate::raster::Rasterizer;
|
||||||
use crate::template::{OutputForm, render_template, RenderContext};
|
use crate::template::{render_template, OutputForm, RenderContext};
|
||||||
|
|
||||||
|
fn split_on_extension(path: &str) -> Option<(&str, &str)> {
|
||||||
|
let split = path.rsplit_once('.');
|
||||||
|
if split.is_none() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the file is not a dotfile (.env)
|
||||||
|
if split.unwrap().0.len() == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(split.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_path(path: &str) -> (&str, &str) {
|
fn parse_path(path: &str) -> (&str, &str) {
|
||||||
split_on_extension(path)
|
split_on_extension(path)
|
||||||
@@ -25,12 +36,20 @@ fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), Time
|
|||||||
let renderer = Rasterizer::new();
|
let renderer = Rasterizer::new();
|
||||||
let raw_image = renderer.render(data.into_bytes());
|
let raw_image = renderer.render(data.into_bytes());
|
||||||
if raw_image.is_err() {
|
if raw_image.is_err() {
|
||||||
return Err(TimeBannerError::RasterizeError(raw_image.unwrap_err().message.unwrap_or("Unknown error".to_string())));
|
return Err(TimeBannerError::RasterizeError(
|
||||||
|
raw_image
|
||||||
|
.unwrap_err()
|
||||||
|
.message
|
||||||
|
.unwrap_or("Unknown error".to_string()),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(("image/x-png", Bytes::from(raw_image.unwrap())))
|
Ok(("image/x-png", Bytes::from(raw_image.unwrap())))
|
||||||
}
|
}
|
||||||
_ => Err(TimeBannerError::RasterizeError(format!("Unsupported extension: {}", extension)))
|
_ => Err(TimeBannerError::RasterizeError(format!(
|
||||||
|
"Unsupported extension: {}",
|
||||||
|
extension
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +70,6 @@ pub async fn absolute_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
let (raw_time, extension) = parse_path(path.as_str());
|
let (raw_time, extension) = parse_path(path.as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// basic handler that responds with a static string
|
// basic handler that responds with a static string
|
||||||
pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
||||||
// Get extension if available
|
// Get extension if available
|
||||||
@@ -60,13 +78,19 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
// Parse epoch
|
// Parse epoch
|
||||||
let parsed_epoch = raw_time.parse::<i64>();
|
let parsed_epoch = raw_time.parse::<i64>();
|
||||||
if parsed_epoch.is_err() {
|
if parsed_epoch.is_err() {
|
||||||
return get_error_response(TimeBannerError::ParseError("Input could not be parsed into integer.".to_string())).into_response();
|
return get_error_response(TimeBannerError::ParseError(
|
||||||
|
"Input could not be parsed into integer.".to_string(),
|
||||||
|
))
|
||||||
|
.into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert epoch to DateTime
|
// Convert epoch to DateTime
|
||||||
let naive_time = NaiveDateTime::from_timestamp_opt(parsed_epoch.unwrap(), 0);
|
let naive_time = NaiveDateTime::from_timestamp_opt(parsed_epoch.unwrap(), 0);
|
||||||
if naive_time.is_none() {
|
if naive_time.is_none() {
|
||||||
return get_error_response(TimeBannerError::ParseError("Input was not a valid DateTime".to_string())).into_response();
|
return get_error_response(TimeBannerError::ParseError(
|
||||||
|
"Input was not a valid DateTime".to_string(),
|
||||||
|
))
|
||||||
|
.into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
let utc_time = DateTime::<Utc>::from_utc(naive_time.unwrap(), Utc);
|
let utc_time = DateTime::<Utc>::from_utc(naive_time.unwrap(), Utc);
|
||||||
@@ -85,10 +109,12 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
if rendered_template.is_err() {
|
if rendered_template.is_err() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
.body(Full::from(
|
.body(Full::from(format!(
|
||||||
format!("Template Could Not Be Rendered :: {}", rendered_template.err().unwrap())
|
"Template Could Not Be Rendered :: {}",
|
||||||
))
|
rendered_template.err().unwrap()
|
||||||
.unwrap().into_response();
|
)))
|
||||||
|
.unwrap()
|
||||||
|
.into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
||||||
@@ -96,6 +122,6 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
Ok((mime_type, bytes)) => {
|
Ok((mime_type, bytes)) => {
|
||||||
(StatusCode::OK, [(header::CONTENT_TYPE, mime_type)], bytes).into_response()
|
(StatusCode::OK, [(header::CONTENT_TYPE, mime_type)], bytes).into_response()
|
||||||
}
|
}
|
||||||
Err(e) => get_error_response(e).into_response()
|
Err(e) => get_error_response(e).into_response(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user