Sign inSign up

quyetlq/labhok_backend_api

By quyetlq

•Updated over 7 years ago

labhok_backend_api

Image
0

10K+

quyetlq/labhok_backend_api repository overview

⁠labhok_backend_api

⁠Configure file

  • Run cp config/database.yml.example config/database.yml
  • Run cp config/database.yml.example config/database.yml.docker
  • Run cp config/redis.conf.example config/redis.conf
  • Run cp .env.example .env.docker

⁠Configure environments

The default environment file is in .env.example. Rename it to .env to apply default environment variables.

  • With development environment, set LABHOK_CORS_ALLOW_HOST="localhost:4200"

  • Run EDITOR=cat rails credentials:edit. It will create master.key and credentinals.yml.ync.

  • credential.yml.example is the example file for credentials.yml.enc when decrypted. It contains variables required for rails to work properly

  • Optionally, do cp .firebaserc.example .firebaserc, and set the appropriate default project name in .firebaserc. By setting the default project, firebase CLI commands can now be issued without using the --project flag.

⁠Setup Firebase Project Credentials

⁠Step #1: Import a Service Account Private Key
  • From Firebase console > Settings > Service accounts > Firebase Admin SDK, generate a new private key
  • Now rename the private key file to firebase.<RAILS_ENV>.json and drop in the config directory
⁠Step #2: Configure Web API Key
  • From Firebase console > Settings > General > Your project, copy the Web API Key
  • And, paste the copied key in the environment file (e.g. .env) as a value for the FIREBASE_WEB_API_KEY variable

⁠ImageMagick

  • Please install package imagemagick on your machine to work with uploading attachment on Communication section

⁠MailCatcher

  • Please make sure mailcatcher is running SMTP on port 1025 before starting the application.

  • To install mailcatcher: gem install mailcatcher

  • To run mailcatcher: mailcatcher

⁠Graphviz

Install Graphviz⁠ to support the ERD generator.

⁠Firebase Deployment

⁠Step #1: Install Dependencies and Setup Environment
  • Install Node.js v6.11.5
  • Install firebase-tools using any of the following methods.
    $ npm install -g firebase-tools
    $ yarn global add firebase-tools
    
  • Login to Firebase by choosing a suitable method.
    • Development: firebase login
    • Production and Staging Deployment: Generate a CLI token by running firebase login:ci
⁠Step #2: Fetch Credentials

From Firebase console > Project settings > Cloud messaging > Project credentials, collect Server key and Sender ID.

⁠Step #3: Deploy Configuration

Now, deploy the fetched credentials.

$ cd firebase
$ firebase functions:config:set messaging.sender=<SENDER_ID> messaging.key=<SERVER_KEY> # Development
$ firebase functions:config:set messaging.sender=<SENDER_ID> messaging.key=<SERVER_KEY> --project=<PROJECT_ID> --token=<CLI_KEY> # Staging and Production
⁠Step #4: Deploy Functions

And, finally, deploy the cloud functions.

$ firebase deploy --only functions # Development
$ firebase deploy --only functions --force --project=<PROJECT_ID> --token=<CLI_KEY> # Staging and Production

⁠Run on Docker

To run application on Docker, run the following commands:

NOTE: Before building the image, please make sure config/master.key and config/credentials.yml.enc is properly configured for the application

  • To build necessary image for the project, run make build-all. This command will build image for the application and haproxy for load-balancing

  • To start the application for the first time, run make up. This command will build images, create and migrate database and then startup the application

  • To run test, run make test. This command will run rubocop and rspec on this project

Server logs are stored in /data/labhok/log

⁠Database migration

docker-compose run --rm server-1 bundle exec rake db:create

docker-compose run --rm server-1 bundle exec rake db:migrate

⁠Rails console

docker-compose run --rm server-1 bundle exec rails console

⁠Upgrade Deployment

We can upgrade the application by running deploy.sh script. In this script, we can deploy by tag or by branch name. The syntax for deploy.sh script is

./deploy.sh [tag|branch] [tag_name|branch_name]

For example: To deploy develop branch, run ./deploy.sh branch develop

NOTE: This script also tag the docker image of the current deploy by git tag or git branch. For example: labhok-backend:branch-[branch_name]-20180101000000 or labhok-backend:tag-[tag_name]

⁠Install and Test with databases read and write

  mysql:
    image: mysql:5.7
    restart: always
    volumes:
      - "/data/labhok/mysql:/var/lib/mysql"
    ports:
      - 3307:3306
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1

  mysql2:
    image: mysql:5.7
    restart: always
    ports:
      - 3308:3306
    volumes:
      - "/data/labhok/mysql2:/var/lib/mysql"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
development: &common
  <<: *default
  database: development_write

development_reader:
  <<: *common
  database: labhok_backend_api_development
  host: <%= ENV["DEVELOPMENT_READER_LABHOK_DATABASE_HOST"] %>
  • Step 4: change host reader ENV["DEVELOPMENT_READER_LABHOK_DATABASE_HOST"], <%= ENV["LABHOK_DATABASE_HOST"] %> in file .env.docker

  • Example:

DEVELOPMENT_READER_LABHOK_DATABASE_HOST = "development_reader_host"
LABHOK_DATABASE_HOST = "development_host"

NOTE: create new database test ("development_write") with host LABHOK_DATABASE_HOST

  • Step 5: Test switch_point in 2 databases development_write and labhok_backend_api_development

    • with query SELECT, expect using database labhok_backend_api_development
    irb(main):020:0> u = User.first                            
    D, [2018-11-30T08:26:40.330831 #1] DEBUG -- :   User Load (0.6ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
    => #<Teacher id: 1, first_name: "xxxxxx", last_name: "Phạm Nhật", avatar: "https://static.giantbomb.com/uploads/scale_medium/...", password_digest: "$2a$10$rk9rOxLxZ9NUsHxS2QEPrupki9J.8Kz1ts1q31PGjyg...", date_of_birth: "1986-01-01", identity: "12345670", phone: "12345678", type: "Teacher", username: "teacher1", gender: "male", created_at: "2018-11-28 09:09:40", updated_at: "2018-11-30 03:51:33", study_class_id: nil, national: "Kinh", job: nil, email: nil, student_id: nil, nationality: nil, place_of_identity: nil, date_of_identity: nil, blood_group: nil, place_of_birth: nil>
    
    • with query INSERT, UPDATE, expect using database development_write
    irb(main):023:0> u.update first_name: "xxx"
    D, [2018-11-30T08:38:18.363681 #1] DEBUG -- :    (0.4ms)  BEGIN
    D, [2018-11-30T08:38:18.365004 #1] DEBUG -- :   User Exists (0.4ms)  SELECT  1 AS one FROM `users` WHERE `users`.`username` = BINARY 'teacher1' AND `users`.`id` != 1 LIMIT 1
    D, [2018-11-30T08:38:18.365868 #1] DEBUG -- :   User Exists (0.3ms)  SELECT  1 AS one FROM `users` WHERE `users`.`identity` = BINARY '12345670' AND `users`.`id` != 1 LIMIT 1
    D, [2018-11-30T08:38:18.367189 #1] DEBUG -- :    (0.3ms)  ROLLBACK
    Traceback (most recent call last):
            1: from (irb):23
    Mysql2::Error (MySQL client is not connected)
    

NOTE: Mysql2::Error because development_write hasn't tables

Tag summary

Content type

Image

Digest

Size

498.4 MB

Last updated

over 7 years ago

docker pull quyetlq/labhok_backend_api