Skip to content

Commit 2c5e88b

Browse files
committed
init boilerplate postgres
0 parents  commit 2c5e88b

File tree

6 files changed

+2165
-0
lines changed

6 files changed

+2165
-0
lines changed

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Postgres + PosgREST + Socket.IO
2+
3+
Document is soon...

docker-compose.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: '3'
2+
services:
3+
server:
4+
image: postgrest/postgrest
5+
ports:
6+
- "3000:3000"
7+
environment:
8+
PGRST_DB_URI: postgres://test_user:123456@db:5432/blog
9+
PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
10+
PGRST_DB_SCHEMA: public
11+
PGRST_DB_ANON_ROLE: web_anon
12+
PGRST_JWT_SECRET: "varmB8bXwQzn91ZLxHFJhXEy6S3cN3sB"
13+
depends_on:
14+
- db
15+
db:
16+
image: postgres
17+
ports:
18+
- "5432:5432"
19+
environment:
20+
POSTGRES_DB: blog
21+
POSTGRES_USER: postgres
22+
POSTGRES_PASSWORD: 123456
23+
# Uncomment this if you want to persist the data.
24+
volumes:
25+
- "./pgdata:/var/lib/postgresql/data"
26+
swagger:
27+
image: swaggerapi/swagger-ui
28+
ports:
29+
- "8080:8080"
30+
expose:
31+
- "8080"
32+
environment:
33+
API_URL: http://127.0.0.1:3000/
34+
socket:
35+
image: node:14
36+
working_dir: /app
37+
volumes:
38+
- ./socket:/app
39+
command: "npm start"
40+
ports:
41+
- "4000:4000"
42+
depends_on:
43+
- db
44+
environment:
45+
PGHOST: db
46+
PGUSER: test_user
47+
PGPASSWORD: 123456
48+
PGDATABASE: blog

socket/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { Client } = require('pg');
2+
const express = require('express');
3+
const http = require('http');
4+
const socketIo = require('socket.io');
5+
6+
const app = express();
7+
const server = http.createServer(app);
8+
const io = socketIo(server, {
9+
cors: {
10+
origin: "*",
11+
methods: ["GET", "POST"],
12+
}
13+
});
14+
15+
const pgClient = new Client({
16+
host: process.env.PGHOST,
17+
user: process.env.PGUSER,
18+
password: process.env.PGPASSWORD,
19+
database: process.env.PGDATABASE,
20+
});
21+
22+
pgClient.connect();
23+
24+
pgClient.query('LISTEN category_changes');
25+
26+
pgClient.on('notification', (msg) => {
27+
const payload = JSON.parse(msg.payload);
28+
io.emit('categoryChanges', payload);
29+
});
30+
31+
io.on('connection', (socket) => {
32+
console.log('Yeni bir istemci bağlandı');
33+
34+
socket.on('listenCategoryId', (categoryId) => {
35+
console.log(`İstemci ${categoryId} ID'sine sahip kayıtları dinliyor.`);
36+
37+
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);
42+
}
43+
});
44+
});
45+
});
46+
47+
server.listen(4000, () => {
48+
console.log('Socket.IO sunucusu 4000 portunda çalışıyor');
49+
});
50+

0 commit comments

Comments
 (0)