高木のブログ

Sinatra アプリを Cloud Functions にデプロイする

· 91 words · 1 minutes to read

Sinatra アプリのデプロイ先を探していて、Cloud Fucntions が候補に上がったので簡単に肩慣らし

functions_framework gem のリポジトリにサンプル が用意されていたのでそれを参考にした

手順 🔗

既に Sinatra アプリをモジュラースタイルで作ってある前提

Functions Framework の導入 🔗

gem を追加し、リクエストが来たら Sinatra アプリを呼び出すコードを追記をするだけ

 # frozen_string_literal: true

 source "https://rubygems.org"

 git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

 gem "sinatra"
+gem "functions_framework"
 # frozen_string_literal: true

 require "sinatra/base"
+require "functions_framework"

 class App < Sinatra::Application
   get "/" do
     "Hello world!\n"
   end
 end

+FunctionsFramework.http "sinatra_example" do |request|
+  App.call request.env
+end

ローカルで起動 🔗

$ bundle exec functions-framework-ruby --target sinatra_example
$ curl localhost:8080
Hello world!

Cloud Functions にデプロイ 🔗

$ gcloud functions deploy sinatra_example --runtime ruby27 --trigger-http
$ curl https://hogehoge.cloudfunctions.net/sinatra_example
Hello world!

リポジトリ 🔗

今回作ったものは、GitHub にあげた
https://github.com/ytkg/cloud-functions-sinatra

Categories


Tags