Skip to content

Commit

Permalink
refactor!: reduce boilerplate code for ngx_module_t declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed Jan 29, 2025
1 parent a575249 commit e834149
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 154 deletions.
38 changes: 7 additions & 31 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::time::Instant;

use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_cycle, ngx_event_t, ngx_http_core_module,
ngx_http_core_run_phases, ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE,
ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_posted_events, ngx_queue_s, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_cycle, ngx_event_t, ngx_http_core_module, ngx_http_core_run_phases,
ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_http_request_t, ngx_int_t,
ngx_module_t, ngx_posted_events, ngx_queue_s, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF,
NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET,
};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
Expand Down Expand Up @@ -86,34 +86,10 @@ ngx::ngx_modules!(ngx_http_async_module);
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,

ctx: &NGX_HTTP_ASYNC_MODULE_CTX as *const _ as *mut _,
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
type_: NGX_HTTP_MODULE as ngx_uint_t,

init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,

spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};

impl http::Merge for ModuleConfig {
Expand Down
37 changes: 6 additions & 31 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::ptr::addr_of;
use http::HeaderMap;
use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF, NGX_RS_HTTP_LOC_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF, NGX_RS_HTTP_LOC_CONF_OFFSET,
};
use ngx::http::*;
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
Expand Down Expand Up @@ -106,34 +105,10 @@ ngx::ngx_modules!(ngx_http_awssigv4_module);
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,

ctx: &NGX_HTTP_AWSSIGV4_MODULE_CTX as *const _ as *mut _,
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
commands: unsafe { &NGX_HTTP_AWSSIGV4_COMMANDS[0] as *const _ as *mut _ },
type_: NGX_HTTP_MODULE as ngx_uint_t,

init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,

spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};

impl Merge for ModuleConfig {
Expand Down
36 changes: 6 additions & 30 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::ptr::addr_of;

use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET,
};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
Expand Down Expand Up @@ -68,34 +68,10 @@ ngx::ngx_modules!(ngx_http_curl_module);
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,

ctx: &NGX_HTTP_CURL_MODULE_CTX as *const _ as *mut _,
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
commands: unsafe { &NGX_HTTP_CURL_COMMANDS[0] as *const _ as *mut _ },
type_: NGX_HTTP_MODULE as ngx_uint_t,

init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,

spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};

impl http::Merge for ModuleConfig {
Expand Down
35 changes: 6 additions & 29 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::ffi::{c_char, c_int, c_void};
use std::ffi::{c_int, c_void};
use std::ptr::addr_of;

use ngx::core;
use ngx::ffi::{
in_port_t, nginx_version, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
in_port_t, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_uint_t,
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE, NGX_RS_MODULE_SIGNATURE,
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
};
use ngx::http::{self, HTTPModule};
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_null_string, ngx_string};
Expand Down Expand Up @@ -96,33 +96,10 @@ ngx::ngx_modules!(ngx_http_orig_dst_module);
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
ctx: &NGX_HTTP_ORIG_DST_MODULE_CTX as *const _ as *mut _,
ctx: std::ptr::addr_of!(NGX_HTTP_ORIG_DST_MODULE_CTX) as _,
commands: std::ptr::null_mut(),
type_: NGX_HTTP_MODULE as ngx_uint_t,

init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,

spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};

static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 3] = [
Expand Down
41 changes: 8 additions & 33 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ use std::slice;

use ngx::core::{Pool, Status};
use ngx::ffi::{
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1,
NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt, ngx_event_get_peer_pt,
ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t,
ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR,
NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
};
use ngx::http::{
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
Expand Down Expand Up @@ -110,34 +109,10 @@ ngx::ngx_modules!(ngx_http_upstream_custom_module);
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,

ctx: &NGX_HTTP_UPSTREAM_CUSTOM_CTX as *const _ as *mut _,
ctx: std::ptr::addr_of!(NGX_HTTP_UPSTREAM_CUSTOM_CTX) as _,
commands: unsafe { &NGX_HTTP_UPSTREAM_CUSTOM_COMMANDS[0] as *const _ as *mut _ },
type_: NGX_HTTP_MODULE as ngx_uint_t,

init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,

spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
type_: NGX_HTTP_MODULE as _,
..ngx_module_t::default()
};

// http_upstream_init_custom_peer
Expand Down
33 changes: 33 additions & 0 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,39 @@ impl TryFrom<ngx_str_t> for &str {
}
}

impl ngx_module_t {
/// Create a new `ngx_module_t` instance with default values.
pub const fn default() -> Self {
Self {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: core::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr(),
ctx: core::ptr::null_mut(),
commands: core::ptr::null_mut(),
type_: 0,
init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,
spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
}
}
}

/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
///
/// # Arguments
Expand Down

0 comments on commit e834149

Please sign in to comment.