new handler to allow for exclusions from ServeDir for scripting languages interpretation (lua)
This commit is contained in:
38
src/main.rs
38
src/main.rs
@@ -5,7 +5,13 @@ use std::{collections::HashMap, fs, io::ErrorKind, path::PathBuf, str::FromStr};
|
||||
use icon::extract_icon;
|
||||
use once_cell::sync::{Lazy, OnceCell};
|
||||
|
||||
use axum::{extract::Request, http::StatusCode, response::Html, routing::get, Router};
|
||||
use axum::{
|
||||
extract::Request,
|
||||
handler::HandlerWithoutStateExt,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
};
|
||||
use base64::prelude::*;
|
||||
use serde::Serialize;
|
||||
use tera::{to_value, Context, Result as TeraResult, Tera, Value};
|
||||
@@ -162,15 +168,32 @@ struct FileInfo {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> AResult<()> {
|
||||
let listener = tokio::net::TcpListener::bind("[::]:3000").await.unwrap();
|
||||
Ok(axum::serve(listener, file_handler.into_make_service()).await?)
|
||||
}
|
||||
|
||||
async fn file_handler(request: Request) -> impl IntoResponse {
|
||||
let base_dir = BASE_DIR.get_or_init(|| {
|
||||
PathBuf::from_str(&std::env::var("BASE_DIR").unwrap_or(String::from("./public"))).unwrap()
|
||||
});
|
||||
|
||||
// let app = Router::new().fallback_service(get(handler));
|
||||
let app = Router::new().fallback_service(ServeDir::new(base_dir).fallback(get(handler)));
|
||||
dbg!(&request);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("[::]:3000").await.unwrap();
|
||||
Ok(axum::serve(listener, app).await?)
|
||||
if !request.uri().path().ends_with(".lua") {
|
||||
return ServeDir::new(base_dir)
|
||||
.fallback(get(handler))
|
||||
.try_call(request)
|
||||
.await
|
||||
.map(|e| e.into_response())
|
||||
.map_err(|_| StatusCode::NOT_FOUND);
|
||||
};
|
||||
|
||||
// TODO: process file as lua, where everything "printed" and or "returned" from the file is presented on screen
|
||||
// OR, prints go to console, return from main script goes to screen
|
||||
// Also need to be able to set content-type etc from within lua
|
||||
|
||||
// Ok(Html("Rawr").into_response())
|
||||
Err(StatusCode::NOT_IMPLEMENTED)
|
||||
}
|
||||
|
||||
async fn handler(request: Request) -> Result<Html<String>, StatusCode> {
|
||||
@@ -186,11 +209,12 @@ async fn handler(request: Request) -> Result<Html<String>, StatusCode> {
|
||||
path.push(&*uri);
|
||||
let full_path = fs::canonicalize(&path).map_err(|e| {
|
||||
println!("{:?}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
StatusCode::NOT_FOUND
|
||||
})?;
|
||||
|
||||
let full_base_path = fs::canonicalize(base_dir).map_err(|e| {
|
||||
println!("{:?}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
StatusCode::NOT_FOUND
|
||||
})?;
|
||||
|
||||
if !full_path.starts_with(full_base_path) {
|
||||
|
Reference in New Issue
Block a user