github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/README.md (about) 1 # CAST Backend 2 3 ## Prerequisites 4 5 - [GoLang 1.16+](https://golang.org/doc/install) 6 - [PostgreSQL 14.1](https://www.postgresql.org/download/) 7 - [Flow CLI](https://docs.onflow.org/flow-cli/install/) 8 - Note: See below for how install v0.30.2 (required) 9 10 ## Local Development 11 12 ### Environment Variables 13 14 Copy `.env.example` to a new file `.env`. 15 16 ```bash 17 cp .env.example .env 18 ``` 19 20 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](https://www.pinata.cloud/). 21 22 ### Database 23 24 #### Install PSQL 25 - [PostgreSQL 14.1](https://www.postgresql.org/download/) 26 - via homebrew `brew install postgresql` 27 28 #### Configure Postgres DB User/PW 29 30 By default the postgres user has no password. Give it a password 31 so we can connect with our application 32 ```bash 33 sudo -i -u postgres 34 psql 35 CREATE USER postgres; # if postgres user doesnt exist 36 ALTER USER postgres PASSWORD 'admin'; 37 ``` 38 39 #### Create flow_snapshot Database, and Test Database 40 41 ```bash 42 sudo -i -u postgres 43 psql 44 CREATE DATABASE flow_snapshot; 45 CREATE DATABASE flow_snapshot_test; 46 ``` 47 48 NOTE: in some system configurations `sudo -i -u postgres` will return an error so you can run the following: 49 50 ```bash 51 psql 52 CREATE USER postgres; 53 ALTER USER postgres PASSWORD 'admin'; 54 ALTER USER postgres WITH SUPERUSER; 55 CREATE DATABASE flow_snapshot WITH OWNER = postgres; 56 CREATE DATABASE flow_snapshot_test WITH OWNER = postgres; 57 ``` 58 59 #### Install Migrate Tool 60 61 To run the `make` migrate scripts, you need to have the `migrate` CLI installed in your local path. Follow the [steps here](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate) to install `migrate` on your machine. 62 63 64 #### Creating Migration Files 65 66 In `flow-voting-tool`: 67 ```bash 68 migrate create -ext sql -dir migrations -seq my_migration_file 69 ``` 70 71 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. 72 73 #### Migrating Up & Down 74 75 Migrate Up: 76 ```bash 77 make migrateup 78 ``` 79 80 Migrate Down: 81 ```bash 82 make migratedown 83 ``` 84 85 #### Migrating Up & Down for Test Database 86 87 The first time you run the tests, you'll need to migrate the test database. 88 89 Migrate Up: 90 ```bash 91 make testmigrateup 92 ``` 93 94 Migrate Down: 95 ```bash 96 make testmigratedown 97 ``` 98 99 #### Dealing with Dirty Schema Migrations 100 101 Reset Schema Migrations 102 ```bash 103 UPDATE schema_migrations SET dirty = false 104 ``` 105 106 ### Testing 107 108 1. 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](https://docs.onflow.org/flow-cli/install/) 109 2. Run migrations against the test database (if migrations aren't up to date): `make testmigrateup` 110 3. Run the test suite: `make test` 111 112 #### Building & Running Docker Test Image 113 114 Build (To build locally, uncomment the `COPY .env .env` line in Dockerfile.test) 115 ```bash 116 docker build . --file=Dockerfile.test -t vt-test 117 ``` 118 119 Run (To run locally, must have flow emulator running, and local PostgresDB): 120 ```bash 121 docker run -it --network=host --rm --name vt-test vt-test:latest 122 ``` 123 124 ### Run the app 125 126 Before running the app you should: 127 128 1. 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](https://docs.onflow.org/flow-cli/install/) 129 2. Run migrations against the database (if migrations aren't up to date): `make migrateup` 130 131 #### Go Executable 132 To build the binary & run it: 133 ```bash 134 LINUX: make build_and_run 135 MACOS: make macrun 136 ``` 137 138 Alternatively: 139 ```bash 140 go run ./main 141 ``` 142 143 The server runs on port 5001. Confirm that it is running by hitting `https://localhost:5001/api` in your browser. 144 145 #### Running Blockchain & Dev Wallet 146 147 To start a local blockchain & dev-wallet 148 149 ```bash 150 npm run chain 151 ``` 152 153 #### Running via Docker 154 155 This is a good way to debug env related issues. 156 157 1. If you are connecting to a local PostgresDB, update `DB_HOST` in your `.env` file to `host.docker.internal` 158 2. Set `ENV_NAME="DOCKER"` 159 160 Finally, 161 ```bash 162 make image # create the Docker image 163 make container # spin up a container from Docker image 164 ```