Upgrade to Pro — share decks privately, control downloads, hide ads and more …

少人数でサービスをすばやく開発するためのRails活用事例 - Rails Developers Meetup 2019

少人数でサービスをすばやく開発するためのRails活用事例 - Rails Developers Meetup 2019

Rails Developers Meetup 2019
https://railsdm.github.io/
SESSION: C-2 (ja) @tawachan

少人数でサービスをすばやく開発するためのRails活用事例
A case study on developing a web service quickly with less bugs using Ruby on Rails
ピクシブ株式会社 大多和 祐介
当セッションでは、弊社サービス「pixiv chatstory」が少人数ですばやく、しかしバグは少なく開発していくために採用していているアーキテクチャについてお話します。Ruby/RailsはAPIサーバーとしてHeroku上で使用しているので、このあたりの話を中心にしたいと思います。

This session mainly discusses the IT architecture that one of pixiv's products, pixiv chatstory adopts to enable us the small team (2-4 engineers) to develop the both web and native apps quickly but with less bugs, using Rails as an API server deployed onto Heroku.

Keywords: Rails, Heroku, Swagger, CloudFront, ImageFlux, Firebase, PWA, Angular, Ionic, ReactNative, Expo.io

【参考】フロント周りの話ができなかったので関連記事をよろしければ
pixiv chatstoryのiOSアプリをReact Native(Expo)でリニューアルしました!
https://inside.pixiv.blog/tawachan/6254
pixiv chatstory の
 PWA としての取り組み
https://speakerdeck.com/ikasoumen/pixiv-chatstory-false-pwa-tositefalsequ-rizu-mi

Yusuke Otawa

March 22, 2019
Tweet

More Decks by Yusuke Otawa

Other Decks in Technology

Transcript

  1. クライアントは多種多様 • ウェブ ◦ 普通のウェブ ◦ PWA • スマホアプリ ◦

    ネイティブ ◦ クロスプラットフォーム • スマートスピーカー • その他 ◦ Slack ◦ LINE… etc 12
  2. • クライアントによって返すものが異なるのはそもそもどうなのか ◦ ウェブだったらHTML ◦ でも非同期でのリクエストだったら JSON ◦ アプリなら常にJSON •

    クライアントが増えてパターンが増えてくると見通し悪い ◦ DRYにしづらくなる • → バックエンドはデータの登録、管理、取得が責務 バックエンドはデータが責務 14
  3. • メリット ◦ ウェブ用とアプリ用の2種類のエンドポイントを用意する必要がなくなる ▪ →共通化できる ◦ その他のクライアントにも対応しやすい • デメリット

    ◦ セッション管理や認証周りがちょっと手間になる ◦ フロントの分リポジトリや用意する環境が増える RailsはAPIサーバーに使用 15
  4. CDNでのキャッシュ前提のAPI設計 • 誰でも見れる public なエンドポイント ◦ どのユーザーがAPI を叩いても値は同じ ◦ 処理内で

    current_user を使わない • 認証状態によってユーザー固有の情報を返すエンドポイント ◦ API を叩くユーザーによって値が変わる ◦ 処理内で current_user を使う 28
  5. # examples_controller.rb before_action :require_api_key, only: [:create, :destroy, :update] before_action :optional_api_key,

    only: [:index, :show] before_action :cdn_cache_long, only: [:index, :show] def index @examples = Example.all end … def create @examples = current_user.examples.create!(example_params) end … キャッシュの機構の例 29
  6. キャッシュの機構の例 # application_controller.rb def require_api_key raise StandardError, 'require_api_key not allowed

    after cdn_cache called' if @cdn_cache_called @require_api_key_called = true @current_user = User.find_by(api_key: api_key) unless api_key.blank? render json: { error: 'Api-Keyが有効ではありません ' }, status: :unauthorized unless @current_user end def cdn_cache(s_maxage) raise StandardError, 'cache not allowed' if @require_api_key_called @cdn_cache_called = true expires_in(0, 's-maxage': s_maxage, public: true, must_revalidate: true) end def current_user raise StandardError, 'current_user not allowed' unless @require_api_key_called @current_user end 30
  7. swagger_path '/api/stories/{url_hash}' do operation :get do key :summary, 'url_hashを指定してストーリーを取得 '

    parameter do key :name, :url_hash key :in, :path key :description, 'storyのurl_hash' key :required, true key :type, :string end response 200 do key :description, 'ok' schema do key :'$ref', :StoryResponse end end end swagger-blocksでRubyで記述 39
  8. • クライアントが使うコードが生成できる ◦ 型情報 ◦ SDK(リクエストを飛ばす部分) • メジャーな言語には対応 ◦ Swift

    ◦ Kotlin ◦ Java ◦ TypeScript(Fetch, Angular… etc) ◦ … and more 42 Swagger Codegenでコード生成
  9. 49 • pixiv chatstoryのiOSアプリをReact Native(Expo)でリニューアルしました! ◦ https://inside.pixiv.blog/tawachan/6254 • pixiv chatstory

    の PWA としての取り組み ◦ https://speakerdeck.com/ikasoumen/pixiv-chatstory-false-pwa-tositefalseq u-rizu-mi 【参考】フロント周りの話ができなかったので 関連記事をよろしければ