forked from minhajuddin/nginx-mongodb-rest
/
ngx_http_mongodb_rest_module.c
295 lines (232 loc) · 7.93 KB
/
ngx_http_mongodb_rest_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <signal.h>
#include <stdio.h>
#include "mongo-c-driver/src/bson.h"
#include "mongo-c-driver/src/mongo.h"
#define CONTENT_TYPE "application/json"
#define MONGO_HOST "127.0.0.1"
#define MONGO_PORT 27017
static ngx_int_t ngx_http_mongodb_rest_handler(ngx_http_request_t* request);
static char* ngx_http_mongodb_rest(ngx_conf_t* cf, ngx_command_t* command, void* void_conf);
static char* ngx_http_mongodb_rest_merge_loc_conf(ngx_conf_t* cf, void* void_parent, void* void_child);
static void* ngx_http_mongodb_rest_create_loc_conf(ngx_conf_t* conf);
static void get(ngx_str_t* db, ngx_str_t* collection, char* id, ngx_buf_t* b);
static void ngx_http_mongodb_rest_exit_worker(ngx_cycle_t* cycle);
static ngx_int_t ngx_http_mongodb_rest_init_worker(ngx_cycle_t* cycle);
//connection shared by requests for a single worker
mongo_connection cached_connection[1];
typedef struct {
ngx_str_t db;
ngx_str_t collection;
} ngx_http_mongodb_rest_loc_conf_t;
static ngx_command_t ngx_http_mongodb_rest_commands[] = {
{
ngx_string("mongodb_rest"),
NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
ngx_http_mongodb_rest,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_mongodb_rest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_mongodb_rest_create_loc_conf,
ngx_http_mongodb_rest_merge_loc_conf
};
ngx_module_t ngx_http_mongodb_rest_module = {
NGX_MODULE_V1,
&ngx_http_mongodb_rest_module_ctx,
ngx_http_mongodb_rest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
ngx_http_mongodb_rest_init_worker,
NULL,
NULL,
ngx_http_mongodb_rest_exit_worker,
NULL,
NGX_MODULE_V1_PADDING
};
static char* ngx_http_mongodb_rest(ngx_conf_t* cf, ngx_command_t* command, void* void_conf){
ngx_http_mongodb_rest_loc_conf_t *mr_conf = void_conf;
ngx_http_core_loc_conf_t* core_conf;
ngx_str_t *value;
core_conf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
core_conf->handler = ngx_http_mongodb_rest_handler;
value = cf->args->elts;
mr_conf->db = value[1];
mr_conf->collection = value[2];
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_mongodb_rest_handler(ngx_http_request_t* r){
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_mongodb_rest_loc_conf_t *mr_conf;
char id[100];
mr_conf = ngx_http_get_module_loc_conf(r, ngx_http_mongodb_rest_module);
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* set the headers of the response */
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_type.len = sizeof(CONTENT_TYPE) - 1;
r->headers_out.content_type.data = (u_char *) CONTENT_TYPE;
ngx_http_send_header(r);
/* get id */
sscanf((char*)r->uri.data, "/%s", id);
/* get the result and write it to the buffer */
get(&mr_conf->db, &mr_conf->collection, id, b);
b->memory = 1; /* content is in read-only memory */
/* (i.e., filters should copy it rather than rewrite in place) */
b->last_buf = 1; /* there will be no more buffers in the request */
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static void* ngx_http_mongodb_rest_create_loc_conf(ngx_conf_t* conf){
ngx_http_mongodb_rest_loc_conf_t* mr_conf;
mr_conf = ngx_pcalloc(conf->pool, sizeof(ngx_http_mongodb_rest_loc_conf_t));
return mr_conf;
}
static char* ngx_http_mongodb_rest_merge_loc_conf(ngx_conf_t* cf, void* void_parent, void* void_child){
ngx_http_mongodb_rest_loc_conf_t *parent = void_parent;
ngx_http_mongodb_rest_loc_conf_t *child = void_child;
ngx_conf_merge_str_value(child->db, parent->db, NULL);
ngx_conf_merge_str_value(child->collection, parent->collection, NULL);
return NGX_CONF_OK;
}
/* mongodb functions */
static ngx_int_t log_mongo_error(ngx_log_t *log, mongo_conn_return status);
void to_json(char *buf, const char * data, int depth);
static void get(ngx_str_t *db, ngx_str_t *collection, char* id, ngx_buf_t *b){
bson_buffer bb;
bson obj;
bson cond;
char result[1000] = "";
char ns[1000];
//get the query
bson_buffer_init(&bb);
bson_append_string(&bb, "_id", id);
bson_from_buffer(&cond, &bb);
sprintf(ns, "%s.%s", db->data, collection->data);
if(!mongo_find_one(cached_connection, ns, &cond, 0, &obj)){
strcpy(result, "{'error':'record not found'}");
} else {
to_json(result, obj.data, 10);
}
b->pos = (u_char*)result; /* address of the first position of the data */
b->last = (u_char*)result + strlen(result); /* address of the last position of the data */
// destroy cond bb and other stuff
bson_destroy(&cond);
bson_destroy(&obj);
}
static ngx_int_t ngx_http_mongodb_rest_init_worker(ngx_cycle_t* cycle) {
mongo_connection_options opts[1];
mongo_conn_return status;
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,"WORKER INIT");
strcpy( opts->host , MONGO_HOST);
opts->port = MONGO_PORT;
status = mongo_connect( cached_connection, opts );
if (status != mongo_conn_success){
return log_mongo_error(cycle->log, status);
}
return NGX_OK;
}
static void ngx_http_mongodb_rest_exit_worker(ngx_cycle_t* cycle) {
mongo_disconnect(cached_connection);
mongo_destroy(cached_connection);
}
static ngx_int_t log_mongo_error(ngx_log_t *log, mongo_conn_return status){
switch (status) {
case mongo_conn_success:
return NGX_OK;
case mongo_conn_bad_arg:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Bad Arguments");
return NGX_ERROR;
case mongo_conn_no_socket:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: No Socket");
return NGX_ERROR;
case mongo_conn_fail:
ngx_log_error(NGX_LOG_ERR, log, 0, "Mongo Exception: Connection Failure 127.0.0.1:27017;");
return NGX_ERROR;
case mongo_conn_not_master:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Not Master");
return NGX_ERROR;
default:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Unknown Error");
return NGX_ERROR;
}
}
void to_json(char *buf, const char * data, int depth){
int array_count = 0;
int object_count = 0;
bson_iterator i;
const char * key;
char oidhex[25];
bson_iterator_init( &i , data );
sprintf(buf+strlen(buf),"{");
while ( bson_iterator_next( &i ) ){
bson_type t = bson_iterator_type( &i );
if ( t == 0 )
break;
key = bson_iterator_key( &i );
if(object_count > 0){sprintf(buf+strlen(buf),",");}
else{object_count=1;}
sprintf(buf+strlen(buf), "\"%s\":" , key );
switch ( t ){
case bson_int:
sprintf(buf+strlen(buf), "%d" , bson_iterator_int( &i ) );
break;
case bson_double:
sprintf(buf+strlen(buf), "%f" , bson_iterator_double( &i ) );
break;
case bson_bool:
sprintf(buf+strlen(buf), "%s" , bson_iterator_bool( &i ) ? "true" : "false" );
break;
case bson_string:
sprintf(buf+strlen(buf), "\"%s\"" , bson_iterator_string( &i ) );
break;
case bson_null:
sprintf( buf+strlen(buf),"null" );
break;
case bson_oid:
bson_oid_to_string(bson_iterator_oid(&i), oidhex);
sprintf(buf+strlen(buf), "%s" , oidhex );
break;
case bson_object:
to_json(buf, bson_iterator_value( &i ) , depth + 1 );
break;
case bson_array:
sprintf(buf+strlen(buf), "[" );
bson_iterator j;
bson_iterator_init( &j , bson_iterator_value(&i) );
array_count =0;
while( bson_iterator_next(&j)){
if(array_count > 0){sprintf(buf+strlen(buf),",");}
else{array_count=1;}
to_json(buf, bson_iterator_value( &j ) , depth + 1 );
}
sprintf(buf+strlen(buf), "]" );
break;
default:
fprintf( stderr , "can't print type : %d\n" , t );
}
}
sprintf(buf+strlen(buf),"}");
}