Skip to content

Commit 144dc95

Browse files
authored
Apply Nginx as a caching proxy to serve SSE requests (buerokratt#380)
* Add nginx service * Handle SSE connection in nginx * Add node server to server SSEs * Rename opensearch index * Add notification service * Update opensearch index * Create notification backend * Serve sse and connect to OpenSearch * Remove console.log * Remove timestamp filtering * Solve OpenSearch update conflict * Only send payload back to notification listeners * Change pyload type to obect * Refactor & clean up * Remove console.log * Add nginx mapping for OpenSearch
1 parent de1fb28 commit 144dc95

18 files changed

+1450
-40
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/tim-db
44
.DS_Store
55
/loba
6+
node_modules

DSL.OpenSearch/fieldMappings/sse-events.json DSL.OpenSearch/fieldMappings/notifications.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"mappings": {
33
"properties": {
4-
"event_id": {
4+
"channelId": {
55
"type": "keyword"
66
},
7-
"event_type": {
8-
"type": "keyword"
9-
},
10-
"data": {
11-
"type": "text"
7+
"payload": {
8+
"type": "object"
129
},
1310
"timestamp": {
14-
"type": "date"
11+
"type": "long"
12+
},
13+
"sentTo": {
14+
"type": "keyword"
1515
}
1616
}
1717
}

DSL.OpenSearch/init.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#clear
22
curl -XDELETE 'http://localhost:9200/*' -u admin:admin --insecure
33

4-
#SSE events
5-
curl -H "Content-Type: application/x-ndjson" -X PUT "http://localhost:9200/sse-events" -ku admin:admin --data-binary "@fieldMappings/sse-events.json"
6-
curl -H "Content-Type: application/x-ndjson" -X PUT "http://localhost:9200/sse-events/_bulk" -ku admin:admin --data-binary "@mock/sse-events.json"
7-
curl -L -X POST 'http://localhost:9200/_scripts/sse-events-by-type' -H 'Content-Type: application/json' -H 'Cookie: customJwtCookie=test' --data-binary "@templates/sse-events-by-type.json"
4+
#notifications
5+
curl -H "Content-Type: application/x-ndjson" -X PUT "http://localhost:9200/notifications" -ku admin:admin --data-binary "@fieldMappings/notifications.json"
6+
curl -H "Content-Type: application/x-ndjson" -X PUT "http://localhost:9200/notifications/_bulk" -ku admin:admin --data-binary "@mock/notifications.json"
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{"index":{"_id":"1"}}
2+
{"channelId": "b794c417-7f90-4e4e-b7e5-16f41614c4fe","payload": {"content":"This is a notification for user 1"},"timestamp": 1801371325497, "sentTo": []}
3+
{"index":{"_id":"2"}}
4+
{"channelId": "9f6d91f8-ee5a-4d68-9c79-f71f5cda2c57","payload": {"content":"Notification for user 2"},"timestamp": 1801371325597, "sentTo": []}
5+
{"index":{"_id":"3"}}
6+
{"channelId": "b794c417-7f90-4e4e-b7e5-16f41614c4fe","payload": {"content":"Another notification for user 1"},"timestamp": 1801371325697, "sentTo": []}
7+
{"index":{"_id":"4"}}
8+
{"channelId": "11111111-7f90-4e4e-b7e5op-111111111111","payload": {"content":"Again, another notification for user 1"},"timestamp": 1801371325797, "sentTo": []}

DSL.OpenSearch/mock/sse-events.json

-6
This file was deleted.

DSL.OpenSearch/templates/sse-events-by-type.json

-23
This file was deleted.

constants.ini

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ CHATBOT_TIM=http://tim
1818
CHATBOT_TIM_PORT=8085
1919
CHATBOT_OPENSEARCH=http://opensearch-node
2020
CHATBOT_OPENSEARCH_PORT=9200
21+
CHATBOT_NOTIFICATIONS=http://notifications-node
22+
CHATBOT_NOTIFICATIONS_PORT=4040
23+
CHATBOT_NGINX=http://nginx-node
24+
CHATBOT_NGINX_PORT=80

docker-compose.yml

+36
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,42 @@ services:
273273
networks:
274274
- bykstack
275275

276+
notifications-node:
277+
container_name: notifications-node
278+
build:
279+
context: ./notification-server
280+
dockerfile: Dockerfile
281+
ports:
282+
- 4040:4040
283+
depends_on:
284+
- opensearch-node
285+
environment:
286+
OPENSEARCH_PROTOCOL: http
287+
OPENSEARCH_HOST: opensearch-node
288+
OPENSEARCH_PORT: 9200
289+
OPENSEARCH_USERNAME: admin
290+
OPENSEARCH_PASSWORD: admin
291+
PORT: 4040
292+
REFRESH_INTERVAL: 1000
293+
volumes:
294+
- ./notification-server:/app
295+
- ./notification-server/node_modules:/app/node_modules
296+
command: ["npm", "run", "dev"]
297+
networks:
298+
- bykstack
299+
300+
nginx:
301+
image: nginx:latest
302+
container_name: nginx-node
303+
ports:
304+
- "80:80"
305+
volumes:
306+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
307+
depends_on:
308+
- notifications-node
309+
networks:
310+
- bykstack
311+
276312
volumes:
277313
opensearch-data:
278314
networks:

nginx/nginx.conf

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
worker_processes auto;
3+
4+
events {
5+
worker_connections 1024;
6+
}
7+
8+
http {
9+
include mime.types;
10+
default_type application/octet-stream;
11+
sendfile on;
12+
13+
proxy_buffering off;
14+
proxy_request_buffering off;
15+
proxy_http_version 1.1;
16+
keepalive_timeout 65;
17+
keepalive_requests 1000;
18+
19+
server {
20+
listen 80;
21+
22+
location /sse {
23+
proxy_pass http://notifications-node:4040/sse;
24+
25+
proxy_http_version 1.1;
26+
proxy_set_header Upgrade $http_upgrade;
27+
proxy_set_header Connection "upgrade";
28+
proxy_set_header Host $host;
29+
proxy_set_header X-Real-IP $remote_addr;
30+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
31+
proxy_set_header X-Forwarded-Proto $scheme;
32+
33+
proxy_buffering off;
34+
proxy_cache off;
35+
proxy_redirect off;
36+
proxy_read_timeout 24h;
37+
}
38+
39+
location /notifications {
40+
proxy_pass http://opensearch-node:9200/notifications;
41+
42+
proxy_set_header Host $host;
43+
proxy_set_header X-Real-IP $remote_addr;
44+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
45+
proxy_set_header X-Forwarded-Proto $scheme;
46+
}
47+
}
48+
}

notification-server/.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
OPENSEARCH_PROTOCOL=http
2+
OPENSEARCH_HOST=opensearch-node
3+
OPENSEARCH_PORT=9200
4+
OPENSEARCH_USERNAME=admin
5+
OPENSEARCH_PASSWORD=admin
6+
PORT=4040
7+
REFRESH_INTERVAL=1000

notification-server/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:lts-slim
2+
3+
WORKDIR /app
4+
5+
COPY package.json package-lock.json /app/
6+
7+
RUN npm install
8+
9+
COPY . /app/
10+
11+
EXPOSE 4040
12+
13+
CMD ["npm", "start"]

notification-server/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require('dotenv').config();
2+
const { client } = require('./src/openSearch');
3+
4+
(async () => {
5+
try {
6+
await client.indices.putSettings({
7+
index: 'notifications',
8+
body: {
9+
refresh_interval: '5s',
10+
},
11+
});
12+
13+
require('./src/server');
14+
} catch (error) {
15+
console.error('Error:', error);
16+
}
17+
})();

0 commit comments

Comments
 (0)