Skip to content

Commit

Permalink
feat: optional compression and added new route methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Dec 26, 2024
1 parent a53971e commit ae7b01e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
[package]
name = "astra"
version = "0.3.0"
version = "0.3.1"
authors = ["Elham Aryanpur <elhamaryanpur5@gmail.com>"]
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"]

[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"] }
Expand All @@ -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",
Expand All @@ -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"] }
Expand Down
26 changes: 18 additions & 8 deletions lua/astra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 31 additions & 22 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +15,9 @@ pub enum Method {
Post,
Put,
Delete,
Options,
Patch,
Trace,
Static,
}
#[derive(Debug, Clone, mlua::FromLua, PartialEq)]
Expand Down Expand Up @@ -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<Body>| route(route_values, request)),
)
};
}

router = match route_values.method {
Method::Get => router.route(
path,
get(|request: Request<Body>| route(route_values, request)),
),
Method::Post => router.route(
path,
post(|request: Request<Body>| route(route_values, request)),
),
Method::Put => router.route(
path,
put(|request: Request<Body>| route(route_values, request)),
),
Method::Delete => router.route(
path,
delete(|request: Request<Body>| 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))
Expand All @@ -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
}

0 comments on commit ae7b01e

Please # to comment.