Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloudflare Workers は楽しい!

Kei Kamikawa
November 24, 2023

Cloudflare Workers は楽しい!

2023-11-24 の Cloudflare Workers Meetup by NOT A HOTEL で話ました
https://notahotel-cf.peatix.com/

Kei Kamikawa

November 24, 2023
Tweet

More Decks by Kei Kamikawa

Other Decks in Programming

Transcript

  1. ΞϓϦέʔγϣϯͷ։ൃ؀ڥΛ੔͑Δ $ tree my-app --gitignore my-app ├── README.md ├── package.json

    ├── src │ └── index.ts ├── tsconfig.json 
 └── wrangler.toml
  2. import { Hono } from "hono"; const app = new

    Hono<{ Bindings: { ENV: string; }; }>(); app.get("/", (c) => c.text(c.env.ENV)); export default app; ؀ڥม਺Λ੔͑Δ JOEFYUT
  3. ؀ڥม਺Λ੔͑Δ $ cd my-app && npm run dev 
 $

    curl http://localhost:8787 
 local
  4. ϛυϧ΢ΣΞΛ੔͑Δʢྫ,7ʣ $ npx wrangler kv:namespace create TEST_KV ⛅ wrangler 3.17.1

    ------------------- 🌀 Creating namespace with title "my-app-TEST_KV" ✨ Success! Add the following to your configuration file in 
 your kv_namespaces array: { binding = "TEST_KV", id = "da56a0e3ee6641fabf26b726e60fa550" }
  5. const app = new Hono<{ Bindings: { TEST_KV: KVNamespace }

    }>(); app.get("/counter", async (c) => { const key = c.req.query()["key"] || "defaultKey"; const count = Number(await c.env.TEST_KV.get(key)); const count = !isNaN(value) ? value + 1 : 1; await c.env.TEST_KV.put(key, String(count)); return c.text(String(count)); }); export default app; JOEFYUT ϛυϧ΢ΣΞΛ੔͑Δʢྫ,7ʣ
  6. σϓϩΠ͢Δ $ npx wrangler deploy src/index.ts 
 ⛅ wrangler 3.17.1

    ------------------- Your worker has access to the following bindings: - KV Namespaces: - TEST_KV: da56a0e3ee6641fabf26b726e60fa550 Total Upload: 45.37 KiB / gzip: 10.97 KiB Uploaded my-app (0.93 sec) Published my-app (0.45 sec) https://my-app.example.workers.dev Current Deployment ID: 3fa11a11-bb8e-4442-bafb-4d16b157ee34
  7. σϓϩΠ؀ڥΛෳ਺༻ҙ͢Δ name = "my-app-dev" 
 [[env.prd]] name = "my-app-prd" [env.prd.vars]

    ENV = "production" 
 
 [[env.prd.kv_namespaces]] binding = "TEST_KV" id = "6054ddfbe0bc4f00b1abaa74af92b413" XSBOHMFSUPNM
  8. ։ൃεςʔδ΁σϓϩΠ͢Δ $ npx wrangler deploy src/index.ts 
 ⛅ wrangler 3.17.1

    ------------------- Your worker has access to the following bindings: - KV Namespaces: - TEST_KV: da56a0e3ee6641fabf26b726e60fa550 Total Upload: 45.37 KiB / gzip: 10.97 KiB Uploaded my-app-dev (0.93 sec) Published my-app-dev (0.45 sec) https://my-app-dev.example.workers.dev Current Deployment ID: 3fa11a11-bb8e-4442-bafb-4d16b157ee34
  9. ຊ൪εςʔδ΁σϓϩΠ͢Δ $ npx wrangler deploy src/index.ts —-env prd 
 ⛅

    wrangler 3.17.1 ------------------- Your worker has access to the following bindings: - KV Namespaces: - TEST_KV: 6054ddfbe0bc4f00b1abaa74af92b413 Total Upload: 45.37 KiB / gzip: 10.97 KiB Uploaded my-app-dev (0.93 sec) Published my-app-prd (0.45 sec) https://my-app-prd.example.workers.dev Current Deployment ID: 7219bd7f-e4d3-47d0-970b-5917523da603