Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Deno Deployと組み合わせるのに Upstashをおすすめしたい
Search
虎の穴ラボ株式会社
August 31, 2022
Technology
0
310
Deno Deployと組み合わせるのに Upstashをおすすめしたい
Deno Deployと組み合わせるのに Upstashをおすすめしたい
虎の穴ラボ株式会社
August 31, 2022
Tweet
Share
More Decks by 虎の穴ラボ株式会社
See All by 虎の穴ラボ株式会社
FreshとHonoでDeno KVを使い倒す
toranoana
1
45
サブカル業界Developers_実践_開発業務でのAIの活用
toranoana
1
280
「エンジニアリングマネージャーのしごと」勉強してみた
toranoana
2
250
【OSC2024 Online/Spring】とらのあなラボの Deno イベント開催の取り組み
toranoana
0
290
【toranoana.deno#15】WebGPUで遊ぼう
toranoana
0
320
App Router による Web 開発について
toranoana
0
93
Fresh(Deno)をプラグインで拡張しよう!
toranoana
0
220
Freshプラグインのテストもプラグインを使うと捗る!
toranoana
0
120
個人開発アプリにスキーマ 駆動開発を取り入れた話
toranoana
0
80
Other Decks in Technology
See All in Technology
推しは推せるときに推せ! プロダクトにフィードバックしていこう
nakasho
0
310
20240418_Google ColabにLLMが搭載されたようなのでPython x データ分析の勉強方法を考えてみる
doradora09
0
130
On Your Data を超えていく!
hirotomotaguchi
2
680
競技としてのKaggle、役に立つKaggle
yu4u
3
1.3k
長期間TiDBを使ってきた話 @ 私たちはなぜNewSQLを使うのかTiDB選定5社が語る選定理由と活用LT / Experiences with TiDB Over Time
chibiegg
2
900
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
160
元インフラエンジニアに成る / Human Resources to Human Relations
bobtani
4
920
GraphQL 成熟度モデルの紹介と、プロダクトに当てはめた事例 / GraphQL maturity model
mh4gf
7
1.3k
Cracking the KubeCon CfP
inductor
2
250
アクセス制御にまつわる改善 / Improving access control
itkq
0
540
開発生産性大幅アップ!Postman VS Code拡張機能
nagix
2
380
エンジニア候補者向け資料2024.04.24.pdf
macloud
0
3.3k
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
325
20k
Side Projects
sachag
451
41k
The Mythical Team-Month
searls
216
42k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
2
1.3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
648
58k
Fireside Chat
paigeccino
21
2.6k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
Designing the Hi-DPI Web
ddemaree
276
33k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
Scaling GitHub
holman
457
140k
Transcript
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. 2022/08/24 toranoana.deno
#8 虎の穴ラボ 奥谷 一陽 Deno Deployと組み合わせるのに upstashをおすすめしたい
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. 自己紹介 奥谷
一陽 所属:虎の穴ラボ株式会社 担当:とらコインSHOPなど新規事業系の開発 興味:TypeScript、Deno おすすめコンテンツ: 『プラネテス』 『暴太郎戦隊ドンブラザーズ』 Twitter:@okutann88
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy
どのくらい使っ てますか?
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy
と組み合わせる前提で、 オススメSaaSを紹介したい
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash
参考:https://upstash.com/
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash -
カルフォルニアの Upstash, Inc. が運営するSaaS - Redis Kafka qStashを提供 - キーワード:グローバル 低レイテンシ 上限付きの従量課金
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash の
Redis - 無料プラン:10K コマンド/日 - 有料プラン:0.2 $/100Kコマンド、月間最大 $120 まで - 公式が、deno.land/x にモジュールを公開済み https://deno.land/x/
[email protected]
- Redisと銘打つものの、内部でfetchが使用されたhttpリクエストを使用 「Redisっぽく動く」 「Redis互換の」ぐらいの感触
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash の
Redis の使用例 import { Application, Router, Context } from "https://deno.land/x/oak/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL"), token: Deno.env.get("UPSTASH_TOKEN"), }); const router = new Router(); router.get("/", async (context: Context) => { const count = await redis.get("count"); context.response.body = count; }); router.post("/update", async (context: Context) => { await redis.incr("count"); context.response.body = "updated"; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 });
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash の
Redis で実際にやりたいのは? - キャッシュの仕組みが有ったときに、Webサービスで扱いたいのは 「セッション」では無いだろうか(偏見かもしれません) - oak_session モジュール + upstash_redis モジュール は組み合わせ可能? => できる ただし...、 オプションが必須
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. upstash の
Redis を oak_session で使う import { Application, Context, Router } from "https://deno.land/x/oak/mod.ts"; import { RedisStore, Session } from "https://deno.land/x/oak_sessions/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL")!, token: Deno.env.get("UPSTASH_TOKEN")!, automaticDeserialization: false, // <== ポイント }); const router = new Router(); router.get("/", async (context: Context) => { const name = await context.state.session.get("name"); context.response.body = ` <!DOCTYPE html> <html> <body> <div> ${!name ? "" : "name=" + name} </div> <form method="POST"> <input name="name"> <button type="submit">submit</button> </form> </body> </html> `; }); router.post("/", async (context: Context) => { const form = await context.request.body({ type: "form" }).value; const name = form.get("name"); if (!!name) context.state.session.set("name", name); context.response.redirect("/"); }); type AppState = { session: Session; }; const app = new Application<AppState>(); const store = new RedisStore(redis); app.use(Session.initMiddleware(store)); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 }); upstash_redisは、デフォルトで、jsonの自動デシリアライズが ONになっているので この機能を停止させる デシリアライズは、oak_session に任せる!
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. qStash ?
- upstashの独自サービス - サーバーレス、エッジランタイム向け、HTTPベースメッセージング/ スケジューリングソリューション - 無料プラン:50リクエスト/日 - 有料プラン:50Kリクエスト/日 0.4 $/1Kリクエスト 月間最大 $450 - 最大のポイント Deno Deploy をスケジュール実行できる => さらに、upstash公式が deno deploy をサポート
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. qStash の
deno deploy サポート
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deployで
qStash 使ってみる 以下の内容を実装する - qStashから1分に1回 Deno Deploy に置いたアプリを呼び出す - 呼び出しに対応して upstash Redisに保管した値をカウントする - ブラウザからアクセスし値を参照する
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deployで
qStash 使ってみる ポイント - 公式が、deno.land/x にモジュールを公開済み - https://deno.land/x/
[email protected]
- qStashからのリクエストであることを upstash_qstash モジュールを 使い検証すること
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy
で qStash を使う実装例 import { Application, Router, Context } from "https://deno.land/x/oak/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; import { Receiver } from "https://deno.land/x/upstash_qstash/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL"), token: Deno.env.get("UPSTASH_TOKEN"), }) const receiver = new Receiver({ currentSigningKey: Deno.env.get("QSTASH_CURRENT_SIGNING_KEY")!, nextSigningKey: Deno.env.get("QSTASH_NEXT_SIGNING_KEY")!, }); const router = new Router(); router.get("/", async(context:Context) => { const count = await redis.get("count"); context.response.body = count; }); router.post("/update", async(context:Context) => { const isValid = await receiver.verify({ // <== ポイント signature: context.request.headers.get("Upstash-Signature")!, body: await context.request.body({ type: "text" }).value, }).catch((err: Error) => { console.error(err); return false; }); if (!isValid) { return new Response("Invalid signature", { status: 401 }); } await redis.incr("count"); return new Response("OK", { status: 200 }); }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 });
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. 現在動作中です。 一分に一回カウントアップされます。
https://late-otter-59.deno.dev/
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. まとめ -
Deno Deployの有効活用のため、upstashの組み合わせオススメ - 出来ることが、大きく広がる - Deno Deploy 起点のフルサーバーレスなサービスを開発できる可能性 例えば?
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy
起点フルサーバーレス サービス インフラ 構想 deno deploy ・APIサーバー ・DB管理 ・ストレージ キャッシュ呼び出し Redis qStash バッチ処理呼び出し フロントサーバー
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy
起点フルサーバーレス サービス インフラ 構想 deno deploy フロントサーバー ・APIサーバー ・DB管理 ・ストレージ キャッシュ呼び出し Redis qStash バッチ処理呼び出し deno deploy + + bunny cdn ... API呼び出し
Copyright (C) 2021 Toranoana Inc. All Rights Reserved. ありがとうございました