Skip to content

Commit

Permalink
feat: example of Nginx buildsystem integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bavshin-f5 committed Apr 17, 2024
1 parent 4e6520f commit f7dc512
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ crate-type = ["cdylib"]
tokio = { version = "1.33.0", features = ["full"] }

[features]
default = ["export-modules"]
# Generate `ngx_modules` table with module exports
export-modules = []
linux = []
2 changes: 2 additions & 0 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ static ngx_http_async_module_ctx: ngx_http_module_t = ngx_http_module_t {
merge_loc_conf: Some(Module::merge_loc_conf),
};

#[cfg(feature = "export-modules")]
ngx_modules!(ngx_http_async_module);

#[no_mangle]
#[used]
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
Expand Down
2 changes: 2 additions & 0 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ static ngx_http_awssigv4_module_ctx: ngx_http_module_t = ngx_http_module_t {
merge_loc_conf: Some(Module::merge_loc_conf),
};

#[cfg(feature = "export-modules")]
ngx_modules!(ngx_http_awssigv4_module);

#[no_mangle]
#[used]
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
Expand Down
94 changes: 94 additions & 0 deletions examples/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
ngx_addon_name=ngx_rust_examples
ngx_cargo_profile=ngx-module

if [ $HTTP = YES ]; then
ngx_module_type=HTTP

if :; then
ngx_module_name=ngx_http_async_module
ngx_module_lib=async

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs="$ngx_module_lib -lm"

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"

. auto/module
fi

if :; then
ngx_module_name=ngx_http_awssigv4_module
ngx_module_lib=awssig

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs=$ngx_module_lib

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"

. auto/module
fi

if :; then
ngx_module_name=ngx_http_curl_module
ngx_module_lib=curl

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs=$ngx_module_lib

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"

. auto/module
fi

case "$NGX_PLATFORM" in
Linux:*)
ngx_module_name=ngx_http_orig_dst_module
ngx_module_lib=httporigdst

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs=$ngx_module_lib

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"

. auto/module
;;
esac

if :; then
ngx_module_name=ngx_http_upstream_custom_module
ngx_module_lib=upstream

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs=$ngx_module_lib

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"

. auto/module
fi
fi

# Write a cargo config with the $ngx_cargo_profile definition (optional)

if [ "$NGX_DEBUG" = YES ]; then
NGX_CARGO_PROFILE_BASE=dev
else
NGX_CARGO_PROFILE_BASE=release
fi

mkdir -p "$NGX_OBJS/.cargo"
cat > "$NGX_OBJS/.cargo/config.toml" << END

[profile.$ngx_cargo_profile]
inherits = "$NGX_CARGO_PROFILE_BASE"

END
35 changes: 35 additions & 0 deletions examples/config.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ngx_addon_name=ngx_rust_examples
ngx_cargo_profile=ngx-module
ngx_cargo_manifest=$(realpath $ngx_addon_dir/Cargo.toml)
ngx_cargo_features=
ngx_rust_examples="async awssig curl upstream"

case "$NGX_PLATFORM" in
Linux:*)
ngx_cargo_features="$ngx_cargo_features linux"
ngx_rust_examples="$ngx_rust_examples httporigdst"
;;
esac

for ngx_rust_example in $ngx_rust_examples
do

cat << END >> $NGX_MAKEFILE

# Always call cargo instead of tracking the source modifications
.PHONY: $NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a

$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a:
cd $NGX_OBJS && \\
NGX_OBJS="\$\$PWD" cargo rustc \\
--crate-type staticlib \\
--example "$ngx_rust_example" \\
--no-default-features \\
--features "$ngx_cargo_features" \\
--profile $ngx_cargo_profile \\
--target-dir $ngx_addon_name \\
--manifest-path $ngx_cargo_manifest

END

done
2 changes: 2 additions & 0 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ static ngx_http_curl_module_ctx: ngx_http_module_t = ngx_http_module_t {
merge_loc_conf: Some(Module::merge_loc_conf),
};

#[cfg(feature = "export-modules")]
ngx_modules!(ngx_http_curl_module);

#[no_mangle]
#[used]
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
Expand Down
2 changes: 2 additions & 0 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ static ngx_http_orig_dst_module_ctx: ngx_http_module_t = ngx_http_module_t {
merge_loc_conf: Some(Module::merge_loc_conf),
};

#[cfg(feature = "export-modules")]
ngx_modules!(ngx_http_orig_dst_module);

#[no_mangle]
#[used]
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
Expand Down
2 changes: 2 additions & 0 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ static mut ngx_http_upstream_custom_commands: [ngx_command_t; 2] = [
ngx_null_command!(),
];

#[cfg(feature = "export-modules")]
ngx_modules!(ngx_http_upstream_custom_module);

#[no_mangle]
#[used]
pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
Expand Down

0 comments on commit f7dc512

Please sign in to comment.