Skip to content

Commit 3245146

Browse files
committed
POC: Add auth_request example
1 parent 1cf8881 commit 3245146

File tree

7 files changed

+68
-39
lines changed

7 files changed

+68
-39
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
description: Auth Request
3+
title: Auth Request
4+
weight: 100
5+
---
6+
7+
In this example, when a client sends a request to the `/auth` endpoint, NGINX calls the `choose_upstream` function. This function reads the token from the request's query string and then selects the appropriate backend server based on the token's value. If the token does not match a known value, the function returns a 404 error.
8+
9+
```nginx
10+
load_module modules/ngx_http_js_module.so;
11+
12+
events {}
13+
14+
http {
15+
js_import http.js;
16+
17+
server {
18+
listen 8000;
19+
20+
location /auth {
21+
js_content http.auth;
22+
}
23+
}
24+
}
25+
```
26+
27+
28+
{{< njs-playground exampleNJS=auth_request.js fnName=choose_upstream >}}

exampleSite/content/njs/basic-example.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ title: Basic Example
44
weight: 100
55
---
66

7-
8-
To use njs in nginx:
9-
- [install](https://nginx.org/en/docs/njs/install.html) njs scripting language
7+
In this example, the client sends a request to `/`, which calls the `hello` njs function. By default, the function returns status code `200` and the message `Hello World`.
108

119

1210
```nginx

exampleSite/content/njs/echo-server.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ title: Echo Server
44
weight: 100
55
---
66

7-
This example demonstrates how to create a simple echo endpoint.
8-
The endpoint returns the details of the request sent to NGINX as JSON.
7+
This example shows how to create a simple echo endpoint. The client sends a request to the `/echo` endpoint, which calls the `echo` njs function. By default, this function returns the request details received by NGINX in JSON format.
98

109
```nginx
1110
load_module modules/ngx_http_js_module.so;

exampleSite/content/njs/magic-ai-thing.md

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function choose_upstream(r) {
2+
let backend;
3+
4+
let uriParts = r.headersIn['X-Original-URI'].split('?');
5+
if (uriParts.length < 2) {
6+
return r.return(404);
7+
}
8+
9+
let queryString = uriParts[1];
10+
let args = {};
11+
12+
queryString.split('&').forEach(part => {
13+
let [key, value] = part.split('=');
14+
args[decodeURIComponent(key)] = decodeURIComponent(value || '');
15+
});
16+
17+
switch (args.token) {
18+
case 'A':
19+
backend = '127.0.0.1:8081';
20+
break;
21+
case 'B':
22+
backend = '127.0.0.1:8082';
23+
break;
24+
default:
25+
return r.return(404);
26+
}
27+
28+
r.headersOut['X-backend'] = backend;
29+
return r.return(200, r.headersOut);
30+
}
31+
32+
// uncomment for us in NJS
33+
// export default { choose_upstream };

exampleSite/static/njs/echo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function echo(r) {
2020
version: httpVersion,
2121
};
2222

23-
return r.return(200, JSON.stringify(response));
23+
return r.return(200, response);
2424
}
2525

2626
// uncomment for use in NJS

layouts/partials/njs-playground.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h3>Request Builder</h3>
1212
</select>
1313

1414
<label for="url">URI:</label>
15-
<input id="url" type="text" placeholder="/echo">
15+
<input id="url" type="text" placeholder="/api">
1616

1717
<label for="bodyJSON">JSON Body:</label>
1818
<textarea id="bodyJSON" rows="8" placeholder='{"example":"value"}'></textarea>
@@ -42,6 +42,7 @@ <h3>NJS Function</h3>
4242
const bodyEl = document.getElementById('bodyJSON');
4343
const headersContainerEl = document.getElementById('headers-container');
4444
const addHeaderBtn = document.getElementById('add-header');
45+
4546
// Initialize headers with one empty pair
4647
addHeaderField();
4748

@@ -95,7 +96,7 @@ <h3>NJS Function</h3>
9596
const requestObject = {
9697
method: methodEl.value,
9798
uri: urlEl.value,
98-
headersIn: headers,
99+
headersIn: headers || {},
99100
body: bodyContent
100101
};
101102

@@ -148,6 +149,7 @@ <h3>NJS Function</h3>
148149
...request,
149150
requestText: request.body,
150151
return: (status, value) => (JSON.stringify({status, value})),
152+
headersOut: {},
151153
}
152154
}
153155
{{ .fnName }}(requestConverter(exampleRequest))

0 commit comments

Comments
 (0)