Skip to content

Commit a12bb45

Browse files
committed
feat: example of NGINX buildsystem integration
1 parent b1cee8f commit a12bb45

File tree

8 files changed

+156
-10
lines changed

8 files changed

+156
-10
lines changed

examples/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ crate-type = ["cdylib"]
4242
tokio = { version = "1.33.0", features = ["full"] }
4343

4444
[features]
45+
default = ["export-modules"]
46+
# Generate `ngx_modules` table with module exports
47+
# Exports table is required for building loadable modules with --crate-type cdylib
48+
# outside of the NGINX buildsystem, but cargo currently does not allow to detect
49+
# this configuration automatically.
50+
# See https://github.com/rust-lang/rust/issues/20267
51+
export-modules = []
4552
linux = []

examples/async.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ngx::ffi::{
66
};
77
use ngx::http::MergeConfigError;
88
use ngx::{core, core::Status, http, http::HTTPModule};
9-
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
9+
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
1010
use std::os::raw::{c_char, c_void};
1111
use std::ptr::{addr_of, addr_of_mut};
1212
use std::sync::atomic::AtomicBool;
@@ -78,9 +78,11 @@ static ngx_http_async_module_ctx: ngx_http_module_t = ngx_http_module_t {
7878
merge_loc_conf: Some(Module::merge_loc_conf),
7979
};
8080

81-
ngx_modules!(ngx_http_async_module);
81+
#[cfg(feature = "export-modules")]
82+
ngx::ngx_modules!(ngx_http_async_module);
8283

8384
#[no_mangle]
85+
#[used]
8486
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
8587
ctx_index: ngx_uint_t::max_value(),
8688
index: ngx_uint_t::max_value(),

examples/awssig.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ngx::ffi::{
66
NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
77
};
88
use ngx::{core, core::Status, http::*};
9-
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
9+
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
1010
use std::os::raw::{c_char, c_void};
1111
use std::ptr::addr_of;
1212

@@ -97,9 +97,11 @@ static ngx_http_awssigv4_module_ctx: ngx_http_module_t = ngx_http_module_t {
9797
merge_loc_conf: Some(Module::merge_loc_conf),
9898
};
9999

100-
ngx_modules!(ngx_http_awssigv4_module);
100+
#[cfg(feature = "export-modules")]
101+
ngx::ngx_modules!(ngx_http_awssigv4_module);
101102

102103
#[no_mangle]
104+
#[used]
103105
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
104106
ctx_index: ngx_uint_t::max_value(),
105107
index: ngx_uint_t::max_value(),

examples/config

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
ngx_addon_name=ngx_rust_examples
2+
ngx_cargo_profile=ngx-module
3+
4+
if [ $HTTP = YES ]; then
5+
ngx_module_type=HTTP
6+
7+
if :; then
8+
ngx_module_name=ngx_http_async_module
9+
ngx_module_lib=async
10+
11+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
12+
ngx_module_deps=$ngx_module_lib
13+
ngx_module_libs="$ngx_module_lib -lm"
14+
15+
# Module deps are usually added to the object file targets, but we don't have any
16+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
17+
18+
. auto/module
19+
fi
20+
21+
if :; then
22+
ngx_module_name=ngx_http_awssigv4_module
23+
ngx_module_lib=awssig
24+
25+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
26+
ngx_module_deps=$ngx_module_lib
27+
ngx_module_libs=$ngx_module_lib
28+
29+
# Module deps are usually added to the object file targets, but we don't have any
30+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
31+
32+
. auto/module
33+
fi
34+
35+
if :; then
36+
ngx_module_name=ngx_http_curl_module
37+
ngx_module_lib=curl
38+
39+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
40+
ngx_module_deps=$ngx_module_lib
41+
ngx_module_libs=$ngx_module_lib
42+
43+
# Module deps are usually added to the object file targets, but we don't have any
44+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
45+
46+
. auto/module
47+
fi
48+
49+
case "$NGX_PLATFORM" in
50+
Linux:*)
51+
ngx_module_name=ngx_http_orig_dst_module
52+
ngx_module_lib=httporigdst
53+
54+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
55+
ngx_module_deps=$ngx_module_lib
56+
ngx_module_libs=$ngx_module_lib
57+
58+
# Module deps are usually added to the object file targets, but we don't have any
59+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
60+
61+
. auto/module
62+
;;
63+
esac
64+
65+
if :; then
66+
ngx_module_name=ngx_http_upstream_custom_module
67+
ngx_module_lib=upstream
68+
69+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
70+
ngx_module_deps=$ngx_module_lib
71+
ngx_module_libs=$ngx_module_lib
72+
73+
# Module deps are usually added to the object file targets, but we don't have any
74+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
75+
76+
. auto/module
77+
fi
78+
fi
79+
80+
# Write a cargo config with the $ngx_cargo_profile definition (optional)
81+
82+
if [ "$NGX_DEBUG" = YES ]; then
83+
NGX_CARGO_PROFILE_BASE=dev
84+
else
85+
NGX_CARGO_PROFILE_BASE=release
86+
fi
87+
88+
mkdir -p "$NGX_OBJS/.cargo"
89+
cat > "$NGX_OBJS/.cargo/config.toml" << END
90+
91+
[profile.$ngx_cargo_profile]
92+
inherits = "$NGX_CARGO_PROFILE_BASE"
93+
94+
END

