Skip to content

Commit a239b17

Browse files
committed
POC: NJS Playground
1 parent d30f27f commit a239b17

File tree

17 files changed

+364
-0
lines changed

17 files changed

+364
-0
lines changed

assets/css/v2/style.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,3 +1429,30 @@ hr {
14291429
visibility: hidden;
14301430
}
14311431
}
1432+
1433+
/* NJS Hackathon stuff */
1434+
.request-builder {
1435+
height: 20rem;
1436+
width: var(--grid-content);
1437+
border: 1px solid #cccccc;
1438+
}
1439+
1440+
.njs-function {
1441+
height: 20rem;
1442+
width: var(--grid-content);
1443+
border: 1px solid #cccccc;
1444+
}
1445+
1446+
#run-btn {
1447+
padding: 8px 16px;
1448+
font-size: 16px;
1449+
}
1450+
1451+
.njs-output {
1452+
padding: var(--flow-gap);
1453+
background-color: #fff;
1454+
white-space: pre-wrap;
1455+
min-height: 40px;
1456+
border: 1px solid #ccc;
1457+
font-family: monospace;
1458+
}

exampleSite/content/njs/_index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
description: NJS Playground
3+
title: NJS Playground
4+
weight: 100
5+
---
6+
7+
Welcome to the NJS playground!
8+
9+
Below you will find NJS examples, which you can tweak and run in your browser.
10+
11+
12+
- [Basic Example](basic-example) - Return "Hello!" for all requests
13+
- [Echo Server](echo-server) - Returns a `JSON` version of the request you just sent
14+
- [Magic AI](magic-ai-thing) - WIP - Something to do with AI
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
description: Basic Example
3+
title: Basic Example
4+
weight: 100
5+
---
6+
7+
8+
To use njs in nginx:
9+
- [install](https://nginx.org/en/docs/njs/install.html) njs scripting language
10+
- create an njs script file, for example, http.js. See [reference](Reference) for the list of njs properties and methods.
11+
12+
```nginx
13+
load_module modules/ngx_http_js_module.so;
14+
15+
events {}
16+
17+
http {
18+
js_import http.js;
19+
20+
server {
21+
listen 8000;
22+
23+
location / {
24+
js_content http.hello;
25+
}
26+
}
27+
}
28+
```
29+
30+
31+
{{< njs-playground exampleRequest=exampleRequest.js exampleNJS=greet.js fnName=hello >}}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
description: Echo Server
3+
title: Echo Server
4+
weight: 100
5+
---
6+
7+
## Basic Example
8+
To use njs in nginx:
9+
- [install](https://nginx.org/en/docs/njs/install.html) njs scripting language
10+
- create an njs script file, for example, http.js. See [reference](Reference) for the list of njs properties and methods.
11+
12+
```nginx
13+
load_module modules/ngx_http_js_module.so;
14+
15+
events {}
16+
17+
http {
18+
js_import http.js;
19+
20+
server {
21+
listen 8000;
22+
23+
location /echo {
24+
js_content http.echo;
25+
}
26+
}
27+
}
28+
```
29+
30+
31+
{{< njs-playground exampleRequest=exampleRequestEcho.js exampleNJS=echo.js fnName=echo >}}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
description: Magic AI Thing
3+
title: Magic AI Thing
4+
weight: 100
5+
---
6+
7+
## Basic Example
8+
To use njs in nginx:
9+
- [install](https://nginx.org/en/docs/njs/install.html) njs scripting language
10+
- create an njs script file, for example, http.js. See [reference](Reference) for the list of njs properties and methods.
11+
12+
```nginx
13+
load_module modules/ngx_http_js_module.so;
14+
15+
events {}
16+
17+
http {
18+
js_import http.js;
19+
20+
server {
21+
listen 8000;
22+
23+
location / {
24+
js_content http.hello;
25+
}
26+
}
27+
}
28+
```
29+
30+
31+
{{< njs-playground "example_request.js" >}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
---
3+
description: Callout - Detail
4+
title: Callout - Detail
5+
weight: 100
6+
---
7+
8+
9+
<details open>
10+
<summary>example nginx-agent.conf</summary>
11+
12+
{{<note>}}
13+
There was a bug which broke padding for blockquotes in details view.
14+
{{</note>}}
15+
16+
And another note
17+
18+
{{<note>}}
19+
In the following example `nginx-agent.conf` file, you can change the `server.host` and `server.grpcPort` to connect to the control plane.
20+
{{</note>}}
21+
22+
And a warning
23+
24+
{{<warning>}}
25+
This is a Warning callout. There was previously a bug with **bold text** that we should be aware of and continue to check for. This callout was invoked with the `<warning>` shortcode. It has no custom title.
26+
{{</warning>}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
description: Table
3+
title: Table
4+
weight: 100
5+
---
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: Table - Bootstrap
3+
title: Table - Bootstrap
4+
weight: 100
5+
---
6+
7+
8+
{{< bootstrap-table "table table-striped table-bordered" >}}
9+
10+
| Field | Datatype | Possible Values | Description | Required | Default |
11+
|--------------|----------|---------------------|----------------------------------------------------|----------|-----------------------|
12+
| `users.id` | integer | A unique int >= 1 | {{ <note> }}Description for value. {{ </note> }} | Yes | System assigned |
13+
| `users.name` | string | Example: `Jane Doe` | A short description of what the field is used for. | Yes | Add the default value |
14+
| `user.age` | integer | 1–110 | Description for the value | Yes | |
15+
16+
{{< /bootstrap-table >}}

exampleSite/layouts/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ <h1>Homepage Example</h1>
99
<li>
1010
<a href="{{ relref . "test-product" }}">Test Product</a>
1111
</li>
12+
<li>
13+
<a href="{{ relref . "njs" }}">njs-playground</a>
14+
</li>
1215
</ul>
1316
</p>
1417

exampleSite/static/njs/echo.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function echo(r) {
2+
const {
3+
method,
4+
uri,
5+
args,
6+
headersIn,
7+
remoteAddress,
8+
httpVersion,
9+
requestText,
10+
} = r;
11+
12+
// Get request body (if applicable)
13+
let body = "";
14+
if (requestBuffer !== "" || !Array.isArray(requestBuffer)) {
15+
body = Buffer.from(requestBuffer).toString();
16+
}
17+
18+
// Construct the echo response (JSON format)
19+
const response = {
20+
method,
21+
uri,
22+
args,
23+
headers: headersIn,
24+
body: requestText,
25+
client: remoteAddress,
26+
version: httpVersion,
27+
};
28+
29+
r.return(200, JSON.stringify(response));
30+
}
31+
32+
// uncomment for us in NJS
33+
// export default { echo }

0 commit comments

Comments
 (0)