Skip to content

Commit

Permalink
chore: abstract routes into separate scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Jun 29, 2023
1 parent 86a9c15 commit 500639b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
27 changes: 27 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use axum::{
body::Body,
http::header::{CONTENT_DISPOSITION, CONTENT_TYPE},
http::StatusCode,
response::{IntoResponse, Response},
Json,
};

use serde::Deserialize;
use tectonic::latex_to_pdf;

#[derive(Deserialize)]
pub struct CompileSchema {
latex: String,
}

pub async fn compile(Json(payload): Json<CompileSchema>) -> impl IntoResponse {
let pdf = latex_to_pdf(payload.latex).expect("Unable to convert LaTeX to PDF");
let body = Body::from(pdf);

Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, "application/pdf")
.header(CONTENT_DISPOSITION, "attachment; filename=\"compiled.pdf\"")
.body(body)
.unwrap()
}
3 changes: 3 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub async fn index() -> &'static str {
"Welcome to v1 of the API!"
}
38 changes: 8 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
mod compile;
mod index;

use axum::{
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
Json, Router, Server,
Router, Server,
};
use serde::Deserialize;

use compile::compile;
use index::index;
use std::env::var;
use tectonic::latex_to_pdf;
use tracing_subscriber::fmt;

#[derive(Deserialize)]
struct CompileSchema {
latex: String,
}

async fn root() -> &'static str {
"Welcome to v1 of the API!"
}

async fn compile(Json(payload): Json<CompileSchema>) -> impl IntoResponse {
let pdf = latex_to_pdf(payload.latex).expect("Unable to convert LaTeX to PDF");
let body = axum::body::Body::from(pdf);

Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/pdf")
.header(
header::CONTENT_DISPOSITION,
"attachment; filename=\"compiled.pdf\"",
)
.body(body)
.unwrap()
}

#[tokio::main]
async fn main() {
fmt::init();

let app = Router::new()
.route("/", get(root))
.route("/", get(index))
.route("/compile", post(compile));

let port = var("SERVER_PORT").unwrap();
Expand Down

0 comments on commit 500639b

Please sign in to comment.