Skip to content

Fastly 计算

🌐 Fastly Compute

Fastly Compute 是一个先进的边缘计算系统,可以在 Fastly 的全球边缘网络上以你喜欢的语言运行你的代码。Hono 也可以在 Fastly Compute 上运行。

你可以在本地开发应用,并通过使用 Fastly CLI 的几个命令来发布它,该 CLI 会作为模板的一部分自动在本地安装。

🌐 You can develop the application locally and publish it with a few commands using Fastly CLI, which is installed locally automatically as part of the template.

1. 设置

🌐 1. Setup

Fastly Compute 的入门模板已可用。使用 "create-hono" 命令开始你的项目。此示例请选择 fastly 模板。

🌐 A starter for Fastly Compute is available. Start your project with "create-hono" command. Select fastly template for this example.

sh
npm create hono@latest my-app
sh
yarn create hono my-app
sh
pnpm create hono my-app
sh
bun create hono@latest my-app
sh
deno init --npm hono my-app

移动到 my-app 并安装依赖。

🌐 Move to my-app and install the dependencies.

sh
cd my-app
npm i
sh
cd my-app
yarn
sh
cd my-app
pnpm i
sh
cd my-app
bun i

2. 你好,世界

🌐 2. Hello World

编辑 src/index.ts

🌐 Edit src/index.ts:

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

当在应用的顶层使用来自 @fastly/hono-fastly-compute'fire(或 buildFire())时,使用来自 'hono'Hono 而不是 'hono/quick' 是合适的,因为 fire 会导致其路由在应用初始化阶段构建其内部数据。

3. 跑

🌐 3. Run

在本地运行开发服务器。然后,在你的网页浏览器中访问 http://localhost:7676

🌐 Run the development server locally. Then, access http://localhost:7676 in your Web browser.

sh
npm run start
sh
yarn start
sh
pnpm run start
sh
bun run start

4. 部署

🌐 4. Deploy

要将你的应用构建并部署到你的 Fastly 账户,请输入以下命令。第一次部署应用时,系统会提示你在账户中创建一个新服务。

🌐 To build and deploy your application to your Fastly account, type the following command. The first time you deploy the application, you will be prompted to create a new service in your account.

如果你还没有账户,你必须创建你的 Fastly 账户

🌐 If you don't have an account yet, you must create your Fastly account.

sh
npm run deploy
sh
yarn deploy
sh
pnpm run deploy
sh
bun run deploy

绑定

🌐 Bindings

在 Fastly Compute 中,你可以绑定 Fastly 平台资源,例如 KV 存储、配置存储、秘密存储、后端、访问控制列表、命名日志流和环境变量。你可以通过 c.env 访问它们,并将拥有它们各自的 SDK 类型。

🌐 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.

要使用这些绑定,请从 @fastly/hono-fastly-compute 导入 buildFire 而不是 fire。定义你的 绑定 并将它们传递给 buildFire() 以获得 fire。然后使用 fire.Bindings 在构造 Hono 时定义你的 Env 类型。

🌐 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.

ts
// 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)

Hono 中文网 - 粤ICP备13048890号