Skip to content

Commit 38c6d69

Browse files
committed
chore: reduce boilerplate code for ngx_module_t declarations
1 parent e47e4e6 commit 38c6d69

File tree

6 files changed

+133
-223
lines changed

6 files changed

+133
-223
lines changed

examples/async.rs

+17-41
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Instant;
55

66
use ngx::core;
77
use ngx::core::prelude::*;
8-
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, ngx_queue_s, NGX_RS_MODULE_SIGNATURE};
8+
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, ngx_queue_s};
99
use ngx::http::prelude::*;
1010
use ngx::http::{self, HTTPModule, MergeConfigError};
1111
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -49,18 +49,6 @@ impl Default for ModuleConfig {
4949
}
5050
}
5151

52-
static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
53-
ngx_command_t {
54-
name: ngx_string!("async"),
55-
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
56-
set: Some(ngx_http_async_commands_set_enable),
57-
conf: NGX_HTTP_LOC_CONF_OFFSET,
58-
offset: 0,
59-
post: std::ptr::null_mut(),
60-
},
61-
ngx_null_command!(),
62-
];
63-
6452
static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
6553
preconfiguration: Some(Module::preconfiguration),
6654
postconfiguration: Some(Module::postconfiguration),
@@ -81,34 +69,22 @@ ngx::ngx_modules!(ngx_http_async_module);
8169
#[allow(non_upper_case_globals)]
8270
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
8371
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
84-
ctx_index: ngx_uint_t::MAX,
85-
index: ngx_uint_t::MAX,
86-
name: std::ptr::null_mut(),
87-
spare0: 0,
88-
spare1: 0,
89-
version: nginx_version as ngx_uint_t,
90-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
91-
92-
ctx: &NGX_HTTP_ASYNC_MODULE_CTX as *const _ as *mut _,
93-
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
94-
type_: NGX_HTTP_MODULE as ngx_uint_t,
95-
96-
init_master: None,
97-
init_module: None,
98-
init_process: None,
99-
init_thread: None,
100-
exit_thread: None,
101-
exit_process: None,
102-
exit_master: None,
103-
104-
spare_hook0: 0,
105-
spare_hook1: 0,
106-
spare_hook2: 0,
107-
spare_hook3: 0,
108-
spare_hook4: 0,
109-
spare_hook5: 0,
110-
spare_hook6: 0,
111-
spare_hook7: 0,
72+
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
73+
commands: [
74+
ngx_command_t {
75+
name: ngx_string!("async"),
76+
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as _,
77+
set: Some(ngx_http_async_commands_set_enable),
78+
conf: NGX_HTTP_LOC_CONF_OFFSET,
79+
offset: 0,
80+
post: std::ptr::null_mut(),
81+
},
82+
ngx_null_command!(),
83+
]
84+
.as_ptr()
85+
.cast_mut(),
86+
type_: NGX_HTTP_MODULE as _,
87+
..NGX_RS_MODULE_V1
11288
};
11389

