Skip to content

Instantly share code, notes, and snippets.

@patryk4815
Created January 21, 2024 15:36
Show Gist options
  • Save patryk4815/320d403dd15d5d0e19562f453ea8a7aa to your computer and use it in GitHub Desktop.
Save patryk4815/320d403dd15d5d0e19562f453ea8a7aa to your computer and use it in GitHub Desktop.
const c = @cImport({
@cInclude("ngx_config.h");
@cInclude("ngx_core.h");
@cInclude("ngx_string.h");
@cInclude("ngx_http.h");
@cInclude("ngx_event_openssl.h");
});
const ngx_foo_conf_t = extern struct {
enable: c.ngx_flag_t,
};
// static void *
// ngx_foo_create_srv_conf(ngx_conf_t *cf) {
fn ngx_foo_create_srv_conf(cf: ?*c.ngx_conf_t) callconv(.C) ?*anyopaque {
var conf: ?*c.ngx_foo_conf_t = c.ngx_pcalloc(cf.pool, @sizeOf(ngx_foo_conf_t));
if (!conf) {
return c.NGX_CONF_ERROR;
}
conf.enable = c.NGX_CONF_UNSET;
return conf; //??? @as(?*anyopaque, @ptrCast(conf));
}
// static char *
// ngx_foo_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) {
fn ngx_foo_merge_srv_conf(cf: ?*c.ngx_conf_t, parent: ?*anyopaque, child: ?*anyopaque) callconv(.C) *c.u8 {
_ = cf;
var prev: *c.ngx_foo_conf_t = @as(*c.ngx_foo_conf_t, @ptrCast(@alignCast(parent)));
var conf: *c.ngx_foo_conf_t = @as(*c.ngx_foo_conf_t, @ptrCast(@alignCast(child)));
c.ngx_conf_merge_value(conf.enable, prev.enable, 0);
return c.NGX_CONF_OK;
}
// static ngx_int_t
// ngx_foo_variables(ngx_conf_t *cf) {
fn ngx_foo_variables(cf: ?*c.ngx_conf_t) callconv(.C) c.ngx_int_t {
_ = cf;
return c.NGX_OK;
}
// static ngx_int_t
// ngx_foo_init_module(ngx_cycle_t *cycle) {
fn ngx_foo_init_module(cycle: ?*c.ngx_cycle_t) callconv(.C) c.ngx_int_t {
_ = cycle;
return c.NGX_OK;
}
const ngx_foo_commands = c.ngx_command_t[2]{
c.ngx_command_t{
.name = c.ngx_string("foo_enable"),
.type = c.NGX_HTTP_SRV_CONF | c.NGX_CONF_FLAG,
.set = c.ngx_conf_set_flag_slot,
.conf = c.NGX_HTTP_SRV_CONF_OFFSET,
.offset = @offsetOf(ngx_foo_conf_t, "enable"),
.post = c.NULL,
},
c.ngx_null_command,
};
const ngx_foo_module_ctx = c.ngx_http_module_t{
.preconfiguration = ngx_foo_variables,
.postconfiguration = c.NULL,
.create_main_conf = c.NULL,
.init_main_conf = c.NULL,
.create_srv_conf = ngx_foo_create_srv_conf,
.merge_srv_conf = ngx_foo_merge_srv_conf,
.create_loc_conf = c.NULL,
.merge_loc_conf = c.NULL,
};
export const ngx_foo_module: c.ngx_module_t = .{
// .version = c.NGX_MODULE_V1,
.version = @as(c.ngx_uint_t, @bitCast(@as(c_long, @as(c_int, 1025003)))),
.signature = "8,4,8,1011000111010111011101111111111111",
.ctx = &ngx_foo_module_ctx,
.commands = ngx_foo_commands,
.type = c.NGX_HTTP_MODULE,
.init_master = c.NULL,
.init_module = ngx_foo_init_module,
.init_process = c.NULL,
.init_thread = c.NULL,
.exit_thread = c.NULL,
.exit_process = c.NULL,
.exit_master = c.NULL,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment