CAST Backend
Prerequisites
Local Development
Environment Variables
Copy .env.example
to a new file .env
.
cp .env.example .env
The correct values for IPFS_KEY
and IPFS_SECRET
can be found in the Dapper Collectives 1password, or you you can use your own by creating an account with Pinata.
Database
Install PSQL
Configure Postgres DB User/PW
By default the postgres user has no password. Give it a password
so we can connect with our application
sudo -i -u postgres
psql
CREATE USER postgres; # if postgres user doesnt exist
ALTER USER postgres PASSWORD 'admin';
Create flow_snapshot Database, and Test Database
sudo -i -u postgres
psql
CREATE DATABASE flow_snapshot;
CREATE DATABASE flow_snapshot_test;
NOTE: in some system configurations sudo -i -u postgres
will return an error so you can run the following:
psql
CREATE USER postgres;
ALTER USER postgres PASSWORD 'admin';
ALTER USER postgres WITH SUPERUSER;
CREATE DATABASE flow_snapshot WITH OWNER = postgres;
CREATE DATABASE flow_snapshot_test WITH OWNER = postgres;
To run the make
migrate scripts, you need to have the migrate
CLI installed in your local path. Follow the steps here to install migrate
on your machine.
Creating Migration Files
In flow-voting-tool
:
migrate create -ext sql -dir migrations -seq my_migration_file
This will create two files, one for migrating up and one for migrating down. Please fill in both properly to make rolling back migrations easy, if necessary.
Migrating Up & Down
Migrate Up:
make migrateup
Migrate Down:
make migratedown
Migrating Up & Down for Test Database
The first time you run the tests, you'll need to migrate the test database.
Migrate Up:
make testmigrateup
Migrate Down:
make testmigratedown
Dealing with Dirty Schema Migrations
Reset Schema Migrations
UPDATE schema_migrations SET dirty = false
Testing
- Run
flow emulator
from this top-level directory (the private key for the emulator-account
in top level flow.json
must match the private key hard-coded in the test suite eg. backend/cadence/V2/tests/flow.json
). Install the flow
CLI here
- Run migrations against the test database (if migrations aren't up to date):
make testmigrateup
- Run the test suite:
make test
Building & Running Docker Test Image
Build (To build locally, uncomment the COPY .env .env
line in Dockerfile.test)
docker build . --file=Dockerfile.test -t vt-test
Run (To run locally, must have flow emulator running, and local PostgresDB):
docker run -it --network=host --rm --name vt-test vt-test:latest
Run the app
Before running the app you should:
- Run
flow emulator
from this top-level directory (the private key for the emulator-account
in top levelflow.json
must match the private key hard-coded in the test suite eg. backend/cadence/V2/tests/flow.json
). Install the flow
CLI here
- Run migrations against the database (if migrations aren't up to date):
make migrateup
Go Executable
To build the binary & run it:
LINUX: make build_and_run
MACOS: make macrun
Alternatively:
go run ./main
The server runs on port 5001. Confirm that it is running by hitting https://localhost:5001/api
in your browser.
Running Blockchain & Dev Wallet
To start a local blockchain & dev-wallet
npm run chain
Running via Docker
This is a good way to debug env related issues.
- If you are connecting to a local PostgresDB, update
DB_HOST
in your .env
file to host.docker.internal
- Set
ENV_NAME="DOCKER"
Finally,
make image # create the Docker image
make container # spin up a container from Docker image