diff --git a/Cargo.lock b/Cargo.lock index feb72e5..d2b731c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,7 +82,7 @@ checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "astra" -version = "0.3.0" +version = "0.3.1" dependencies = [ "axum", "chrono", diff --git a/Cargo.toml b/Cargo.toml index b1a8f3d..d38fb95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "astra" -version = "0.3.0" +version = "0.3.1" authors = ["Elham Aryanpur "] -description = "Experimental Lua 5.1 webserver framework written in Rust + Axum" +description = "Experimental LuaJIT webserver framework written in Rust + Axum" edition = "2021" license = "Apache-2.0" exclude = ["examples"] @@ -10,6 +10,11 @@ exclude = ["examples"] [features] default = ["sqlx"] sqlx = ["dep:sqlx"] +compression = [ + "dep:tower", + "tower-http/compression-full", + "tower-http/decompression-full", +] [dependencies] axum = { version = "0.7.9", features = ["macros"] } @@ -26,7 +31,7 @@ mlua = { version = "0.10.2", features = [ ] } serde = { version = "1.0.216", features = ["derive"] } serde_json = "1.0.133" -sqlx = { version = "0.8", features = [ +sqlx = { version = "0.8.2", features = [ "runtime-tokio", "tls-native-tls", "postgres", @@ -36,13 +41,8 @@ sqlx = { version = "0.8", features = [ ], optional = true } tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] } toml = "0.8.19" -tower = "0.5.2" -tower-http = { version = "0.6.2", features = [ - "fs", - "trace", - "compression-full", - "decompression-full", -] } +tower = { version = "0.5.2", optional = true } +tower-http = { version = "0.6.2", features = ["fs", "trace"] } tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } uuid = { version = "1.11.0", features = ["serde", "v4"] } diff --git a/lua/astra.lua b/lua/astra.lua index a0bf12c..81ebb04 100644 --- a/lua/astra.lua +++ b/lua/astra.lua @@ -11,38 +11,48 @@ _G.Astra = { port = 20001 } ---- ----Registers a GET request to the specified path with the provided callback function. ---@param path string The URL path for the request. ---@param callback fun(request: Request): any A function that will be called when the request is made. function Astra.get(path, callback) table.insert(Astra, { path = path, method = "get", func = callback }) end ---- ----Registers a POST request to the specified path with the provided callback function. ---@param path string The URL path for the request. ---@param callback fun(request: Request): any A function that will be called when the request is made. function Astra.post(path, callback) table.insert(Astra, { path = path, method = "post", func = callback }) end ---- ----Registers a PUT request to the specified path with the provided callback function. ---@param path string The URL path for the request. ---@param callback fun(request: Request): any A function that will be called when the request is made. function Astra.put(path, callback) table.insert(Astra, { path = path, method = "put", func = callback }) end ---- ----Registers a DELETE request to the specified path with the provided callback function. ---@param path string The URL path for the request. ---@param callback fun(request: Request): any A function that will be called when the request is made. function Astra.delete(path, callback) table.insert(Astra, { path = path, method = "delete", func = callback }) end +---@param path string The URL path for the request. +---@param callback fun(request: Request): any A function that will be called when the request is made. +function Astra.options(path, callback) + table.insert(Astra, { path = path, method = "options", func = callback }) +end + +---@param path string The URL path for the request. +---@param callback fun(request: Request): any A function that will be called when the request is made. +function Astra.patch(path, callback) + table.insert(Astra, { path = path, method = "patch", func = callback }) +end + +---@param path string The URL path for the request. +---@param callback fun(request: Request): any A function that will be called when the request is made. +function Astra.trace(path, callback) + table.insert(Astra, { path = path, method = "trace", func = callback }) +end + --- ---Registers a static folder to serve ---@param path string The URL path for the request. diff --git a/src/routes.rs b/src/routes.rs index 1d607fd..f90f71c 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -3,7 +3,7 @@ use axum::{ body::Body, http::Request, response::IntoResponse, - routing::{delete, get, post, put}, + routing::{delete, get, options, patch, post, put, trace}, Router, }; use mlua::LuaSerdeExt; @@ -15,6 +15,9 @@ pub enum Method { Post, Put, Delete, + Options, + Patch, + Trace, Static, } #[derive(Debug, Clone, mlua::FromLua, PartialEq)] @@ -75,23 +78,23 @@ pub fn load_routes() -> Router { let path = route_values.path.clone(); let path = path.as_str(); + macro_rules! match_routes { + ($route_function:expr) => { + router.route( + path, + $route_function(|request: Request| route(route_values, request)), + ) + }; + } + router = match route_values.method { - Method::Get => router.route( - path, - get(|request: Request| route(route_values, request)), - ), - Method::Post => router.route( - path, - post(|request: Request| route(route_values, request)), - ), - Method::Put => router.route( - path, - put(|request: Request| route(route_values, request)), - ), - Method::Delete => router.route( - path, - delete(|request: Request| route(route_values, request)), - ), + Method::Get => match_routes!(get), + Method::Post => match_routes!(post), + Method::Put => match_routes!(put), + Method::Delete => match_routes!(delete), + Method::Options => match_routes!(options), + Method::Patch => match_routes!(patch), + Method::Trace => match_routes!(trace), Method::Static => { if let Some(serve_path) = route_values.serve_folder { router.nest_service(path, tower_http::services::ServeDir::new(serve_path)) @@ -102,9 +105,15 @@ pub fn load_routes() -> Router { } } - router.layer( - tower::ServiceBuilder::new() - .layer(tower_http::decompression::RequestDecompressionLayer::new()) - .layer(tower_http::compression::CompressionLayer::new()), - ) + // TODO: add another release binary that does support this flag + #[cfg(feature = "compression")] + { + router = router.layer( + tower::ServiceBuilder::new() + .layer(tower_http::decompression::RequestDecompressionLayer::new()) + .layer(tower_http::compression::CompressionLayer::new()), + ); + }; + + router }