Skip to content

Commit

Permalink
feat: leverage .env file (#42)
Browse files Browse the repository at this point in the history
* feat: leverage .env file

- prevent user from modifying docker-compose.yml
- prevent user from building db url by itself, thanks to dotenv-expand

* refactor: expose db port only to other services

* refactor: expose db port only to other services

---------

Co-authored-by: João Lucas <[email protected]>
  • Loading branch information
corno-manso and dev-stupid-codes committed Feb 18, 2024
1 parent b43e5b2 commit 9031db0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
11 changes: 10 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
DATABASE_URL=postgresql:https://johndoe:randompassword@localhost:5432/mydb?schema=public
PORT=3000
SALT="this is a very insecure salt, change it"

DB_NAME=mydb
DB_PROVIDER=postgres
DB_HOST=db # db refers to the name of the service in docker-compose.yml
DB_USER=johndoe
DB_PASSWORD=randompassword

# do not modify. copy as-is. dotenv-expand will replace the variables
DATABASE_URL=${DB_PROVIDER}:https://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432?schema=public

MAIL_ENABLED=false
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=465
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ Right now, my instance is using PostgreSQL on Vercel. However, it can be run usi

### .env Configuration

`DATABASE_URL` needs to point to a running SQL database. It uses PostgreSQL by default, but can be changed to MySQL or SQLite by modifying the provider in [schema.prisma](src/lib/server/prisma/schema.prisma).
- `DB_NAME` is the database name;
- `DB_PROVIDER` uses 'postgres' by default, but can be changed to MySQL or SQLite.
- `DB_HOST` database host (defaults to 'db', but can be changed to aything like localhost)
- `DB_USER` database user
- `DB_PORT` database port 5432
- `DB_PASSWORD` the database user password
- `DATABASE_URL` you don't need to modify this variable (thanks to dotenv-expand). keep it though!

Remember to modify `SALT` to something secure if you plan on using user accounts.

Expand Down
31 changes: 21 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
version: '3.3'
networks:
yabin-network:
name: yabin-network
ipam:
driver: default
services:
yabin:
container_name: yabin
ports:
- '3000:3000'
- '${PORT:-3000}:${PORT:-3000}'
image: 'yureien/yabin:latest'
environment:
- DATABASE_URL=postgresql:https://user:pass@db:5432/yabin?schema=public
env_file: .env
depends_on:
- db
networks:
- yabin-network
db:
container_name: yabin-db
restart: always
image: postgres:15-alpine
env_file: .env
expose:
- '5432'
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: yabin
POSTGRES_USER: ${DB_USER:-yabin_user}
POSTGRES_PASSWORD: ${DB_USER_PASS:-123}
POSTGRES_DB: ${DB_NAME:-yabin_db}
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- db_data:/var/lib/postgresql/data:Z
restart: always
volumes:
db_data: {}
- ./db-data:/var/lib/postgresql/data
networks:
- yabin-network
2 changes: 1 addition & 1 deletion src/lib/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ generator client {
}

datasource db {
provider = "postgresql"
provider = env("DB_PROVIDER")
url = env("DATABASE_URL")
}

Expand Down

0 comments on commit 9031db0

Please sign in to comment.