JSON API following jsonapi.org spec for a blog application with auth for creating and editing post resources
- .ruby-version is setup for 3.0.6
- Serialization of models (to JSON) is done with the jsonapi-resources gem
- Use ActiveRecord and the
pg
gem for PostgreSQL
- Use ActiveRecord and the
bundle
to install the gemsmkdir -p shared/log && mkdir -p shared/pids && mkdir -p shared/sockets
Copy the config/database.example.yml config/secrets.example.yml files to: config/database.yml config/secrets.yml
In the database.example.yml file the password is set to and environemnt variables: BLOG_API_DB_USR and BLOG_API_DB_PWORD. set your environment variables, or replace them with your db credentials
export BLOG_API_DB_USR='blog_api'
export BLOG_API_DB_PWORD='XXXX'
- DB role 'blog_api'
Setup a Postgress role: 'blog_api'
createuser -P -s -e blog_api
createdb blog_api_development -O blog_api
Or,
PGUSER=$BLOG_API_DB_USR PGPASSWORD=$BLOG_API_DB_PWORD psql -h localhost -p 5432 blog_api_development
CREATE ROLE blog_api WITH SUPERUSER LOGIN PASSWORD 'X';
CREATE DATABASE blog_api_development OWNER blog_api;
CREATE DATABASE blog_api_test OWNER blog_api;
bundle exec rake db:setup
- See the seeds file to create resources for a post, author, and user
Copy the db/seeds.example.rb and add records for an user, author and posts
-
Use tunnel
ssh -fNg -L 54321:127.0.0.1:5432 pixelhandler.com lsof -i tcp | grep ^ssh
-
Dump database (use pword from prod)
current_time=$(date "+%Y.%m.%d-%H.%M.%S"); pg_dump -U $BLOG_API_DB_PROD_USR -h localhost -p 54321 $BLOG_API_DB_PROD > $BLOG_API_DB_PROD"."$current_time".sql"
-
Drop and create
bin/rails db:drop bin/rails db:create
-
Import
PGUSER=$BLOG_API_DB_USR PGPASSWORD=$BLOG_API_DB_PWORD psql -h localhost -p $BLOG_API_DB_PORT blog_api_development < blog_api_production.sql
-
https://docs.docker.com/engine/examples/postgresql_service/#installing-postgresql-on-docker
docker build -t eg_postgresql . docker run --rm -P --name pg_test eg_postgresql docker ps psql -h localhost -p 32768 -d docker -U docker --password
docker
is the pword.