fix: dockerizing

This commit is contained in:
nftchance 2022-10-10 22:25:25 -05:00
parent df737a7b53
commit 62a5a25c32
6 changed files with 58 additions and 36 deletions

2
api/.gitignore vendored
View File

@ -1,5 +1,7 @@
/.pnp
database/
venv/
cache
*.pyc

6
api/Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM python:3.9
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

View File

@ -4,38 +4,20 @@ The API serves and stores related ownership of Orgs and Badgers to users, as wel
Authentication is gated to a wallet address with the use of Sign In With Ethereum. In order for any access to data related to a User's Organizations or Badges, the front end client must have used SIWE to prove their ownership of that address. If ownership of an address is not confirmed, that client will have no access to any data stored in the database.
### Dev Environment
#### Environment Variables
``ALCHEMY_API_KEY=""``
``PINATA_API_KEY=""``
``PINATA_API_SECRET_KEY=""``
## Prerequisites
- fork the repo
- terminal: `cd api`
- install Docker
- terminaL `npm i`
- setup your `.env` (in `~root/contracts/`) to reflect `example.env`
**Important:** If you do not set Pinata keys in your `.env`, you will not be able to upload images to IPFS which will currently cause the API to halt and fail.
#### Running the API
First, set up your virtual environment with:
``python3 -m venv venv``
Mac:
``source venv/bin/activate``
Windows:
``venv/bin/activate``
Install requirements:
``pip install -r requirements.txt``
Once the local PostgreSQL is running and the virtual environment is activated, run
``python manage.py migrate``
to apply the data tables to the database, before finally running ``python manage.py runserver``
to get the API up.
The development environment for the API is built using Docker to minimize the amount of pain experienced when getting things running. Docker is super confusing so if you know how to do this better, please submit a PR <3.
#### PostgreSQL Setup
In order to run the API locally, a local PostgreSQL server must be present. For ease of access, I recommend [pgAdmin](https://www.pgadmin.org/download/) and a local installation of [PostgreSQL](https://www.postgresql.org/download/).
The default database configuration for the API is below. However, any local settings can be passed in by the .env within the api sub directory.
- PGDATABASE="badger"
- PGUSER="badger"
- PGPASSWORD="badger"
- PGHOST="localhost"
- PGPORT="5432"
If using pgAdmin, first find "Login/Group Roles", and right click to create a new role. General/Name and Definition/Password can be set to "badger" to match the default config. Before saving, ensure "Can login?" and "Superuser?" are set to true.
Next, find "Databases" and right click it, "Create" -> "Database". Name the database "badger", and set the owner as the badger user you created in the previous step. Save it, and then before running the server, run "python manage.py migrate" to apply the data tables.
- `docker compose run web python manage.py migrate`
- `docker compose up --build`

View File

@ -76,11 +76,11 @@ WSGI_APPLICATION = 'api.wsgi.application'
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("PGDATABASE", "badger"),
"USER": os.getenv("PGUSER", "badger"),
"PASSWORD": os.getenv("PGPASSWORD", "badger"),
"HOST": os.getenv("PGHOST", "localhost"),
"PORT": os.getenv("PGPORT", "5432"),
"NAME": os.getenv("POSTGRES_DB", "badger"),
"USER": os.getenv("POSTGRES_USER", "badger"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD", "badger"),
"HOST": os.getenv("POSTGRES_HOST", "badger_db"),
"PORT": os.getenv("POSTGRES_PORT", 5432),
}
}

32
api/docker-compose.yml Normal file
View File

@ -0,0 +1,32 @@
version: '3.8'
services:
# Run the postgres server
badger_db:
image: postgres:13.2
container_name: badger_db
restart: always
environment:
POSTGRES_PASSWORD: badger
POSTGRES_USER: badger
POSTGRES_DB: badger
volumes:
- ./database:/var/lib/postgresql/data
hostname: badger_db
ports:
- '5432:5432'
# Run the badger server
web:
container_name: badger_server
build: .
command: python manage.py runserver 0.0.0.0:8000
restart: always
ports:
- '8000:8000'
env_file:
- .env
volumes:
- .:/code
depends_on:
- badger_db
links:
- badger_db