Rails APIを高速に立ち上げる
ハッカソンで高速にAPIサーバーを立ち上げるためのメモです.
Rails API 立ち上げ
1 プロジェクト作成
$ rails new rails_api --api --skip-bundle $ bundle install
2 create db
$ rails generate scaffold User name:string $ rails db:create $ rails db:migrate
3 routing設定
- route.rb
Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html scope :api, defaults: { format: :json } do resources :users end end
4 access! localhost:3000/api/users
post
% curl -X POST -H "Content-Type: application/json" -d '{"name": "hoge"}' http://localhost:3000/api/users
get
% curl http://localhost:3000/api/users
Heroku Deploy
1 heroku create heat-monitor
2 sqlite3
をdevelopment
, test
に移動し,production
にpg
を追加.
herokuはpostgresql
を使うので, production環境ではpostgresql
に変更する.
group :production do gem 'pg' end roup :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'sqlite3', '~> 1.4' end
3 bundle install
% bundle install --without production
4 database.ymlを変更
production: <<: *default # database: db/production.sqlite3 adapter: postgresql encoding: unicode pool: 5
5 コミット
git add -A & git commit -m "first commit"
6 herokuにpush
git push heroku master
7 heroku環境でmigrate
heroku run rails db:migrate
8 access!(4とほぼ同じ)