Skip to content

Commit 7ae5e4d

Browse files
committed
add nginx+certbot support
1 parent dc94896 commit 7ae5e4d

File tree

4 files changed

+165
-11
lines changed

4 files changed

+165
-11
lines changed

docker-compose.yml

+42-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ services:
66
- "3000:3000"
77
environment:
88
PGRST_DB_URI: postgres://test_user:123456@db:5432/blog
9-
PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
9+
#PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
10+
PGRST_OPENAPI_SERVER_PROXY_URI: https://domain.com/
1011
PGRST_DB_SCHEMA: public
1112
PGRST_DB_ANON_ROLE: web_anon
1213
PGRST_JWT_SECRET: "varmB8bXwQzn91ZLxHFJhXEy6S3cN3sB"
14+
PGRST_JWT_ROLE_CLAIM_KEY: ".role"
15+
PGRST_SERVER_CORS_ALLOWED_ORIGINS: "https://domain.com,http://domain.com,http://domain.com:8080"
1316
depends_on:
1417
- db
18+
networks:
19+
- app-network
20+
restart: always
1521
db:
1622
image: postgres
1723
ports:
@@ -20,17 +26,23 @@ services:
2026
POSTGRES_DB: blog
2127
POSTGRES_USER: postgres
2228
POSTGRES_PASSWORD: 123456
23-
# Uncomment this if you want to persist the data.
29+
# comment volumes this if you want to no persist the data.
2430
volumes:
2531
- "./pgdata:/var/lib/postgresql/data"
32+
networks:
33+
- app-network
34+
restart: always
2635
swagger:
2736
image: swaggerapi/swagger-ui
2837
ports:
2938
- "8080:8080"
3039
expose:
3140
- "8080"
3241
environment:
33-
API_URL: http://127.0.0.1:3000/
42+
API_URL: https://domain.com
43+
networks:
44+
- app-network
45+
restart: always
3446
socket:
3547
image: node:14
3648
working_dir: /app
@@ -46,3 +58,30 @@ services:
4658
PGUSER: test_user
4759
PGPASSWORD: 123456
4860
PGDATABASE: blog
61+
networks:
62+
- app-network
63+
restart: always
64+
nginx:
65+
image: nginx:latest
66+
volumes:
67+
- ./nginx.conf:/etc/nginx/nginx.conf
68+
- ./letsencrypt:/etc/letsencrypt
69+
- ./certbot/www:/var/www/certbot
70+
ports:
71+
- "80:80"
72+
- "443:443"
73+
depends_on:
74+
- server
75+
- socket
76+
networks:
77+
- app-network
78+
restart: always
79+
certbot:
80+
image: certbot/certbot
81+
command: certonly --webroot --webroot-path=/var/www/certbot/ -d domain.com
82+
volumes:
83+
- ./letsencrypt:/etc/letsencrypt
84+
- ./certbot/www:/var/www/certbot
85+
networks:
86+
- app-network
87+
restart: no

nginx.conf

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
include /etc/nginx/mime.types;
7+
default_type application/octet-stream;
8+
9+
server {
10+
listen 80;
11+
server_name domain.com;
12+
13+
location /.well-known/acme-challenge/ {
14+
root /var/www/certbot;
15+
try_files $uri =404;
16+
}
17+
18+
location / {
19+
return 301 https://$host$request_uri;
20+
}
21+
}
22+
23+
# First run disable this block
24+
server {
25+
listen 443 ssl;
26+
server_name domain.com;
27+
28+
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
29+
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
30+
31+
ssl_protocols TLSv1.2 TLSv1.3;
32+
ssl_ciphers HIGH:!aNULL:!MD5;
33+
34+
location / {
35+
proxy_pass http://server:3000;
36+
proxy_set_header Host $host;
37+
proxy_set_header X-Real-IP $remote_addr;
38+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
39+
proxy_set_header X-Forwarded-Proto $scheme;
40+
}
41+
42+
location /socket.io/ {
43+
proxy_pass http://socket:4000;
44+
proxy_http_version 1.1;
45+
proxy_set_header Upgrade $http_upgrade;
46+
proxy_set_header Connection "upgrade";
47+
}
48+
}
49+
}

socket/index.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ pgClient.connect();
2424
pgClient.query('LISTEN category_changes');
2525

2626
pgClient.on('notification', (msg) => {
27-
const payload = JSON.parse(msg.payload);
28-
io.emit('categoryChanges', payload);
27+
if (msg.payload != '') {
28+
const payload = JSON.parse(msg.payload);
29+
io.emit('categoryChanges', payload);
30+
}
2931
});
3032

3133
io.on('connection', (socket) => {
@@ -35,16 +37,17 @@ io.on('connection', (socket) => {
3537
console.log(`İstemci ${categoryId} ID'sine sahip kayıtları dinliyor.`);
3638

3739
pgClient.on('notification', (msg) => {
38-
const payload = JSON.parse(msg.payload);
39-
console.log(payload);
40-
if (payload.id === parseInt(categoryId, 10)) {
41-
socket.emit(`categoryChange-${categoryId}`, payload);
40+
if (msg.payload != '') {
41+
const payload = JSON.parse(msg.payload);
42+
console.log(payload);
43+
if (payload.id === parseInt(categoryId, 10)) {
44+
socket.emit(`categoryChange-${categoryId}`, payload);
45+
}
4246
}
4347
});
4448
});
4549
});
4650

4751
server.listen(4000, () => {
4852
console.log('Socket.IO sunucusu 4000 portunda çalışıyor');
49-
});
50-
53+
});

socket/test.html

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Socket.IO Test</title>
8+
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
9+
<style>
10+
.status {
11+
display: block;
12+
width: 10px;
13+
height: 10px;
14+
border-radius: 50%;
15+
background-color: red;
16+
}
17+
18+
.status.active {
19+
background-color: green;
20+
}
21+
</style>
22+
</head>
23+
24+
<body>
25+
<h3><span class="status"></span></h3>
26+
<h1>Categories Changes</h1>
27+
<ul id="changes"></ul>
28+
29+
<script>
30+
// Connect Socket.IO
31+
const socket = io('https://domain.com');
32+
33+
// listen 'connect' event
34+
socket.on('connect', function () {
35+
const status = document.querySelector('.status');
36+
status.classList.add('active');
37+
});
38+
39+
// listen 'disconnect' event
40+
socket.on('disconnect', function () {
41+
const status = document.querySelector('.status');
42+
status.classList.remove('active');
43+
});
44+
45+
// listen 'categoryChanges' event
46+
socket.on('categoryChanges', function (data) {
47+
const changesList = document.getElementById('changes');
48+
const newItem = document.createElement('li');
49+
newItem.textContent = JSON.stringify(data);
50+
changesList.appendChild(newItem);
51+
});
52+
53+
// listen 'categoryChange' event with categoryId
54+
let categoryId = 5
55+
socket.emit('listenCategoryId', categoryId);
56+
socket.on(`categoryChange-${categoryId}`, function (data) {
57+
const textContent = JSON.stringify(data);
58+
alert(textContent);
59+
});
60+
</script>
61+
</body>
62+
63+
</html>

0 commit comments

Comments
 (0)