This commit is contained in:
2026-03-29 05:11:22 +03:00
parent 07869f03c2
commit 644595b6d3
15 changed files with 4374 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
use axum::{Router, routing::get, Json};
use serde::Serialize;
use std::sync::Arc;
use typst_kit::fonts::Fonts;
#[derive(Serialize)]
struct HealthResponse {
status: &'static str,
version: &'static str,
}
async fn health() -> Json<HealthResponse> {
Json(HealthResponse {
status: "ok",
version: env!("CARGO_PKG_VERSION"),
})
}
pub fn router() -> Router<Arc<Fonts>> {
Router::new().route("/api/health", get(health))
}

12
backend/src/routes/mod.rs Normal file
View File

@@ -0,0 +1,12 @@
mod health;
mod render;
use axum::Router;
use std::sync::Arc;
use typst_kit::fonts::Fonts;
pub fn router() -> Router<Arc<Fonts>> {
Router::new()
.merge(health::router())
.merge(render::router())
}

View File

@@ -0,0 +1,52 @@
use axum::{
Router,
extract::State,
http::{StatusCode, header},
response::IntoResponse,
routing::post,
Json,
};
use serde::Deserialize;
use std::sync::Arc;
use crate::models::Template;
use crate::typst_engine::compiler::compile_pdf;
use crate::typst_engine::template_to_typst::{self, RenderMode};
use typst_kit::fonts::Fonts;
#[derive(Deserialize)]
pub struct RenderRequest {
pub template: Template,
pub data: serde_json::Value,
}
/// POST /api/render — Template + Data → PDF
pub async fn render(
State(fonts): State<Arc<Fonts>>,
Json(payload): Json<RenderRequest>,
) -> impl IntoResponse {
// 1. Template JSON → Typst markup
let typst_markup = template_to_typst::template_to_typst(&payload.template, &payload.data, RenderMode::Pdf);
// 2. Base64 image'ları çıkar
let files = template_to_typst::extract_image_files(&payload.template);
// 3. Typst markup → PDF
match compile_pdf(typst_markup, &fonts, files) {
Ok(pdf_bytes) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "application/pdf")],
pdf_bytes,
)
.into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("PDF derleme hatasi: {}", err),
)
.into_response(),
}
}
pub fn router() -> Router<Arc<Fonts>> {
Router::new().route("/api/render", post(render))
}