Fastly Compute
Fastly Compute は好きな言語で書かれたコードをグローバルネットワーク上で実行できる高度なエッジコンピューティングシステムです。 Hono はもちろん Fastly Compute 上でも動作します。
Fastly CLI を使用すると、少しのコマンドでアプリケーションをローカルで開発して、公開できます。
1. Setup
スターターは Fastly Compute でも使用できます。 "create-hono" コマンドでプロジェクトを開始しましょう。 fastly テンプレートを選択します。
npm create hono@latest my-appyarn create hono my-apppnpm create hono my-appbun create hono@latest my-appdeno init --npm hono my-appmy-app に移動して依存関係をインストールします。
cd my-app
npm icd my-app
yarncd my-app
pnpm icd my-app
bun i2. Hello World
src/index.ts を変更します:
// src/index.ts
import { Hono } from 'hono'
import { fire } from '@fastly/hono-fastly-compute'
const app = new Hono()
app.get('/', (c) => c.text('Hello Fastly!'))
fire(app)NOTE
When using fire (or buildFire()) from @fastly/hono-fastly-compute' at the top level of your application, it is suitable to use Hono from 'hono' rather than 'hono/quick', because fire causes its router to build its internal data during the application initialization phase.
3. Run
ローカルで開発サーバーを起動し、ブラウザで http://localhost:7676 にアクセスしてください。
npm run startyarn startpnpm run startbun run start4. デプロイ
ビルドして、あなたの Fastly アカウントにデプロイするには以下のコマンドを実行します。 あなたがアプリケーションを始めてデプロイする場合、アカウントに新しいサービスを作成するように求められます。
まだアカウントを持っていない場合は、作成する必要があります。
npm run deployyarn deploypnpm run deploybun run deployBindings
In Fastly Compute, you can bind Fastly platform resources, such as KV Stores, Config Stores, Secret Stores, Backends, Access Control Lists, Named Log Streams, and Environment Variables. You can access them through c.env, and will have their individual SDK types.
To use these bindings, import buildFire instead of fire from @fastly/hono-fastly-compute. Define your bindings and pass them to buildFire() to obtain fire. Then use fire.Bindings to define your Env type as you construct Hono.
// src/index.ts
import { buildFire } from '@fastly/hono-fastly-compute'
const fire = buildFire({
siteData: 'KVStore:site-data', // I have a KV Store named "site-data"
})
const app = new Hono<{ Bindings: typeof fire.Bindings }>()
app.put('/upload/:key', async (c, next) => {
// e.g., Access the KV Store
const key = c.req.param('key')
await c.env.siteData.put(key, c.req.body)
return c.text(`Put ${key} successfully!`)
})
fire(app)