examples/config.make

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ngx_addon_name=ngx_rust_examples
2+
ngx_cargo_profile=ngx-module
3+
ngx_cargo_manifest=$(realpath $ngx_addon_dir/Cargo.toml)
4+
ngx_cargo_features=
5+
ngx_rust_examples="async awssig curl upstream"
6+
7+
case "$NGX_PLATFORM" in
8+
Linux:*)
9+
ngx_cargo_features="$ngx_cargo_features linux"
10+
ngx_rust_examples="$ngx_rust_examples httporigdst"
11+
;;
12+
esac
13+
14+
for ngx_rust_example in $ngx_rust_examples
15+
do
16+
17+
cat << END >> $NGX_MAKEFILE
18+
19+
# Always call cargo instead of tracking the source modifications
20+
.PHONY: $NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a
21+
22+
$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a:
23+
cd $NGX_OBJS && \\
24+
NGX_OBJS="\$\$PWD" cargo rustc \\
25+
--crate-type staticlib \\
26+
--example "$ngx_rust_example" \\
27+
--no-default-features \\
28+
--features "$ngx_cargo_features" \\
29+
--profile $ngx_cargo_profile \\
30+
--target-dir $ngx_addon_name \\
31+
--manifest-path $ngx_cargo_manifest
32+
33+
END
34+
35+
done

examples/curl.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ngx::ffi::{
66
};
77
use ngx::http::MergeConfigError;
88
use ngx::{core, core::Status, http, http::HTTPModule};
9-
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
9+
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
1010
use std::os::raw::{c_char, c_void};
1111
use std::ptr::addr_of;
1212

@@ -61,9 +61,11 @@ static ngx_http_curl_module_ctx: ngx_http_module_t = ngx_http_module_t {
6161
merge_loc_conf: Some(Module::merge_loc_conf),
6262
};
6363

64-
ngx_modules!(ngx_http_curl_module);
64+
#[cfg(feature = "export-modules")]
65+
ngx::ngx_modules!(ngx_http_curl_module);
6566

6667
#[no_mangle]
68+
#[used]
6769
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
6870
ctx_index: ngx_uint_t::max_value(),
6971
index: ngx_uint_t::max_value(),

examples/httporigdst.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ngx::ffi::{
55
NGX_RS_MODULE_SIGNATURE,
66
};
77
use ngx::{core, core::Status, http, http::HTTPModule};
8-
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_modules, ngx_null_string, ngx_string};
8+
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_null_string, ngx_string};
99
use std::os::raw::{c_char, c_int, c_void};
1010

1111
const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;
@@ -86,9 +86,11 @@ static ngx_http_orig_dst_module_ctx: ngx_http_module_t = ngx_http_module_t {
8686
merge_loc_conf: Some(Module::merge_loc_conf),
8787
};
8888

89-
ngx_modules!(ngx_http_orig_dst_module);
89+
#[cfg(feature = "export-modules")]
90+
ngx::ngx_modules!(ngx_http_orig_dst_module);
9091

9192
#[no_mangle]
93+
#[used]
9294
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
9395
ctx_index: ngx_uint_t::max_value(),
9496
index: ngx_uint_t::max_value(),

examples/upstream.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ngx::{
2222
},
2323
http_upstream_init_peer_pt,
2424
log::DebugMask,
25-
ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string,
25+
ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string,
2626
};
2727
use std::{
2828
mem,
@@ -105,9 +105,11 @@ static mut ngx_http_upstream_custom_commands: [ngx_command_t; 2] = [
105105
ngx_null_command!(),
106106
];
107107

108-
ngx_modules!(ngx_http_upstream_custom_module);
108+
#[cfg(feature = "export-modules")]
109+
ngx::ngx_modules!(ngx_http_upstream_custom_module);
109110

110111
#[no_mangle]
112+
#[used]
111113
pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
112114
ctx_index: ngx_uint_t::max_value(),
113115
index: ngx_uint_t::max_value(),

0 commit comments

Comments
 (0)