Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Axum+RustをWebAssemblyを使ってCloudflare Workersで動かしてみた
Search
PharmaX(旧YOJO Technologies)開発チーム
October 16, 2023
1.5k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Axum+RustをWebAssemblyを使ってCloudflare Workersで動かしてみた
axumで開発中のSlackbotをCloudflare Workersで動かす際に得た知見をシェアします。
PharmaX(旧YOJO Technologies)開発チーム
October 16, 2023
More Decks by PharmaX(旧YOJO Technologies)開発チーム
See All by PharmaX(旧YOJO Technologies)開発チーム
LLMアプリケーション評価駆動開発の歩き方
pharma_x_tech
3
510
LLMアプリケーションやAIエージェントの評価の基本
pharma_x_tech
1
440
「動くだけ」で終わらせない ― LLMアプリを継続的に改善・運用するための評価駆動開発
pharma_x_tech
2
54
LangSmithによるAIエージェントの継続的改善のためのオブザーバビリティの向上
pharma_x_tech
1
350
PdMによるLiveバイブコーディング〜プロトタイプ開発実践〜
pharma_x_tech
1
97
2025.10.28_CodexとClaude Codeの比較検討 社内座談会
pharma_x_tech
2
680
LLMのアウトプットの評価と改善 〜DSPyによるプロンプト最適化入門によせて〜
pharma_x_tech
6
1.3k
2025.09.02_AIコーディングを利用した開発自動化を目指しての座談会
pharma_x_tech
5
390
AIコーディングを前提にした開発プロセス再設計〜開発生産性向上に向けた試行錯誤〜
pharma_x_tech
4
480
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
699
190k
Color Theory Basics | Prateek | Gurzu
gurzu
1
470
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
420
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
Building AI with AI
inesmontani
PRO
1
1.3k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Prompt Engineering for Job Search
mfonobong
0
460
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
920
The Cost Of JavaScript in 2023
addyosmani
55
10k
Transcript
Axum+Rustを WebAssemblyを使って Cloudflare Workersで 動かしてみた 2023/10/18 古家 大
(C)PharmaX Inc. 2023 All Rights Reserve 2 自己紹介 • YOJO事業部
エンジニアリーダー • 開発生産性集計SlackBotをRustで 開発中です • 息子の運動会のためにリングフィッ ト再開しました 古家 大 X(旧Twitter)
(C)PharmaX Inc. 2023 All Rights Reserve 3 今回やりたいこと Axum+Rustで開発中のSlackbotをCloudflare Workersで動かす
(C)PharmaX Inc. 2023 All Rights Reserve 4 rustupでRust環境をインストール Rust開発環境の準備
(C)PharmaX Inc. 2023 All Rights Reserve 5 rust-analyzerの導入 Rust開発環境の準備
(C)PharmaX Inc. 2023 All Rights Reserve 6 型推論による返り値の型の表示 rust-analyzerの機能例
(C)PharmaX Inc. 2023 All Rights Reserve 7 メソッド呼び出し時の引数情報の表示 rust-analyzerの機能例
(C)PharmaX Inc. 2023 All Rights Reserve 8 定義へのジャンプ機能 rust-analyzerの機能例
(C)PharmaX Inc. 2023 All Rights Reserve 9 Rust開発環境の準備 Even Better
TOMLを導入 (.tomlのシンタックスハイライト用)
(C)PharmaX Inc. 2023 All Rights Reserve 10 WranglerでCloudflare Workersのワーカーの雛形作成 npx
wrangler generate [プロジェクト名] https://github.com/cloudflare/workers-sdk/templates/experimental/worker-rust
(C)PharmaX Inc. 2023 All Rights Reserve 11 Cloudflare Workersについて •
Cloudflare社のCDNのエッジでJavaScriptを動かせる • 他にもVercel Edge Functions・Deno Deployとか色々ある • コンテナを使用しないサーバーレス環境
(C)PharmaX Inc. 2023 All Rights Reserve 12 Cloudflare Workersの対応言語 •
JavaScript / TypeScriptが基本 • JavaScriptにコンパイルできる他言語もサポート (Kotlin / Dart / Python / Scala / Perl / PHP / OCaml / F#) • WebAssembly(Wasm)に対応しているので、Rust・C・C++等でコンパ イルしたバイナリを使ってネイティブに近い速度で実行可 → Rustで書いたコードを動かすにはWasmコンパイルが必要
(C)PharmaX Inc. 2023 All Rights Reserve 13 WebAssemblyについて • 元々Webブラウザ上でJavaScriptでは荷が重い処理(グラフィック処理な
ど)を高速に実行できるバイナリーフォーマットとして開発 • 2017年に主要なブラウザはすべてサポートを開始 • 今ではWebブラウザ以外でもエッジコンピューティングなど 高速動作が求められる環境で活用が広まっている
(C)PharmaX Inc. 2023 All Rights Reserve 14 WranglerでCloudflare WorkersにRustアプリをデプロイ •
npx wrangler deployをするだけでWasmコンパイルされ秒速デプロイ • CloudFlareのアカウント登録は事前に必要 • 無料でhttps://プロジェクト名.サブドメイン.workers.dev を発行
(C)PharmaX Inc. 2023 All Rights Reserve 15 axumをCloudflare Workersで動かすには •
そのままだとCloudflare Workersとaxumが競合して動かない • リクエストとレスポンスの型が異なるため変換が必要だったり (Worker::Request ⇔http::Request) • Cloudflare Workersのworker::ENVがAxumルート内で使えなかったり • 上記を行うため axum-cloudflare-adapter というクレートを使う
(C)PharmaX Inc. 2023 All Rights Reserve 16 Cargo.tomlはaxum-cloudflare-adapterに合わせる https://github.com/logankeenan/axum-cloudflare-adapter/blob/main/example/Cargo.toml 依存関係がシビアで
バージョンまで合わせないとな かなか動かない
(C)PharmaX Inc. 2023 All Rights Reserve 17 axumをWasm対応させる • axumのデフォルト機能の一つであるtokioを無効化(wasm未対応のため)
https://docs.rs/tokio/latest/tokio/#wasm-support • 他のデフォルト機能も無効化するため必要なjson機能は明示的に有効化 https://docs.rs/axum/latest/axum/#feature-flags
(C)PharmaX Inc. 2023 All Rights Reserve 18 axum-cloudflare-adapterを使った書き方でmainを更新 • workerとaxumの
request・responseを変 換 • Workerのenvをラッパー 用の構造体に格納して State管理
(C)PharmaX Inc. 2023 All Rights Reserve 19 axumのStateを利用した環境変数の渡し方 • 状態管理のためのStateという概念はaxum
v0.6から導入された • axum+Cloudflare WorkersではWorkerで設定した環境変数をstateで渡す
(C)PharmaX Inc. 2023 All Rights Reserve 20 Cloudflare Workersでの環境変数の設定方法
(C)PharmaX Inc. 2023 All Rights Reserve 21 Cloudflare Workersでの環境変数の設定方法 •
SecretsもGUIから設定可能
(C)PharmaX Inc. 2023 All Rights Reserve 22 Slackbotの認証処理でSecretsを読み取り
(C)PharmaX Inc. 2023 All Rights Reserve 23 Slackbotの認証処理でSecretsを読み取り
(C)PharmaX Inc. 2023 All Rights Reserve 24 axumをCloudflare Workersで使ってみて •
現状、axum-cloudflare-adapterを使うしかなさそう • Cloudflare WorkersやWasm対応のまとまったドキュメントがないので大変 • Cloudflare Workersの爆速デプロイ・レスポンスには感動したので 引き続きSlackbot開発など小さい所から使っていきたい (個人利用なら無料でも十分そう) • axum-cloudflare-adapterのコードリーディングを通して Rustやaxumの仕組みについて理解が深まった
(C)PharmaX Inc. 2023 All Rights Reserve 25 ご清聴ありがとうございました!