--Building Ruby on Rails5 (RoR) development environment with docker-compose --Deploy to Heroku --Create a new channel with LINE DV ELOPERS --Creation of parrot return BOT
$ mkdir Linebot
$ cd Linebot
$ vi Dockerfiles
Dockerfile
# ruby2.3.Execute the following command on the image layer of 3 Declaration
FROM ruby:2.3.3
# apt-get is like a package manager
#Update list with update & install required packages
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
#Create and set working directory
RUN mkdir /app_name
##APP the working directory name_Assign to ROOT and$APP_Referenced by ROOT
ENV APP_ROOT /app_name
WORKDIR $APP_ROOT
#Add host side (local) Gemfile
ADD ./Gemfile $APP_ROOT/Gemfile
ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock
#Gemfile bundle install
RUN bundle install
ADD . $APP_ROOT
$ vi docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres:13.0
environment:
POSTGRES_PASSWORD: 'postgres'
ports:
- "3306:3306"
web:
build: .
command: rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app_name
ports:
- "3000:3000"
depends_on:
- db
Start a container called web and db
$ touch Gemfile.lock
$ vi Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 5.2.2'
$ docker-compose run app rails new . --force --database=postgresql --skip-bundle
$ docker-compose build -d
$ docker-compose up -d web
The -d option boots in detach mode (runs on the backend without logging) If you want to see the log, you can remove it, but if you stop it, the container will also stop, so it is troublesome
/config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: db
username: postgres
password: postgres
....
production:
<<: *default
database: app_name_production
username: postgres
password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %>
Write that postgresql will be used in development and production When using Heroku, which will be described later, postgresql is the standard, so it's good. Well, it's okay to use MySQL
Gemfile
#For LINE API
gem 'line-bot-api'
#For managing access tokens, etc.(Environment variable management)
gem 'dotenv-rails'
#postgresql for heroku environment
gem 'pg'
$ docker-compose run web bundle install
$ docker-compose run web rails db:create
$ docker-compose run web rails g controller top index
$ docker-compose run web rails g controller linebot collback
I'll get angry if I don't create a database Since you need a top screen on Heroku, let's make anything for the time being (I don't think it's good to play with volume directly, not just commands, but ignore it)
routes.rb
post '/callback' => 'linebot#callback'
root 'top#index'
/app/controllers/linebot_controller
class LinebotController < ApplicationController
require 'line/bot' # gem 'line-bot-api'
#Disable CSRF token authentication for callback action
protect_from_forgery :except => [:callback]
def client
@client ||= Line::Bot::Client.new { |config|
config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
}
end
def callback
body = request.body.read
signature = request.env['HTTP_X_LINE_SIGNATURE']
unless client.validate_signature(body, signature)
head :bad_request
end
events = client.parse_events_from(body)
events.each { |event|
case event
when Line::Bot::Event::Message
case event.type
when Line::Bot::Event::MessageType::Text
message = {
type: 'text',
text: event.message['text']
}
client.reply_message(event['replyToken'], message)
when Line::Bot::Event::MessageType::Image, Line::Bot::Event::MessageType::Video
response = client.get_message_content(event.message['id'])
tf = Tempfile.open("content")
tf.write(response.body)
end
end
}
head :ok
end
end
$ touch .env
.env
LINE_CHANNEL_SECRET=XXXXXXXXXXX
LINE_CHANNEL_TOKEN=XXXXXXXXXXX
For XXXXXXXXXXX, write the Line Developers guy you'll create later.
If you want to drop a local container
$ docker-compose down
OK if you do it with
If you drop it with Ctl + C
$ rm tmp/pids/server.pid
It is better to do
$ vi heroku.yml
heroku.yml
build:
docker:
web: Dockerfile
run:
web: bundle exec puma -C config/puma.rb
You can't build without writing this It seems that heroku.yml can switch development / production and the accompanying db (not done)
$ heroku login
$ heroku create
$ heroku addons:create heroku-postgresql:hobby-dev
$ heroku container:login
$ heroku container:push web
$ git init
$ heroku git:remote -a <Your heroku repository name>
$ git add .
$ git commit -am "Nice comment"
$ heroku stack:set container
$ git push heroku master
$ heroku container:release web
$ heroku open
If you can't cut the branch, you can add it like git, commit it, and update it with heroku push. Around here, container push and image push are cluttered, so it may be better not to imitate too much
――When something is wrong with the server
$ heroku restert
You should try hitting
--When you want to check the log
$ heroku logs --tail
Deo k
--If you want to see the files on the server
$ heroku bash
It is good to see it by hitting the cat command with (the point of anger is that vim can not be used)
--Create channel with Messaging API
--Issue a channel secret and channel access token and paste it into your .env file
--Turn off response and greeting messages to enable webhooks
--After enabling webhook, enter
I want to test but I don't want to deploy! I want to publish it locally! You can use ngrok
I want to send a request for verification for the time being! Use Talend API Tester
LINE Bot Development Super Introduction (Part 1) From Zero to Response https://qiita.com/nkjm/items/38808bbc97d6927837cd
Create a container-based Rails app with Docker Compose and deploy it to Heroku https://qiita.com/akirakudo/items/16a01271b0a39316e439
Rails5 + MySQL on Docker environment construction by too polite Docker-compose (Docker for Mac) https://qiita.com/azul915/items/5b7063cbc80192343fc0
Recommended Posts