Easier dev setup

This commit is contained in:
Lucas BEE 2019-05-15 13:06:26 +00:00 committed by Gogs
parent 5b9e9b15d2
commit 42a0a8eb73
2 changed files with 107 additions and 12 deletions

View File

@ -16,12 +16,27 @@ yarn install
#### Check your config.yml file #### Check your config.yml file
#### Setup your database
Install your own database or spawn one in a docker
```sh
./dev.sh docker-db up -d
```
#### Run the backend #### Run the backend
```sh ```sh
./dev.sh back ./dev.sh back
``` ```
In case of custom database, launch the backend with DB_USER, DB_PASS,
DB_DATABASE, DB_HOST, DB_PORT, or directly the full DSN in DB_DSN_DEV
```sh
DB_DSN_DEV="postgres://test:test@127.0.0.1:5432/dev?sslmode=disable" ./dev.sh back
```
#### Run the frontend #### Run the frontend
```sh ```sh
@ -51,6 +66,12 @@ Users:
* Admin user: admin / admin * Admin user: admin / admin
* Test user: test / test * Test user: test / test
## Init the database
```sh
./dev.sh db-init
```
## Run the tests ## Run the tests
```sh ```sh

98
dev.sh
View File

@ -2,27 +2,54 @@
set -e set -e
DB_USER="test" DB_USER=${DB_USER:-"test"}
DB_PASS="test" DB_PASS=${DB_PASS:-"test"}
DB_DSN_DEV="postgres://$DB_USER:$DB_PASS@127.0.0.1:5432/dev?sslmode=disable" DB_DATABASE=${DB_DATABASE:-"dev"}
DB_HOST=${DB_HOST:-"127.0.0.1"}
DB_PORT=${DB_PORT:-"5432"}
DB_DSN_DEV=${DB_DSN_DEV:-"postgres://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_DATABASE?sslmode=disable"}
CANAPE_USERNAME=${CANAPE_USERNAME:-"admin"}
CANAPE_PASS=${CANAPE_PASS:-"admin"}
TOKEN=${CANAPE_TOKEN}
CONFIG_FILE=config.yml CONFIG_FILE=config.yml
MIGRATION_SCHEMA=./migrations MIGRATION_SCHEMA=./migrations
DOCKER_COMPOSE_FILE=./docker/docker-compose.yml DOCKER_COMPOSE_FILE=./docker/docker-compose.yml
_usage() { _usage() {
prog=$(basename "$0")
echo "Usage:" echo "Usage:"
echo "$0 [back|front]" echo " $prog back"
echo " Apply the migrations, build and run the backend"
echo ""
echo " $prog front"
echo " Install the JS packages and run the frontend"
echo ""
echo " $prog db-init"
echo " Refresh the informations (imdb / movies / shows) needed"
echo " before the first run"
echo ""
echo " $prog docker-db [up|up -d|down|other docker-compose options...]"
echo " Setup the database in a docker"
exit 1 exit 1
} }
_log_info() {
printf "$(tput setaf 5)-->$(tput setaf 2) %s$(tput sgr0)\n" "$@"
}
_log_error() {
printf "$(tput setaf 6)-->$(tput setaf 9) %s$(tput sgr0)\n" "$@"
}
_check_command() { _check_command() {
command -v "$1" >/dev/null 2>/dev/null command -v "$1" >/dev/null 2>/dev/null
} }
_ensure_command() { _ensure_command() {
_check_command "$1" && return 0 _check_command "$1" && return 0
echo "Please install $1 first" _log_error "Please install $1 first"
exit 1 exit 1
} }
@ -30,42 +57,89 @@ _migrate() {
migrate -database "$DB_DSN_DEV" "$@" migrate -database "$DB_DSN_DEV" "$@"
} }
_ensure_command jq
_ensure_command go _ensure_command go
_ensure_command docker
_ensure_command docker-compose
_ensure_command yarn _ensure_command yarn
_check_command migrate || { _check_command migrate || {
echo "Installing migrate" _log_info "Installing migrate"
GO111MODULE=off \ GO111MODULE=off \
go get -tags 'postgres' \ go get -tags 'postgres' \
-u -v github.com/golang-migrate/migrate/cmd/migrate -u -v github.com/golang-migrate/migrate/cmd/migrate
} }
_check_command fresh || { _check_command fresh || {
echo "Installing fresh" _log_info "Installing fresh"
GO111MODULE=off \ GO111MODULE=off \
go get -u -v github.com/pilu/fresh go get -u -v github.com/pilu/fresh
} }
[ -f "$CONFIG_FILE" ] || { [ -f "$CONFIG_FILE" ] || {
echo "Please setup the canape configuration in $CONFIG_FILE" _log_error "Please setup the canape configuration in $CONFIG_FILE"
exit 1 exit 1
} }
canape_call() {
method=$1
resource=$2
_log_info "Calling: $method $resource ..."
response=$(curl --silent --show-error \
-X "$method" \
-H "Content-Type: application/json" \
-H "Authorization: $TOKEN" \
"http://localhost:3000/$resource")
[ -n "$response" ] && echo "$response"
_log_info "Done"
}
canape_login() {
# Skip if the user defined its own token
[ -n "$CANAPE_TOKEN" ] && return
_log_info "Logging in ..."
json_data=$(jq -n -c --arg username "$CANAPE_USERNAME" \
--arg password "$CANAPE_PASS" \
'{username: $username,password: $password}')
TOKEN=$(curl --silent --show-error -X POST \
-d "$json_data" \
http://localhost:3000/users/login | jq -r .data.token )
_log_info "Logged in"
}
canape_logout() {
# Skip if the user defined its own token
[ -n "$CANAPE_TOKEN" ] && return
_log_info "Disconnecting ..."
canape_call DELETE "users/tokens/$TOKEN"
_log_info "Disconnected"
}
case $1 in case $1 in
back) back)
docker-compose -f "$DOCKER_COMPOSE_FILE" up -d # Apply the migrations
# Wait for the container to be up
_migrate -path "$MIGRATION_SCHEMA" up _migrate -path "$MIGRATION_SCHEMA" up
CONFIG_FILE="./config.yml" fresh -c fresh.conf CONFIG_FILE="./config.yml" fresh -c fresh.conf
;; ;;
docker-db)
_ensure_command docker
_ensure_command docker-compose
shift
docker-compose -f "$DOCKER_COMPOSE_FILE" "$@"
;;
front) front)
yarn install yarn install
yarn start yarn start
;; ;;
db-init)
_ensure_command jq
canape_login
canape_call POST ratings/refresh
canape_call POST movies/refresh
canape_call POST shows/refresh
canape_logout
;;
*) *)
_log_error "Unknown command $1"
_usage _usage
;; ;;
esac esac