Skip to content

Commit

Permalink
chore: add postgres and config with docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
JDIZM committed Jan 2, 2024
1 parent edb7685 commit b13dcb4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
11 changes: 9 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
NODE_ENV=development

###
## prisma
## Database
###
DATABASE_URL=mongodb://root:example@localhost:27017/dbname
# replace with host.docker.internal if using docker
# POSTGRES_HOST=host.docker.internal
POSTGRES_HOST=localhost
POSTGRES_USER=postgres
POSTGRES_PASSWORD=example
POSTGRES_DB=test
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use postgres/example user/password credentials
version: "3.1"

services:
db:
image: postgres
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- 5432:5432
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
driver: local
18 changes: 18 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import dotenv from "dotenv";

const ENV = process.env.NODE_ENV ?? "development";

console.log("mode:", ENV);

if (ENV !== "production") {
dotenv.config();
}

export const config = {
env: ENV,
port: process.env.PORT || 3000,
db_host: process.env.POSTGRES_HOST || "localhost",
db_user: process.env.POSTGRES_USER || "postgres",
db_password: process.env.POSTGRES_PASSWORD || "postgres",
db_name: process.env.POSTGRES_DB || "test"
};
13 changes: 4 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import express from "express";
import dotenv from "dotenv";
import { config } from "./config.js";
import { pinoHttp } from "pino-http";
import { routes } from "./routes/index.js";

const { logger } = pinoHttp();

const env = process.env.NODE_ENV || "development";
const port = process.env.PORT || 3000;

if (env !== "production") {
dotenv.config();
}
console.log("config", config);

const app = express();

Expand All @@ -22,6 +17,6 @@ app.use(

routes(app);

app.listen(port, () => {
console.log(`[server]: Server is running at http://localhost:${port}`);
app.listen(config.port, () => {
console.log(`[server]: Server is running at http://localhost:${config.port}`);
});

0 comments on commit b13dcb4

Please sign in to comment.