Created
March 20, 2015 22:19
-
-
Save sunilmallya/c3b8200b2e5ef8c30578 to your computer and use it in GitHub Desktop.
hello world nginx module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Hello world NGINX template for the blog | |
* | |
* For nginx data types refer - | |
* http://antoine.bonavita.free.fr/nginx_mod_dev_en.html | |
* @author: sunil mallya | |
* | |
*/ | |
#include <ngx_core.h> | |
#include <ngx_http.h> | |
#include <pthread.h> | |
#include <stdlib.h> | |
#include <time.h> | |
/* | |
* These function install request handlers | |
*/ | |
static char *ngx_http_test_healthcheck_hook(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
static ngx_int_t ngx_http_test_handler_healthcheck(ngx_http_request_t *r); | |
// config handlers | |
static ngx_conf_post_handler_pt ngx_http_test_my_test_variable_p = ngx_http_test_my_test_variable; | |
// Module Hook Methods | |
ngx_int_t test_init_process(ngx_cycle_t *cycle); | |
void test_exit_process(ngx_cycle_t *cycle); | |
ngx_int_t test_init_module(ngx_cycle_t *cycle); | |
/* | |
* specific configuration structure | |
* | |
* */ | |
typedef struct { | |
time_t my_test_variable; | |
// Add more variables to the struct here | |
} ngx_http_test_loc_conf_t; | |
static ngx_http_test_loc_conf_t ngx_http_test_loc_conf; | |
/* | |
* config vars | |
*/ | |
static ngx_command_t ngx_http_test_commands[] = { | |
{ ngx_string("variable_name_in_config"), | |
NGX_HTTP_MAIN_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
ngx_conf_set_sec_slot, | |
NGX_HTTP_LOC_CONF_OFFSET, | |
offsetof(ngx_http_test_loc_conf_t, my_test_variable), | |
&ngx_http_test_my_test_variable_p}, | |
ngx_null_command | |
}; | |
/* Create loc conf to be used by the module | |
* It takes a directive struct (ngx_conf_t) and returns a newly | |
* created module configuration struct | |
*/ | |
static void * | |
ngx_http_test_create_loc_conf(ngx_conf_t * cf) | |
{ | |
ngx_http_test_loc_conf_t * conf; | |
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_test_loc_conf_t)); | |
if(conf == NULL) { | |
return NGX_CONF_ERROR; | |
} | |
conf->my_test_variable = NGX_CONF_UNSET_UINT; | |
return conf; | |
} | |
static char * | |
ngx_http_test_merge_loc_conf(ngx_conf_t * cf, void * parent, void * child) | |
{ | |
return NGX_CONF_OK; | |
} | |
static char * | |
ngx_http_test_my_test_variable(ngx_conf_t *cf, void *post, void *data){ | |
time_t *name = data; // i.e., first field of var | |
if(name == NULL){ | |
return NGX_CONF_ERROR; | |
} | |
ngx_http_test_loc_conf.my_test_variable = *name; | |
return NGX_CONF_OK; | |
} | |
/* Module Context | |
* | |
* This is a static ngx_http_module_t struct, which just has a bunch of function | |
* references for creating the three configurations and merging them together. | |
* Its name is ngx_http_<module name>_module_ctx. In order, the function references are: | |
* | |
* preconfiguration | |
* postconfiguration | |
* creating the main conf (i.e., do a malloc and set defaults) | |
* initializing the main conf (i.e., override the defaults with what's in nginx.conf) | |
* creating the server conf | |
* merging it with the main conf | |
* creating the location conf | |
* merging it with the server conf | |
*/ | |
static ngx_http_module_t ngx_http_test_module_ctx = { | |
NULL, /* preconfiguration */ | |
NULL, /* postconfiguration */ | |
NULL, /* create main configuration */ | |
NULL, /* init main configuration */ | |
ngx_http_test_create_loc_conf, /* create server configuration */ | |
ngx_http_test_merge_loc_conf, /* merge server configuration */ | |
ngx_http_test_create_loc_conf, /* create location configuration */ | |
ngx_http_test_merge_loc_conf /* merge location configuration */ | |
}; | |
/* Module Definition | |
* This is one more layer of indirection, the ngx_module_t struct. | |
* The variable is called ngx_http_<module name>_module. | |
* This is where references to the context and directives go, as well as the | |
* remaining callbacks (exit thread, exit process, etc.). | |
* The module definition is sometimes used as a key to look up data associated with a particular module | |
* | |
*/ | |
ngx_module_t ngx_http_test_module = { | |
NGX_MODULE_V1, | |
&ngx_http_test_module_ctx, /* module context */ | |
ngx_http_test_commands, /* module directives */ | |
NGX_HTTP_MODULE, /* module type */ | |
NULL, /* init master */ | |
test_init_module, /* init module */ | |
&test_init_process, /* init process */ | |
NULL, /* init thread */ | |
NULL, /* exit thread */ | |
&test_exit_process, /* exit process */ | |
NULL, /* exit master */ | |
NGX_MODULE_V1_PADDING | |
}; | |
ngx_int_t test_init_process(ngx_cycle_t *cycle){ | |
// Call any init functions that you have to | |
// Place to start background threads, watchdogs etc... | |
// | |
return 0; | |
} | |
void | |
test_exit_process(ngx_cycle_t *cycle){ | |
test_terminate_updater(); | |
} | |
ngx_int_t | |
test_init_module(ngx_cycle_t *cycle){ | |
return 0; | |
} | |
static ngx_int_t ngx_http_test_handler_healthcheck(ngx_http_request_t *r) | |
{ | |
ngx_buf_t *b; | |
ngx_chain_t out; | |
r->headers_out.content_type.len = sizeof("text/plain") - 1; | |
r->headers_out.content_type.data = (u_char *) "text/plain"; | |
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
out.buf = b; | |
out.next = NULL; | |
char * body = "some msg"; | |
b->pos = (unsigned char*)body; | |
b->last = (unsigned char*)body + strlen((char*)body); | |
b->memory = 1; | |
b->last_buf = 1; | |
r->headers_out.content_length_n = strlen((char*)body); | |
ngx_http_send_header(r); | |
return ngx_http_output_filter(r, &out); | |
} | |
// Handlers installers : Hooks for each of the Interface handlers | |
static char *ngx_http_test_healthcheck_hook(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
{ | |
ngx_http_core_loc_conf_t *clcf; | |
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
clcf->handler = ngx_http_test_handler_healthcheck; | |
return NGX_CONF_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment