LoginSignup
0
0

More than 5 years have passed since last update.

ElasticBeanstalk with sinatra

Posted at

環境構築

eb cliのインストール

pip install awsebcli --upgrade --user
pyenvを入れている場合、--userの指定は不要

dockerfile

FROM ruby
ここでrubyとしか指定していないが、自動的にdebianイメージになる模様

ローカルでsinatraアプリのモックを作る

ruby -v
>ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16] #rbenvの環境がある前提

Gemfile作成

vim Gemfile

最低限のインストール

source 'https://rubygems.org'
gem 'sinatra'

bundle installする

bundle install

軽量web serverのthinを入れ、そいつを起動するshotgunを入れる

起動

bundle exec shotgun main.rb

== Shotgun/Thin on http://127.0.0.1:9393/
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on 127.0.0.1:9393, CTRL+C to stop
127.0.0.1 - - [30/Jan/2018:16:39:35 +0900] "GET / HTTP/1.1" 200 3 0.0295

これで、127.0.0.1:9393にアクセスするとなんか出る

ActiveRecord

sinatra用activerecordを入れる

https://github.com/janko-m/sinatra-activerecord

dbに接続する

require 'sinatra/activerecord'

これでconfig/database.ymlがあれば自動的に読み込んでくれる

接続

ActiveRecord::Base.connection.execute('select now()').first

こんなエラー

uninitialized constant ActiveRecord::ConnectionAdapters::RedshiftAdapter::PGconn

PGconnという定数が未定義なのが原因なのだが、理由がわからん

vim /Users/hokaron/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pg-0.21.0/lib/pg.rb
autoload :PGconn,   'pg/deprecated_constants' # ここで読み込んでいる

deprecated_constantsってのは明らかに怪しい
中身を見ると

The PGconn, PGresult, and PGError constants are deprecated, and will be removed as of version 1.0.

とばっちり書いてある

bundle show
pg (1.0)

お前か・・

Gemfile更新

gem 'pg', '~> 0.20.0'
bundle update pg

これでOK。疲れたよパトラッシュ・・(activerecordって依存関係にうるさすぎ)
ちなみにactiverecord5のredshift-adapterでもだめでした。

SQLのテンプレートはerubisを使用(書き方はerbとほぼ同じ)

Sinatraの実行環境の指定

なぜかrubyコマンドでサーバーも起動。なぜだろう

ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]

何気に入れているsinatra-contribはすごいやつらしい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0