11490
impl http::Merge for ModuleConfig {

examples/awssig.rs

+49-73
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ptr::addr_of;
33
use http::HeaderMap;
44
use ngx::core;
55
use ngx::core::prelude::*;
6-
use ngx::ffi::{ngx_array_push, NGX_RS_MODULE_SIGNATURE};
6+
use ngx::ffi::ngx_array_push;
77
use ngx::http::prelude::*;
88
use ngx::http::*;
99
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -37,50 +37,6 @@ struct ModuleConfig {
3737
s3_endpoint: String,
3838
}
3939

40-
static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
41-
ngx_command_t {
42-
name: ngx_string!("awssigv4"),
43-
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
44-
set: Some(ngx_http_awssigv4_commands_set_enable),
45-
conf: NGX_HTTP_LOC_CONF_OFFSET,
46-
offset: 0,
47-
post: std::ptr::null_mut(),
48-
},
49-
ngx_command_t {
50-
name: ngx_string!("awssigv4_access_key"),
51-
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
52-
set: Some(ngx_http_awssigv4_commands_set_access_key),
53-
conf: NGX_HTTP_LOC_CONF_OFFSET,
54-
offset: 0,
55-
post: std::ptr::null_mut(),
56-
},
57-
ngx_command_t {
58-
name: ngx_string!("awssigv4_secret_key"),
59-
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
60-
set: Some(ngx_http_awssigv4_commands_set_secret_key),
61-
conf: NGX_HTTP_LOC_CONF_OFFSET,
62-
offset: 0,
63-
post: std::ptr::null_mut(),
64-
},
65-
ngx_command_t {
66-
name: ngx_string!("awssigv4_s3_bucket"),
67-
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
68-
set: Some(ngx_http_awssigv4_commands_set_s3_bucket),
69-
conf: NGX_HTTP_LOC_CONF_OFFSET,
70-
offset: 0,
71-
post: std::ptr::null_mut(),
72-
},
73-
ngx_command_t {
74-
name: ngx_string!("awssigv4_s3_endpoint"),
75-
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
76-
set: Some(ngx_http_awssigv4_commands_set_s3_endpoint),
77-
conf: NGX_HTTP_LOC_CONF_OFFSET,
78-
offset: 0,
79-
post: std::ptr::null_mut(),
80-
},
81-
ngx_null_command!(),
82-
];
83-
8440
static NGX_HTTP_AWSSIGV4_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
8541
preconfiguration: Some(Module::preconfiguration),
8642
postconfiguration: Some(Module::postconfiguration),
@@ -101,34 +57,54 @@ ngx::ngx_modules!(ngx_http_awssigv4_module);
10157
#[allow(non_upper_case_globals)]
10258
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
10359
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
104-
ctx_index: ngx_uint_t::MAX,
105-
index: ngx_uint_t::MAX,
106-
name: std::ptr::null_mut(),
107-
spare0: 0,
108-
spare1: 0,
109-
version: nginx_version as ngx_uint_t,
110-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
111-
112-
ctx: &NGX_HTTP_AWSSIGV4_MODULE_CTX as *const _ as *mut _,
113-
commands: unsafe { &NGX_HTTP_AWSSIGV4_COMMANDS[0] as *const _ as *mut _ },
114-
type_: NGX_HTTP_MODULE as ngx_uint_t,
115-
116-
init_master: None,
117-
init_module: None,
118-
init_process: None,
119-
init_thread: None,
120-
exit_thread: None,
121-
exit_process: None,
122-
exit_master: None,
123-
124-
spare_hook0: 0,
125-
spare_hook1: 0,
126-
spare_hook2: 0,
127-
spare_hook3: 0,
128-
spare_hook4: 0,
129-
spare_hook5: 0,
130-
spare_hook6: 0,
131-
spare_hook7: 0,
60+
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
61+
commands: [
62+
ngx_command_t {
63+
name: ngx_string!("awssigv4"),
64+
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
65+
set: Some(ngx_http_awssigv4_commands_set_enable),
66+
conf: NGX_HTTP_LOC_CONF_OFFSET,
67+
offset: 0,
68+
post: std::ptr::null_mut(),
69+
},
70+
ngx_command_t {
71+
name: ngx_string!("awssigv4_access_key"),
72+
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
73+
set: Some(ngx_http_awssigv4_commands_set_access_key),
74+
conf: NGX_HTTP_LOC_CONF_OFFSET,
75+
offset: 0,
76+
post: std::ptr::null_mut(),
77+
},
78+
ngx_command_t {
79+
name: ngx_string!("awssigv4_secret_key"),
80+
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
81+
set: Some(ngx_http_awssigv4_commands_set_secret_key),
82+
conf: NGX_HTTP_LOC_CONF_OFFSET,
83+
offset: 0,
84+
post: std::ptr::null_mut(),
85+
},
86+
ngx_command_t {
87+
name: ngx_string!("awssigv4_s3_bucket"),
88+
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
89+
set: Some(ngx_http_awssigv4_commands_set_s3_bucket),
90+
conf: NGX_HTTP_LOC_CONF_OFFSET,
91+
offset: 0,
92+
post: std::ptr::null_mut(),
93+
},
94+
ngx_command_t {
95+
name: ngx_string!("awssigv4_s3_endpoint"),
96+
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
97+
set: Some(ngx_http_awssigv4_commands_set_s3_endpoint),
98+
conf: NGX_HTTP_LOC_CONF_OFFSET,
99+
offset: 0,
100+
post: std::ptr::null_mut(),
101+
},
102+
ngx_null_command!(),
103+
]
104+
.as_ptr()
105+
.cast_mut(),
106+
type_: NGX_HTTP_MODULE as _,
107+
..NGX_RS_MODULE_V1
132108
};
133109

