~electric/kopfsalat_api

Backend for quiz app
8c8a688a — Micah H 10 days ago
Fix create / update of test sets
45fbc1fc — Micah H 10 days ago
Initial changes for direct compose-deploy
ee05872f — Micah H 2 months ago
Fix test group import to include test_group_ids in the questions created

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~electric/kopfsalat_api
read/write
git@git.sr.ht:~electric/kopfsalat_api

You can also use your local clone with git send-email.

#Kopfsalat Api

#Installation

To run this application you need to install:

  • elixir
  • postgres
  • inotify-tools
  • ffmpeg (for audio transcoding)
  • nodejs / yarn (dev only, for building client js)

#Initialize DB

First you need to initialize the data cluster:

sudo su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'"

Then login/connect to the database server:

sudo runuser -u postgres -- psql

Create the database and permissions

CREATE USER kopfsalat_dev WITH PASSWORD 'somePassword';
CREATE DATABASE kopfsalat_dev;
GRANT ALL PRIVILEGES ON DATABASE kopfsalat_dev to kopfsalat_dev;
ALTER DATABASE kopfsalat_dev OWNER TO kopfsalat_dev;

Exit the database server using \q \q

Finally, create the db-schema using ecto.migrate mix ecto.migrate

#Starting

To start the Phoenix server:

  • Install dependencies with mix deps.get
  • Make sure your db is started sudo systemctl start postgresql.service
  • Create and migrate your database with mix ecto.setup (or mix ecto.reset if you want to reset)
  • Run mix assets.deploy to prepare static assets
  • Start Phoenix endpoint with mix phx.server

Now you can visit localhost:4000 from your browser.

Ready to run in production? Please check our deployment guides.

#Creating tables

mix phx.gen.json Accounts User users username:string password:string last_login:datetime language:integer birth_year:integer gender:integer
mix phx.gen.html Accounts Admin admins username:string password:string last_login:datetime super:boolean
mix phx.gen.html Tests Test tests title:string description:string content:string created_admin_id:integer visible:boolean
mix phx.gen.html Tests TestGroup test_groups title:string description:string created_admin_id:integer
mix phx.gen.html Questions Question questions content:string answer_value:string answer_key:string fail_content:string success_content: string order:integer max_time:integer visible:boolean
mix phx.gen.json TestRuns TestRun test_runs user_id:integer test_set_id:integer device_id:string start:date
mix phx.gen.json Responses Response responses user_id:integer test_set_id:integer question_id:integer value:string response_type:integer success:boolean start:date duration:integer out_of_focus_count:number out_of_focus_ms:number
mix phx.gen.schema Media.Upload uploads filename:string size:integer content_type:string hash:string
mix phx.gen.html Badges Badge badges query:map badge_upload_id:integer upload_sprite_index:integer title:string description:string
mix phx.gen.html Badges BadgeLevel badge_levels badge_id:integer comp_mode:integer value:integer color:string
mix phx.gen.schema Media.BadgeUpload badge_uploads badge_id:integer upload_id:integer
mix ecto.migrate

#Create migration

mix ecto.gen.migration some_new_migration

#Run Tests

mix test

#View routes

mix phx.routes

#Quick Deployment

#Build a release / copy to server (docker and podman should be interchangable):

  • podman build -f Containerfile -t kopfsalat_api .
  • podman save kopfsalat_api -o kopfsalat_api.tar.gz
  • scp kopfsalat_api.tar.gz server_user@example.org:/root/
  • scp deploy_scripts/start_container.sh server_user@example.org:/root/

#Setup a new server and login:

  • Install podman and postgres (apt install podman postgresql)
  • Add user for interacting with container mounts
    • adduser kopfsalat --group
  • Start postgresql (service postgresql start)
  • Enable postgresql (service postgresql enable)
  • Add a database/user to postgres
    • Switch users: su postgres
    • Create DB: createdb db_name
    • Add user: psql
      • CREATE USER db_username WITH PASSWORD 'myPassword';
      • GRANT ALL PRIVILEGES ON DATABASE db_name TO db_user_name;
      • \q
    • Type exit drop back to root
  • Import release: podman load -i digamma.tar.gz
  • Set environment variables by creating the file env.sh:
    • export CONTAINER_NAME=kopfsalat_api
    • export HOSTNAME=example.com
    • export DATABASE_URL="ecto://db_username:myPassword@localhost/db_name"
    • export SECRET_KEY_BASE="some really long secret key, possibly generated by: mix phx.gen.secret"
    • export ACME_CHALLENGE_DIR=/opt/acme
    • export SSL_DIR=/etc/letsencrypt/live/$HOSTNAME
  • Run start container script: ./start_container.sh

#Setup certbot / ssl:

  • Copy the run_certbot.sh helper script to the server:
    • scp deploy_scripts/run_certbot.sh server\_user@example.org:/root/
  • Login into the remote server and install certbot (apt install certbot)
  • Make sure the app-server container is already running in http-mode with the AMCE_CHALLENGE_DIR configured.
    • source ./env.sh && curl $HOSTNAME
  • Run helper script: ./run_certbot.sh
  • Restart the app-server container with: ./start_container.sh
  • Setup auto-renew by running: crontab -e
    • and adding the line: @daily /root/start_container.sh
  • (Optional) Force redirect to ssl (http -> https) by using build flag FORCE_SSL=true before the podman build command
    • FORCE_SSL=true podman build -f Containerfile -t kopfsalat\_api .
    • This creates a new release that can be deployed, but it's important to have one that allows http first in-order to verify with lets-encrypt

#Lock-down the firewall

  • Install ufw: apt install ufw
  • Add services/apps ssh, http, https, and enable:
    • ufw allow ssh
    • ufw allow http
    • ufw allow https
    • ufw enable

#Learn more