Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce boilerplate code for ngx_module_t declarations #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 20 additions & 49 deletions examples/async.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::ffi::{c_char, c_void};
use std::ptr::{addr_of, addr_of_mut};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
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,
};
use ngx::core::prelude::*;
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, 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};
use tokio::runtime::Runtime;
Expand All @@ -25,8 +21,7 @@ impl http::HTTPModule for Module {
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_ACCESS_PHASE].handlers) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
Expand Down Expand Up @@ -54,18 +49,6 @@ impl Default for ModuleConfig {
}
}

static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("async"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_async_commands_set_enable),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
];

static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
Expand All @@ -86,34 +69,22 @@ 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 _,
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,
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
commands: [
ngx_command_t {
name: ngx_string!("async"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_async_commands_set_enable),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
]
.as_ptr()
.cast_mut(),
type_: NGX_HTTP_MODULE as _,
..NGX_RS_MODULE_V1
};

impl http::Merge for ModuleConfig {
Expand Down
133 changes: 52 additions & 81 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::ffi::{c_char, c_void};
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,
};
use ngx::core::prelude::*;
use ngx::ffi::ngx_array_push;
use ngx::http::prelude::*;
use ngx::http::*;
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};

Expand All @@ -22,8 +18,7 @@ impl HTTPModule for Module {
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_PRECONTENT_PHASE].handlers) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
Expand All @@ -42,50 +37,6 @@ struct ModuleConfig {
s3_endpoint: String,
}

static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
ngx_command_t {
name: ngx_string!("awssigv4"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_awssigv4_commands_set_enable),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_access_key"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_awssigv4_commands_set_access_key),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_secret_key"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_awssigv4_commands_set_secret_key),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_s3_bucket"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_awssigv4_commands_set_s3_bucket),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_s3_endpoint"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_awssigv4_commands_set_s3_endpoint),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
];

static NGX_HTTP_AWSSIGV4_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
Expand All @@ -106,34 +57,54 @@ 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 _,
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,
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
commands: [
ngx_command_t {
name: ngx_string!("awssigv4"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_awssigv4_commands_set_enable),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_access_key"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_awssigv4_commands_set_access_key),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_secret_key"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_awssigv4_commands_set_secret_key),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_s3_bucket"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_awssigv4_commands_set_s3_bucket),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_command_t {
name: ngx_string!("awssigv4_s3_endpoint"),
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_awssigv4_commands_set_s3_endpoint),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
]
.as_ptr()
.cast_mut(),
type_: NGX_HTTP_MODULE as _,
..NGX_RS_MODULE_V1
};

impl Merge for ModuleConfig {
Expand Down
68 changes: 20 additions & 48 deletions examples/curl.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::ffi::{c_char, c_void};
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,
};
use ngx::core::prelude::*;
use ngx::ffi::ngx_array_push;
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 All @@ -20,8 +17,7 @@ impl http::HTTPModule for Module {
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_ACCESS_PHASE].handlers) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
Expand All @@ -36,18 +32,6 @@ struct ModuleConfig {
enable: bool,
}

static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("curl"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_curl_commands_set_enable),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
];

static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
Expand All @@ -68,34 +52,22 @@ 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 _,
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,
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
commands: [
ngx_command_t {
name: ngx_string!("curl"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as _,
set: Some(ngx_http_curl_commands_set_enable),
conf: NGX_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
]
.as_ptr()
.cast_mut(),
type_: NGX_HTTP_MODULE as _,
..NGX_RS_MODULE_V1
};

impl http::Merge for ModuleConfig {
Expand Down
Loading
Loading