From ea27a93d0896e981d8153d3d9127fc77e0edb51f Mon Sep 17 00:00:00 2001 From: Jake Wheeler Date: Fri, 27 Sep 2024 08:58:49 -0400 Subject: [PATCH] Moving Docker compose and makefile additions into this codebase (#55) --- .gitignore | 1 + README.md | 35 +++++++++++++++++++++++++++++------ docker-compose.yml | 8 +++++--- makefile | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 makefile diff --git a/.gitignore b/.gitignore index 7eeb64f..f7d4f24 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # .env +*.dump # Binaries for programs and plugins *.exe diff --git a/README.md b/README.md index 5d24d69..778b772 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,35 @@ PHIN VADS written in Go. Load `phinvads.dump` into a PostgreSQL database using ` ## Dev setup -1. Clone this repo: +Clone the repo: - ```bash - git clone https://github.com/skylight-hq/phinvads-go.git - cd phinvads-go - ``` +```bash +git clone https://github.com/skylight-hq/phinvads-go.git +cd phinvads-go +``` + +### direnv setup and configuration + +1. [Install direnv (`brew install direnv`)](https://direnv.net/docs/installation.html) +2. [Add a hook for your shell](https://direnv.net/docs/hook.html) +3. Restart your terminal +4. Run `direnv allow` in the project directory + +### Database setup + +#### Running Postgres in Docker -1. Install and run [PostgreSQL](https://www.postgresql.org/download/), or just run `docker compose up` +Download and install [Docker](https://www.docker.com/products/docker-desktop/) if you don't already have it installed. + +1. Place `phinvads.dump` into the top level of your project directory (`/phinvads-go`) +2. Navigate to the project directory and start your PostgreSQL database with `make start` +3. Run `make refresh` to create the `phinvads` database and load in the data + +When you want to shut down your database environment, run `make stop`. + +#### Running Postgres locally + +1. Install and run [PostgreSQL](https://www.postgresql.org/download/) 1. Create an empty database: ```bash @@ -24,6 +45,8 @@ PHIN VADS written in Go. Load `phinvads.dump` into a PostgreSQL database using ` pg_restore -d phinvads --no-owner --role=$(whoami) phinvads.dump ``` +### Application setup + 1. Install [Go](https://go.dev/doc/install) 1. Install [air](https://github.com/air-verse/air): diff --git a/docker-compose.yml b/docker-compose.yml index 3488562..eaa1b59 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,12 +2,14 @@ services: db: image: postgres:alpine environment: - POSTGRES_DB: phinvads + POSTGRES_DB: ${DB_NAME} + POSTGRES_PASSWORD: ${DB_PASSWORD} ports: - - 5433:5432 + - ${DB_PORT}:5432 restart: on-failure volumes: - db-data:/var/lib/postgresql/data + - ./:/app volumes: - db-data: + db-data: \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..9118c17 --- /dev/null +++ b/makefile @@ -0,0 +1,20 @@ +DB_SERVICE = db # Name of the PostgreSQL service in docker-compose +DUMP_FILE = /app/phinvads.dump # Name of your SQL dump file (within db container) + +start: + @echo "Starting database container..." + docker compose up -d + +stop: + @echo "Stopping database container..." + docker compose down + +load: + @echo "Inserting data from $(DUMP_FILE) into $(DB_NAME)..." + docker compose exec -T $(DB_SERVICE) pg_restore -U $(DB_USER) -x --no-owner -d $(DB_NAME) $(DUMP_FILE) + +refresh: + @echo "Running database refresh..." + docker compose exec -T $(DB_SERVICE) psql -U $(DB_USER) -c "DROP DATABASE IF EXISTS $(DB_NAME) WITH (FORCE)" + docker compose exec -T $(DB_SERVICE) psql -U $(DB_USER) -c 'CREATE DATABASE $(DB_NAME)' + $(MAKE) load \ No newline at end of file