134110
impl Merge for ModuleConfig {

examples/curl.rs

+17-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ptr::addr_of;
22

33
use ngx::core;
44
use ngx::core::prelude::*;
5-
use ngx::ffi::{ngx_array_push, NGX_RS_MODULE_SIGNATURE};
5+
use ngx::ffi::ngx_array_push;
66
use ngx::http::prelude::*;
77
use ngx::http::{self, HTTPModule, MergeConfigError};
88
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -32,18 +32,6 @@ struct ModuleConfig {
3232
enable: bool,
3333
}
3434

35-
static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
36-
ngx_command_t {
37-
name: ngx_string!("curl"),
38-
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
39-
set: Some(ngx_http_curl_commands_set_enable),
40-
conf: NGX_HTTP_LOC_CONF_OFFSET,
41-
offset: 0,
42-
post: std::ptr::null_mut(),
43-
},
44-
ngx_null_command!(),
45-
];
46-
4735
static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
4836
preconfiguration: Some(Module::preconfiguration),
4937
postconfiguration: Some(Module::postconfiguration),
@@ -64,34 +52,22 @@ ngx::ngx_modules!(ngx_http_curl_module);
6452
#[allow(non_upper_case_globals)]
6553
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
6654
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
67-
ctx_index: ngx_uint_t::MAX,
68-
index: ngx_uint_t::MAX,
69-
name: std::ptr::null_mut(),
70-
spare0: 0,
71-
spare1: 0,
72-
version: nginx_version as ngx_uint_t,
73-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
74-
75-
ctx: &NGX_HTTP_CURL_MODULE_CTX as *const _ as *mut _,
76-
commands: unsafe { &NGX_HTTP_CURL_COMMANDS[0] as *const _ as *mut _ },
77-
type_: NGX_HTTP_MODULE as ngx_uint_t,
78-
79-
init_master: None,
80-
init_module: None,
81-
init_process: None,
82-
init_thread: None,
83-
exit_thread: None,
84-
exit_process: None,
85-
exit_master: None,
86-
87-
spare_hook0: 0,
88-
spare_hook1: 0,
89-
spare_hook2: 0,
90-
spare_hook3: 0,
91-
spare_hook4: 0,
92-
spare_hook5: 0,
93-
spare_hook6: 0,
94-
spare_hook7: 0,
55+
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
56+
commands: [
57+
ngx_command_t {
58+
name: ngx_string!("curl"),
59+
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as _,
60+
set: Some(ngx_http_curl_commands_set_enable),
61+
conf: NGX_HTTP_LOC_CONF_OFFSET,
62+
offset: 0,
63+
post: std::ptr::null_mut(),
64+
},
65+
ngx_null_command!(),
66+
]
67+
.as_ptr()
68+
.cast_mut(),
69+
type_: NGX_HTTP_MODULE as _,
70+
..NGX_RS_MODULE_V1
9571
};
9672

9773
impl http::Merge for ModuleConfig {

examples/httporigdst.rs

+4-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ngx::core;
55
use ngx::core::prelude::*;
66
use ngx::ffi::{
77
in_port_t, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr, sockaddr_storage,
8-
INET_ADDRSTRLEN, NGX_RS_MODULE_SIGNATURE,
8+
INET_ADDRSTRLEN,
99
};
1010
use ngx::http::prelude::*;
1111
use ngx::http::{self, HTTPModule};
@@ -97,33 +97,10 @@ ngx::ngx_modules!(ngx_http_orig_dst_module);
9797
#[allow(non_upper_case_globals)]
9898
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
9999
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
100-
ctx_index: ngx_uint_t::MAX,
101-
index: ngx_uint_t::MAX,
102-
name: std::ptr::null_mut(),
103-
spare0: 0,
104-
spare1: 0,
105-
version: nginx_version as ngx_uint_t,
106-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
107-
ctx: &NGX_HTTP_ORIG_DST_MODULE_CTX as *const _ as *mut _,
100+
ctx: std::ptr::addr_of!(NGX_HTTP_ORIG_DST_MODULE_CTX) as _,
108101
commands: std::ptr::null_mut(),
109-
type_: NGX_HTTP_MODULE as ngx_uint_t,
110-
111-
init_master: None,
112-
init_module: None,
113-
init_process: None,
114-
init_thread: None,
115-
exit_thread: None,
116-
exit_process: None,
117-
exit_master: None,
118-
119-
spare_hook0: 0,
120-
spare_hook1: 0,
121-
spare_hook2: 0,
122-
spare_hook3: 0,
123-
spare_hook4: 0,
124-
spare_hook5: 0,
125-
spare_hook6: 0,
126-
spare_hook7: 0,
102+
type_: NGX_HTTP_MODULE as _,
103+
..NGX_RS_MODULE_V1
127104
};
128105

129106
static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 3] = [

0 commit comments

Comments
 (0)