Skip to content

Commit 1a8e005

Browse files
author
Chrono Law
committed
test module
1 parent 37b0681 commit 1a8e005

File tree

5 files changed

+492
-0
lines changed

5 files changed

+492
-0
lines changed

modules/test/ModNdgTest.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#include "NdgTestInit.hpp"
4+
5+
auto ndg_test_module = NdgTestInit::module();
6+
7+
/*
8+
#include <iostream>
9+
10+
#include "NgxAll.hpp"
11+
12+
struct NdgTestConf final
13+
{
14+
ngx_flag_t enabled = NgxUnsetValue::get();
15+
};
16+
17+
static void *create(ngx_conf_t* cf);
18+
static char *merge(ngx_conf_t *cf, void *parent, void *child);
19+
20+
static ngx_command_t ndg_test_cmds[] =
21+
{
22+
{
23+
ngx_string("ndg_test"),
24+
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
25+
ngx_conf_set_flag_slot,
26+
NGX_HTTP_LOC_CONF_OFFSET,
27+
offsetof(NdgTestConf, enabled),
28+
nullptr
29+
},
30+
31+
ngx_null_command
32+
};
33+
34+
static ngx_int_t init(ngx_conf_t* cf);
35+
static ngx_int_t handler(ngx_http_request_t *r);
36+
37+
static ngx_http_module_t ndg_test_ctx =
38+
{
39+
nullptr,
40+
init,
41+
nullptr,
42+
nullptr,
43+
nullptr,
44+
nullptr,
45+
create,
46+
nullptr,//merge
47+
};
48+
49+
ngx_module_t ndg_test_module =
50+
{
51+
NGX_MODULE_V1,
52+
&ndg_test_ctx,
53+
ndg_test_cmds,
54+
NGX_HTTP_MODULE,
55+
nullptr, // init master
56+
nullptr, // init module
57+
nullptr, // init process
58+
nullptr, // init thread
59+
nullptr, // exit thread
60+
nullptr, // exit process
61+
nullptr, // exit master
62+
NGX_MODULE_V1_PADDING
63+
};
64+
65+
static void* create(ngx_conf_t* cf)
66+
{
67+
return NgxPool(cf).alloc<NdgTestConf>();
68+
}
69+
70+
static char *merge(ngx_conf_t *cf, void *parent, void *child)
71+
{
72+
boost::ignore_unused(cf);
73+
74+
auto prev = reinterpret_cast<NdgTestConf*>(parent);
75+
auto conf = reinterpret_cast<NdgTestConf*>(child);
76+
77+
NgxValue::merge(conf->enabled, prev->enabled, 1);
78+
79+
return NGX_CONF_OK;
80+
}
81+
82+
static ngx_int_t init(ngx_conf_t* cf)
83+
{
84+
auto cmcf = reinterpret_cast<ngx_http_core_main_conf_t*>(
85+
ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module));
86+
87+
NgxArray<ngx_http_handler_pt> arr(
88+
cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
89+
90+
arr.push(handler);
91+
92+
return NGX_OK;
93+
}
94+
95+
static ngx_int_t handler(ngx_http_request_t *r)
96+
{
97+
auto cf = reinterpret_cast<NdgTestConf*>(
98+
ngx_http_get_module_loc_conf(r, ndg_test_module));
99+
100+
NgxLogError(r).print("hello c++");
101+
102+
if (cf->enabled)
103+
{
104+
std::cout << "hello nginx" << std::endl;
105+
}
106+
else
107+
{
108+
std::cout << "hello disabled" << std::endl;
109+
}
110+
111+
112+
return NGX_DECLINED;
113+
}
114+
*/
115+

modules/test/NdgTestConf.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_TEST_CONF_HPP
4+
#define _NDG_TEST_CONF_HPP
5+
6+
#include "NgxAll.hpp"
7+
8+
class NdgTestConf final
9+
{
10+
public:
11+
typedef NdgTestConf this_type;
12+
public:
13+
NdgTestConf() = default;
14+
~NdgTestConf() = default;
15+
public:
16+
ngx_flag_t enabled = NgxUnsetValue::get();
17+
public:
18+
static void* create(ngx_conf_t* cf)
19+
{
20+
return NgxPool(cf).alloc<this_type>();
21+
}
22+
23+
static char* merge(ngx_conf_t *cf, void *parent, void *child)
24+
{
25+
boost::ignore_unused(cf);
26+
27+
auto prev = reinterpret_cast<this_type*>(parent);
28+
auto conf = reinterpret_cast<this_type*>(child);
29+
30+
NgxValue::merge(conf->enabled, prev->enabled, 0);
31+
32+
return NGX_CONF_OK;
33+
}
34+
};
35+
36+
NGX_MOD_INSTANCE(NdgTestModule, ndg_test_module, NdgTestConf)
37+
38+
#endif //_NDG_TEST_CONF_HPP

0 commit comments

Comments
 (0)