Skip to content

Commit

Permalink
chore: 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 28, 2025
1 parent 2d7fd00 commit 4173182
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 146 deletions.
35 changes: 4 additions & 31 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use std::time::Instant;

use ngx::core;
use ngx::core::prelude::*;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_http_core_run_phases, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_queue_s,
NGX_RS_MODULE_SIGNATURE,
};
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_queue_s};
use ngx::http::prelude::*;
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 +83,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
32 changes: 4 additions & 28 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::addr_of;
use http::HeaderMap;
use ngx::core;
use ngx::core::prelude::*;
use ngx::ffi::{nginx_version, ngx_array_push, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, NGX_RS_MODULE_SIGNATURE};
use ngx::ffi::{ngx_array_push, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE};
use ngx::http::prelude::*;
use ngx::http::*;
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
Expand Down Expand Up @@ -103,34 +103,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
32 changes: 4 additions & 28 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ptr::addr_of;

use ngx::core;
use ngx::core::prelude::*;
use ngx::ffi::{nginx_version, ngx_array_push, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, NGX_RS_MODULE_SIGNATURE};
use ngx::ffi::{ngx_array_push, ngx_http_phases_NGX_HTTP_ACCESS_PHASE};
use ngx::http::prelude::*;
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
Expand Down Expand Up @@ -66,34 +66,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::core::prelude::*;
use ngx::ffi::{
in_port_t, nginx_version, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr,
sockaddr_storage, INET_ADDRSTRLEN, NGX_RS_MODULE_SIGNATURE,
in_port_t, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr, sockaddr_storage,
INET_ADDRSTRLEN,
};
use ngx::http::prelude::*;
use ngx::http::{self, HTTPModule};
Expand Down Expand Up @@ -97,33 +97,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
36 changes: 6 additions & 30 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::slice;
use ngx::core::prelude::*;
use ngx::core::{Pool, Status};
use ngx::ffi::{
nginx_version, ngx_atoi, ngx_event_free_peer_pt, ngx_event_get_peer_pt, 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_peer_connection_t, NGX_ERROR, NGX_RS_MODULE_SIGNATURE,
ngx_atoi, ngx_event_free_peer_pt, ngx_event_get_peer_pt, 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_peer_connection_t, NGX_ERROR,
};
use ngx::http::prelude::*;
use ngx::http::{
Expand Down Expand Up @@ -109,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 @@ -188,6 +188,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() as *const core::ffi::c_char,
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 4173182

Please sign in to comment.