0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cloudflareを利用してwebアプリを公開したい!part2(Workersセットアップ)

Posted at

はじめに

 前回記事の続きです。
 前回は、cloudflare Pagesを使ってwebページをデプロイするところまでやりました。
 今回は、cloudflare Workersを使ってバックエンドを書こうと思います。

下準備

 公式ドキュメントに書いてありましたが、RustでWorkersを作成するときにworkers-rsが使えると楽です。
workers-rsをgenerateするため事前にcargo-generateをinstallしておきましょう。

cargo install cargo-generate

cargo-generateをinstallしたらworkers-rsを生成します。

PS G:\[ルートディレクトリ]> cargo generate cloudflare/workers-rs
 Favorite `cloudflare/workers-rs` not found in config, using it as a git repository: https://github.com/cloudflare/workers-rs.git
✔  Which template should be expanded? · templates\axum
 Project Name: [プロジェクト名]
 Destination: G:\[ルートディレクトリ]\[プロジェクト名] ...
 project-name: function ...
 Generating template ...
 Moving generated files into: `G:\[ルートディレクトリ]\[プロジェクト名]`...
 Initializing a fresh Git repository
 Done! New project created G:\[ルートディレクトリ]\[プロジェクト名]

 自分で操作する必要があるのはテンプレートの選択とプロジェクトネームの入力だけです。
 axumのお勉強もしてみたいのでaxumを選択しました。今回の要件と合わなければ後で変えるかもしれません。
 
 生成されたら以下のコマンドで初回ビルドします。

npx wrangler dev

 途中で何度か選択肢が出ますが基本的にyesで問題ないです。
 以下のように表示されたら起動完了です。curl等でlocalhost:8787に接続し確認してみましょう。
(画像では確認のために一度GETメソッドを実行したのでGET / 200 OKが表示されています。)
workers run dev preview.png

デプロイ

 part1ではcloudflareのダッシュボードからgitと連携させてPagesのデプロイをしましたが、今回はwranglerを使ってCLIからデプロイします。
 コマンドは以下の通り

npx wrangler deploy

 成功したらcloudflareのページが開かれ、デプロイの確認をされますのでこれを認証します。
 その後、Workers&Pagesにデプロイしたワーカーが配置されていればOKです。

おわりに

 次回はpart1で作成したページから今回作成したWorkerを呼び出してみようと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?