mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-15 08:13:31 -06:00
Tera template rendering
This commit is contained in:
40
src/main.rs
40
src/main.rs
@@ -2,16 +2,31 @@ mod config;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{http::StatusCode, Json, response::IntoResponse, Router, routing::{get, post}};
|
||||
use axum::body::{Bytes, Full};
|
||||
use axum::{http::StatusCode, response::IntoResponse, Router, routing::{get}};
|
||||
use axum::body::{Full};
|
||||
use axum::extract::ConnectInfo;
|
||||
use axum::http::header;
|
||||
use axum::response::Response;
|
||||
use dotenvy::dotenv;
|
||||
use lazy_static::lazy_static;
|
||||
use config::Configuration;
|
||||
use tera::{Tera, Context};
|
||||
|
||||
mod svg;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TEMPLATES: Tera = {
|
||||
let mut _tera = match Tera::new("./src/templates/**/*.svg") {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Parsing error(s): {}", e);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
};
|
||||
_tera
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -41,10 +56,25 @@ async fn main() {
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
async fn root_handler(connect_info: ConnectInfo<SocketAddr>) -> impl IntoResponse {
|
||||
async fn root_handler(_connect_info: ConnectInfo<SocketAddr>) -> impl IntoResponse {
|
||||
let renderer = svg::Renderer::new();
|
||||
let data = include_bytes!("./templates/basic.svg");
|
||||
let raw_image = renderer.render(data);
|
||||
|
||||
TEMPLATES.get_template_names().into_iter().for_each(|x| println!("{}", x));
|
||||
|
||||
let mut context = Context::new();
|
||||
context.insert("text", &_connect_info.ip());
|
||||
let data = TEMPLATES.render("basic.svg", &context);
|
||||
|
||||
if data.is_err() {
|
||||
return Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Full::from(
|
||||
format!("Template Could Not Be Rendered :: {}", data.err().unwrap())
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let raw_image = renderer.render(data.unwrap().into_bytes());
|
||||
|
||||
if raw_image.is_err() {
|
||||
return Response::builder()
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use png::EncodingError;
|
||||
use resvg::{tiny_skia, usvg};
|
||||
use resvg::usvg::{fontdb, TreeParsing, TreeTextToPath};
|
||||
|
||||
@@ -32,10 +31,10 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self, svg_data: &[u8]) -> Result<Vec<u8>, RenderError> {
|
||||
pub fn render(&self, svg_data: Vec<u8>) -> Result<Vec<u8>, RenderError> {
|
||||
let tree = {
|
||||
let mut opt = usvg::Options::default();
|
||||
let mut tree_result = usvg::Tree::from_data(svg_data, &opt);
|
||||
let opt = usvg::Options::default();
|
||||
let mut tree_result = usvg::Tree::from_data(&*svg_data, &opt);
|
||||
if tree_result.is_err() { return Err(RenderError { message: Some("Failed to parse".to_string()) }); }
|
||||
|
||||
let tree = tree_result.as_mut().unwrap();
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<svg width="256" height="32" xmlns="http://www.w3.org/2000/svg" font-family="Arial" font-size="32">
|
||||
<text x="10" y="32">Simple rect</text>
|
||||
<svg width="256" height="32" xmlns="http://www.w3.org/2000/svg" font-family="Arial" font-size="28">
|
||||
<text x="10" y="28">{{ text }}</text>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 149 B After Width: | Height: | Size: 148 B |
Reference in New Issue
Block a user