Introduction

Youtube sharing application: Sharing your favorite youtube video with others. Features:

  • User registration and login
  • Sharing YouTube videos by input Youtube URL
  • Viewing a list of shared videos
  • Real-time notifications for new video shares - Click to notification to go to the video page

Production:

Local:

Prerequisites

  • Ruby v3.2.2
  • Node v20.6.1
  • Postgresql 14
  • redis v7.2.1

Installation & Configuration

  1. Add to /etc/hosts file:
127.0.0.1 local.youtubesharing.com
::1       local.youtubesharing.com

127.0.0.1 api-local.youtubesharing.com
::1       api-local.youtubesharing.com
  1. Setup the local certificate
  // Setup self-sign local cert 
  cd local-cert
  ./setup.sh
  cd ../
  1. Create env file:
  • Look up your ip: ifconfig -u | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2 | head -1
  • Create .env and .env.docker files copy from .env.example file and replace DB_HOST with your IP
  1. Run the dev.sh script:
  • With local rails server: ./dev.sh
  • With docker rails server: ./dev.sh docker

Running Application (without using script)

Installation

  • Add to /etc/hosts file, setup the local certificate and setup the env file the same as above

  • Rails

    • Start db and redis: docker compose up -d db redis
    • DB migration
    bundle install
    bin/rails db:create
    bin/rails db:migrate
    bin/rails db:seed
    
    • Run server: bundle exec rails s -p 3001 -b "ssl://api-local.youtubesharing.com:3001?key=local-cert/api-local.youtubesharing.com.key&cert=local-cert/api-local.youtubesharing.com.crt"
  • React

    cd frontend/youtube-sharing
    npm i
    npm run start
    

Test run

  • Rails
    • Set up the env (Start docker for integration test) and migrate the DB
./test.sh
  • Run test
bundle exec rspec
  • Terminate the test docker
./test.sh down
  • React
cd frontend/youtube-sharing
npm i
npm run test

Docker build

Related resources

  • Dockerfile: for production build
  • Dockerfile.dev: for developement build
  • docker-compose.yml: for developement build
  • docker-compose-test.yml: for test build
  • .env: for developement local env
  • .env.test: for test env
  • .env.docker: for development docker env

Local

docker compose build rails

Production

docker build -f Dockerfile .

How to use this app

  • Register a new account
  • Login, click the share button to share a new Youtube video
  • When share video, the video title will be fetched from youtube, so you only need to fill the description and Youtube URL
  • The Notifications will be shown for all users (include not logged in users) except the video share owner (To test the notification, open a new igconito tab or access the web on other devices/browsers)
  • You can click to the notification to the specific video page
  • Pages are responsive

Some limits

  • No pagination - that mean if we have a lot of shared videos the Home page will load all of them and make the performance down
  • No history for notifications

Deploy

  • Rails
    • Rails app is deployed by fly.io. The postgre DB and Redis server also is served by fly.io
    # Install fly cli
    brew install flyctl
    # Login
    fly auth login
    # Init project
    fly launch
    # Deploy without replica
    fly deploy --ha=false
    # Add some needed secrets
    fly secrets set REDIS_URL=xxx
    fly secrets set CLIENT_URL=xxx
    ...
    
  • React

Troubleshooting

  • If have problem when running on local

    • Port already in used: kill the port lsof -i :3000 | awk '/[1-9]/ {print $2}' | xargs kill -9

    • Cannot run bundle install: check presequites install

    • Cannot connect Postgresql database - There is an issue connecting with your hostname: check .env file setup, need to same as below (especially your DB_HOST - Look up your ip: ifconfig -u | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2 | head -1)

      CLIENT_URL=https://local.youtubesharing.com:3000
      REDIS_URL=redis://localhost:6379/1
      DB_USERNAME=postgres
      DB_PASSWORD=
      # Set your ip address as host
      DB_HOST=192.168.0.156
      DB_NAME=youtube_sharing_dev
      DB_URL=
    
    • SSL (HTTPS) error: check self sign cert setup

      • Check add host to /etc/hosts as presequites
      • You can re-generate and re-setup the cert by running
      cd local-cert
      ./generate.sh
      ./setup.sh
      
    • SECRET_KE_BASE missing error: run bin/rails credentials:edit or bin/rails credentials:edit --environment=production for generate key base. Add the master.key file value as the RAILS_MASTER_KEY env

    • If the notification is not working

      • Check your redis server and REDIS_URL setup in env file
      • Rerun the redis and rails docker
    • If have problem with spec/system test

      • CHeck your Selenium driver installation