Skip to content

Commit

Permalink
add verbosity to pg connections for the backend app (#165)
Browse files Browse the repository at this point in the history
Signed-off-by: Baptiste Collard <[email protected]>
  • Loading branch information
bcollard authored Jul 25, 2024
1 parent f3d69a2 commit db9e133
Show file tree
Hide file tree
Showing 8 changed files with 1,200 additions and 347 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/docker-build-push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: docker-build-push

on:
push

jobs:
docker:
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_API_KEY }}

- name: Build and push backend
uses: docker/build-push-action@v5
with:
file: ./api/Dockerfile
context: ./api
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ vars.DOCKER_REPOSITORY || vars.DOCKER_USERNAME }}/kuma-demo-be:latest

- name: Build and push frontend
uses: docker/build-push-action@v5
with:
file: ./app/Dockerfile
context: ./app
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ vars.DOCKER_REPOSITORY || vars.DOCKERHUB_USERNAME }}/kuma-demo-fe:latest

- name: Build and push postgres
uses: docker/build-push-action@v5
with:
file: ./api/db/postgresql/Dockerfile
context: ./api/db/postgresql
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ vars.DOCKERHUB_REPOSITORY || vars.DOCKERHUB_USERNAME }}/kuma-demo-pg:latest

- name: Build and push redis
uses: docker/build-push-action@v5
with:
file: ./api/db/redis/Dockerfile
context: ./api/db/redis
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ vars.DOCKER_REPOSITORY || vars.DOCKER_USERNAME }}/kuma-demo-redis:latest
4 changes: 3 additions & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# docker build . -t kvn0218/kuma-demo-be:latest
# docker push kvn0218/kuma-demo-be:latest

FROM node:lts-alpine
FROM node:21-alpine

WORKDIR /usr/src/app

Expand All @@ -15,4 +15,6 @@ EXPOSE 3001

RUN apk add curl

ENV NODE_ENV=production

CMD [ "npm", "start" ]
26 changes: 20 additions & 6 deletions api/app/postgresql.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
const items = require("../db/items.json");
const { Pool } = require("pg");
const pino = require('pino');
const logger = pino({ name: 'kuma-backend-pg', level: process.env.PINO_LOG_LEVEL || 'info' });
const dns = require('dns');
const dnsPromises = dns.promises;

const pool = new Pool({
user: process.env.POSTGRES_USER || "kumademo",
host: process.env.POSTGRES_HOST || "localhost",
database: process.env.POSTGRES_DB || "kumademo",
password: process.env.POSTGRES_PASSWORD || "kumademo",
port: process.env.POSTGRES_PORT_NUM || 5432, //POSTGRES_PORT environmental variable is taken on K8S
idleTimeoutMillis: process.env.POSTGRES_IDLE_TIMEOUT || 10000,
connectionTimeoutMillis: process.env.POSTGRES_CONNECTION_TIMEOUT || 2000,
});

const dnsOptions = {
all: true,
};

pool.on("error", (err, clients) => {
console.error("Unexpected error on idle client", err);
logger.error('error on postgresql pool', err);
process.exit(-1);
});

const search = async (itemName) => {
await dnsPromises.lookup(pool.options.host, dnsOptions).then(async (result) => {
await logger.info('DNS lookup for host ' + pool.options.host + ': %j', result);
});
return await pool.query(
`SELECT data FROM marketItems WHERE name ILIKE '%${itemName}%'`
);
`SELECT data FROM marketItems WHERE name ILIKE '%${itemName}%'`
);
};

const importData = () => {
Expand All @@ -42,16 +55,17 @@ const importData = () => {
});
await client.query("COMMIT");
} catch (e) {
console.log("Error");
logger.error('search error: ', err);
await client.query("ROLLBACK");
throw e;
} finally {
console.log("Release");
logger.debug("Release");
client.release();
}
})().catch((e) => console.error(e.stack));
})().catch((e) => logger.error(e.stack));
};


module.exports = Object.assign({
search,
importData,
Expand Down
6 changes: 4 additions & 2 deletions api/db/postgresql/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# docker build . -t kvn0218/postgres:latest
# docker push kvn0218/postgres:latest

FROM postgres:alpine
FROM postgres:10-alpine

RUN mkdir -p /tmp/psql_data/

RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
# commented out the following line on 2/12/2024 because it was source of
# clunky builds between the docker legacy builder and docker's new buildkit
# RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf

# Expose the PostgreSQL port
EXPOSE 5432
Expand Down
23 changes: 20 additions & 3 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const redis = require("./app/redis");
const postgresql = require("./app/postgresql");
const promBundle = require("express-prom-bundle");
const pino = require('pino');
const logger = pino({ name: 'kuma-backend', level: process.env.PINO_LOG_LEVEL || 'info' });

const express = require("express");
const app = express();
Expand All @@ -27,6 +29,7 @@ app.use((req, res, next) => {

app.get("/", (req, res) => {
res.set(req.headers);
logger.info('GET /');
res.send(
"Hello World! Marketplace with sales and reviews made with <3 by the OCTO team at Kong Inc."
);
Expand All @@ -39,17 +42,29 @@ app.post("/upload", async (req, res) => {
});

app.get("/items", async (req, res) => {
postgresql
await logger.info('get on /items');
await postgresql
.search(req.query.q)
.then(async (results) => {
if (results === undefined) {
logger.error('result is undefined');
}
if (results.rows === undefined) {
logger.error('result.rows is undefined');
}
if (results.rows.length === 0) {
logger.warn('no results found');
} else {
logger.info('row count: ' + results.rowCount);
}
if (specialOffers == true) {
res.send(addOffer(results.rows));
} else {
res.send(results.rows);
}
})
.catch((err) => {
console.log('catch err: ' + err);
logger.error('catch err: ' + err);;
res.send(err);
});
});
Expand All @@ -69,14 +84,16 @@ const addOffer = (arr) => {
};

app.get("/items/:itemIndexId/reviews", (req, res) => {
logger.info('get on /items/.../reviews');
redis
.search(`${req.params.itemIndexId}`, req.headers)
.then((results) => {
res.send(results);
})
.catch((err) => {
logger.error('catch err: ' + err);;
res.send(err);
});
});

app.listen(app.get("port"));
app.listen(app.get("port"), "0.0.0.0");
Loading

0 comments on commit db9e133

Please sign in to comment.