3. この Lightning Talk は、
Free your Google Sheets data: build a REST API with Cloud Run
https://www.youtube.com/watch?v=V1Fqv8sGqWk&list=PLIivdWyY5sqKiWvnaA5A8F3UQ0Xu5i49U&index=2
…の基本情報を元に、
fully managed Cloud Run で、
Dart言語から Google API を使った
ステートレスな REST API サービスの作り方
の初歩についての説明です。
3
5. Flutter アプリ開発で、ちょっとした WEB API が必要な時
ステートレスなサービスであれば、
Firebase Cloud Functions を使いたいところです…が、
現時点では、dart で実行させることはできません。
Firebase チームでは、
Cloud Run を使うことを勧めています。
5
6. Q. when will dart officially be supported for writing Cloud Functions?
Cloud Functions で dart が正式にサポートされるのはいつですか?
A. So at the moment, you can't run dart on functions,
現時点では、Cloud Functions で dart を実行することはできません。
6
APIs for Firebase console, Dart in Cloud Run, & more! #AskFirebase
https://www.youtube.com/watch?v=2hcHQtuR6l0&feature=youtu.be&t=233
7. A. but what you might be interested in is something called Cloud Run.
ですが、Cloud Run と呼ばれるものに、惹きつけられるかもしれませんね。
7
Firebase チームは、Cloud Run を使うことを勧めています。
9. Cloud Run は、2タイプあることに注意
● fully managed Cloud Run
Cloud Run から実行されるサービスのコンテナは、
Google が暗黙で提供する Knative 互換環境のクラスタにデプロイされます。
Knative の存在を意識しなくて済むよう隠してくれます。
● Cloud Run for Anthos / Cloud Run on GKE
コンテナは、自分が管理するオンプレミス、または GCP 上の
Anthos GKE クラスタにデプロイすることになります。
カスタマイズ性は高いが、Knative の存在を意識する必要があります。
9
Differences between Cloud Run and Cloud Run on GKE
https://www.youtube.com/watch?v=RVdhyprptTQ
10. Cloud Run / Quickstart (dart 版 注意事項)
Dart版 サンプルは、Cloud Run for Anthos になっています
fully managed 版は、Steve Hamblett さんのブログが参考になります。
10
Dart on Googles Cloud Run platform, an introduction (2019/05/19)
https://medium.com/@stevehamblett/dart-on-googles-cloud-run-platform-an-introduction-346feb49cbbb
shamblett/cloudruntest Hello World - Dar sample (fully managed 版)
https://github.com/shamblett/cloudruntest
Quickstart: Build and Deploy (Cloud Run 公式の Hello-World チュートリアル)
https://cloud.google.com/run/docs/quickstarts/build-and-deploy
Hello World - Dart sample (Cloud Run on GKE 版)
https://github.com/knative/docs/tree/master/community/samples/serving/helloworld-dart
https://github.com/knative/docs/tree/175313457f94baa036b400f12d162157edef70a7/community/samples/serving/helloworld-dart
11. Cloud Run で、REST API を作る入門動画
11
Serverless Toolbox
https://www.youtube.com/playlist?list=PLIivdWyY5sqKiWvnaA5A8F3UQ0Xu5i49U
Serverless Toolbox 動画リストの
Free your Google Sheets data では、
Google API を使った REST API 作成
の基本が説明されています。
…が、dart 対応に必要な知見は未提供です。
Free your Google Sheets data: build a REST API with Cloud Run
https://www.youtube.com/watch?v=V1Fqv8sGqWk&list=PLIivdWyY5sqKiWvnaA5A8F3UQ0Xu5i49U&index=2
serverlesstoolbox/rest-api-sheet/
https://github.com/momander/serverlesstoolbox/tree/master/rest-api-sheet
Node.js 版なので、 dart に応用するには、独自調査と確認が必要なことに注意
12. Cloud Run with Google API
at the dart language
fully managed Cloud Run での
dart言語で Google API を使った REST API の作り方
12
13. Free your Google Sheets data でのキーポイント
● Google API を利用するための資格証明(認証)を簡便にするため、
Cloud Run デフォルトのサービスアカウントの資格証明情報を暗黙的に
使う簡易な手法を用いて、HTTP Client オブジェクトを取得している。
● 取得した HTTP Client から、
Google API Client ⇒ Google Sheets v4 API Client を生成している。
● SlipSlap warehouse スプレッドシート ID を指定して、
指定 Range(タブ名+A1:E21などの範囲)のセルデータを取得している。
SlipSlap warehouse は、 共有 設定で誰でも閲覧可能になっているので、
シート共有への サービスアカウントID (メールアドレス)の登録は不要です。
スプレッドシート ID ⇒ 1rcj3SbeK_VcMOBrwwdAksJXQVoHTZHRzVOBO8A3X148
13
SlipSlap warehouse スプレッドシート(Ingredients タブ)
https://docs.google.com/spreadsheets/d/1rcj3SbeK_VcMOBrwwdAksJXQVoHTZHRzVOBO8A3X148/edit#gid=0
14. Cloud Run デフォルト サービスアカウント
● Cloud Run で実行されるサービスアプリは、
サービスアカウントID ⇒ PROJECT_NUMBER-compute@developer.gserviceaccount.com の
GCP プロジェクトの Compute Engine default service account が紐付き、
GCP プロジェクトの全リソースへの Read/Write のアクセス権限を持つ。
● Google Cloud Platform API や Google API は、
利用元が許可を与えたアカウントと認証できなければ利用させません。
ですが、GCP API client library や Google API client library では、
簡便化のため、デフォルトのサービスアカウント暗黙的に使った、
暗黙的認証を利用する(認証を隠蔽化する)こともできます。
14
Service identity
https://cloud.google.com/run/docs/securing/service-identity
15. 作業手順概要
1. GCP プロジェクトを作成する。(課金が有効になっていること!)
2. GCP プロジェクトの Cloud Run を有効にする。
Cloud Build API Cloud Run API のアプリケーションを登録…を行います。
3. GCP プロジェクトの API と サービス > ライブラリ > Google Sheets API を有効にする。
4. dart プロジェクト(PJ)をローカルに作成する。
pubspec.yaml と Dockerfile および サーバ起動 と ロジック の テンプレート追加
5. Cloud Run デフォルトのサービスアカウントを利用した、
暗黙的な資格証明(認証)から HTTP Client を取得するコードをPJに追加。
6. HTTP Client から、Google sheets v4 API クライアントを作成して、
シートアクセス結果を返すコードをPJに追加。
7. gcloud ビルド&デプロイコマンド で、Cloud Run ビルドとデプロイを行う。
gcloud コマンドのインストールやコンポーネントアップデートも忘れずに。
15参照 Quickstart: Build and Deploy ⇒ https://cloud.google.com/run/docs/quickstarts/build-and-deploy#follow-cloud-run
17. pubspec.yaml 構成例
17
name: cloud_run_sheets
description: A Cloud Run with Google API example by Dart language.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
# google api client library
googleapis: ^0.54.0
# google auth client library (for google api credential certification)
googleapis_auth: ^0.2.11
# dart http server
shelf: ^0.7.5
18. dependencies で導入するパッケージ
● googleapis 0.54.0 ⇒ https://pub.dev/packages/googleapis
Google API クライアントを提供する、公式 Dart クライアントライブラリ。
Google Sheet v4 を含む、各種 Google API クライアントオブジェクトは、
Google API 資格証明(認証)で取得した HTTP Client を使って生成します。
● googleapis_auth 0.2.11 ⇒ https://pub.dev/packages/googleapis_auth
公式 Dart 用の Google API OAuth2 クライアントライブラリ。
Dart で、クライアントの Google API 資格証明(認証)を行い、
OAuth2 認証に成功すれば、HTTP Client オブジェクトを返します。
● shelf 0.7.5 ⇒ https://pub.dev/packages/shelf
Dartチームが開発している、Webサーバ・ミドルウェア。
Webサーバとしてだけでなく、Webサーバ部分機能のみの提供も可能。
18
19. Dockerfile 構成例
19
# Use Google's official Dart image.
# https://hub.docker.com/r/google/dart-runtime/
FROM google/dart-runtime
# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080
20. bin/server.dart コード例(1/2)
20
import 'dart:io';
// HTTP Server shelf ユーティリティ・メソッド関連パッケージ
import 'package:shelf/shelf_io.dart' as shelf;
// HTTP Server shelf クラス関連パッケージ
import 'package:shelf/shelf.dart';
import 'package:cloud_run_sheets/service.dart';
/// Google Sheets データ取得サービス・サーバ起動
///
/// 【参考元】
/// Cloud Run
/// Quickstart: Build and Deploy より、OTHER > Dart サンプル・リポジトリ
/// https://github.com/knative/docs/blob/master/community/samples/serving/helloworld-dart/bin/server.dart
21. bin/server.dart コード例(2/2)
21
void main() async {
var port = int.tryParse(Platform.environment['PORT']) ?? 8080;
Future<Response> Function(Request) service = (Request request) async {
// Google Sheet v4 API で、指定スプレッドシートのデータを JSON 文字列形式で取得する。
String response = await new SheetsService().doService();
return Response.ok(response);
};
Handler handler = Pipeline().addMiddleware(logRequests()).addHandler(service);
HttpServer server = await shelf.serve(handler, InternetAddress.anyIPv4, port);
print('Serving at http://${server.address.host}:${server.port}');
}
23. lib/service.dart コード例(1/7)
23
import 'dart:convert';
// Google Sheets v4 API クラス関連パッケージ
import 'package:googleapis/sheets/v4.dart';
// Google API Auth Client 生成ユーティリティ・メソッド関連パッケージ
import 'package:googleapis_auth/auth_io.dart' as auth;
// Google API Auth Client クラス関連パッケージ
import 'package:googleapis_auth/auth.dart';
/// Google Sheets からデータを取得するサンプル
class SheetsService {
35. コンテナデプロイ
● Cloud Run デプロイコマンド例
$ gcloud run deploy hello-sheets
--image gcr.io/project-123ab/cloud_run_sheets
--platform managed --region asia-northeast1 --allow-unauthenticated
gcloud run deploy SERVICE名 --platform managed --region REGION名 で、
アーティファクト指定された docker image を Container Registry の
REGION 指定されたプライベートレジストリに管理(保管/読出)させて、
Google が暗黙で提供する Knative 互換環境のクラスタにデプロイします。
--allow-unauthenticated 指定により、URLアクセス元認証を不要にします。
35
36. コンテナデプロイ(資料先)
● Deploying container images
https://cloud.google.com/run/docs/deploying
● Allowing public (unauthenticated) access
https://cloud.google.com/run/docs/authenticating/public
● gcloud run deploy (コマンドリファレンス)
https://cloud.google.com/sdk/gcloud/reference/run/deploy
36
37. デプロイ実行時出力例
37
注意)プロジェクトID や、サービス URL は、ダミーの値です。
$ gcloud run deploy hello-sheets --image gcr.io/project-123ab/cloud_run_sheets --platform managed
--region asia-northeast1 --allow-unauthenticated
Deploying container to Cloud Run service [hello-sheets] in project [project-123ab] region [asia-northeast1]
✓ Deploying new service... Done.
✓ Creating Revision... Revision deployment finished. Waiting for health check
to begin.
✓ Routing traffic...
✓ Setting IAM Policy...
Done.
Service [hello-sheets] revision [hello-sheets-00001-kit] has been deployed and is serving 100 percent of
traffic at https://hello-sheets-3fooo3barr-an.a.run.app
サービスURLは、メモしておきます。
43. その他 (GCP + Dart 関連資料)
● googleapis と googleapis_auth を使った、
Speech API や Sheets API の利用方法について説明しています。
Cloud Run を使っていませんが、
Google Auth API クライアントを使った明示的認証や、
Google API クライアントやライブラリについての情報があります。
43
Google Sheets API v4 for Flutter/Dart
https://stackoverflow.com/questions/46466410/google-sheets-api-v4-for-flutter-dart
Getting Started w/ Dart on GCP (2019/04/04)
https://medium.com/google-cloud/getting-started-with-dart-on-gcp-ffc49165d181
45. Cloud Run は、2タイプあることに注意
● fully managed Cloud Run
Cloud Run から実行されるサービスのコンテナは、
Google が暗黙で提供する Knative 互換環境のクラスタにデプロイされます。
Knative の存在を意識しなくて済むよう隠してくれます。
● Cloud Run for Anthos / Cloud Run on GKE
コンテナは、自分が管理するオンプレミス、または GCP 上の
Anthos GKE クラスタにデプロイすることになります。
カスタマイズ性は高いが、Knative の存在を意識する必要があります。
45
Differences between Cloud Run and Cloud Run on GKE
https://www.youtube.com/watch?v=RVdhyprptTQ
46. Cloud Run におけるサーバレスとは
● Cloud Run におけるサーバレスとは、
コンテナを使い、様々な言語やフレームワークが使える
セットアップ済の環境を利用できることで、
サーバの環境設定や管理に悩まされず、
単一でステートレスな HTTP駆動型アプリケーション/
マイクロサービスの開発のみに専念できることを示します。
補足:独自カスタマイズした環境を作るコンテナも利用できます。
46