Skip to content

Commit a15516f

Browse files
committed
[add] http/api/set_keyval example
1 parent 0e589b6 commit a15516f

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

‎README.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,80 @@ Checking:
11631163
127.0.0.2 [22/Nov/2021:18:20:24 +0000] 1
11641164
127.0.0.2 [22/Nov/2021:18:20:25 +0000] 2
11651165
1166+
NGINX-PLUS API
1167+
--------------
1168+
1169+
Setting keyval using a subrequest [http/api/set_keyval]
1170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1171+
1172+
.. note:: The `keyval <http://nginx.org/en/docs/http/ngx_http_keyval_module.html#keyval>`_, `api <http://nginx.org/en/docs/http/ngx_http_api_module.html#api>`_ and `keyval_zone <http://nginx.org/en/docs/http/ngx_http_keyval_module.html#keyval_zone>`_ directives are available as part of our `commercial subscription <https://www.nginx.com/products/nginx/>`_.
1173+
1174+
nginx.conf:
1175+
1176+
.. code-block:: nginx
1177+
1178+
...
1179+
1180+
http {
1181+
js_path "/etc/nginx/njs/";
1182+
1183+
js_import main from http/api/set_keyval.js;
1184+
1185+
keyval_zone zone=foo:10m;
1186+
1187+
server {
1188+
listen 80;
1189+
1190+
location /keyval {
1191+
js_content main.set_keyval;
1192+
}
1193+
1194+
location /api {
1195+
internal;
1196+
api write=on;
1197+
}
1198+
1199+
location /api/ro {
1200+
api;
1201+
}
1202+
}
1203+
1204+
example.js:
1205+
1206+
.. code-block:: js
1207+
1208+
async function set_keyval(r) {
1209+
let method = r.args.method ? r.args.method : 'POST';
1210+
let res = await r.subrequest('/api/7/http/keyvals/foo',
1211+
{ method, r.requestBody});
1212+
1213+
if (res.status >= 300) {
1214+
r.return(res.status, res.responseBody);
1215+
return;
1216+
}
1217+
1218+
r.return(200);
1219+
}
1220+
1221+
export default {set_keyval};
1222+
1223+
Checking:
1224+
1225+
.. code-block:: shell
1226+
1227+
curl http://localhost/api/ro/7/http/keyvals/foo
1228+
{}
1229+
curl http://localhost:8000/keyval -d '{"a":1}'
1230+
OK
1231+
curl http://localhost/api/ro/7/http/keyvals/foo
1232+
{"a":"1"}
1233+
curl http://localhost:8000/keyval -d '{"a":2}'
1234+
{"error":{"status":409,"text":"key \"a\" already exists","code":"KeyvalKeyExists"},"request_id":"cbec775883f6b10f2fe79e27d3f249ce","href":"https://nginx.org/en/docs/http/ngx_http_api_module.html"}
1235+
curl http://localhost:8000/keyval?method=PATCH -d '{"a":2}'
1236+
OK
1237+
curl http://localhost:8000/api/ro/7/http/keyvals/foo
1238+
{"a":"2"}
1239+
11661240
Stream
11671241
======
11681242

‎conf/http/api/set_keyval.conf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load_module modules/ngx_http_js_module.so;
2+
3+
events { }
4+
5+
http {
6+
js_path "/etc/nginx/njs/";
7+
8+
js_import main from http/api/set_keyval.js;
9+
10+
keyval_zone zone=foo:10m;
11+
12+
server {
13+
listen 80;
14+
15+
location /keyval {
16+
js_content main.set_keyval;
17+
}
18+
19+
location /api {
20+
internal;
21+
api write=on;
22+
}
23+
24+
location /api/ro {
25+
api;
26+
}
27+
}
28+
}

‎njs/http/api/set_keyval.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
async function set_keyval(r) {
2+
let method = r.args.method ? r.args.method : 'POST';
3+
let res = await r.subrequest('/api/7/http/keyvals/foo',
4+
{ method, r.requestBody});
5+
6+
if (res.status >= 300) {
7+
r.return(res.status, res.responseBody);
8+
return;
9+
}
10+
11+
r.return(200);
12+
}
13+
14+
export default {set_keyval};

0 commit comments

Comments
 (0)