Appearance
Fastly 计算
¥Fastly Compute
Fastly 计算 是一个高级的边缘计算系统,它允许你使用自己喜欢的语言在 Fastly 的全球边缘网络上运行代码。Hono 也适用于 Fastly Compute。
¥Fastly Compute is an advanced edge computing system that runs your code, in your favorite language, on Fastly's global edge network. Hono also works on Fastly Compute.
你可以使用 Fastly CLI 在本地开发应用,并通过几个命令将其发布。Fastly 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. 设置
¥ 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-appsh
yarn create hono my-appsh
pnpm create hono my-appsh
bun create hono@latest my-appsh
deno init --npm hono my-app移至 my-app 并安装依赖。
¥Move to my-app and install the dependencies.
sh
cd my-app
npm ish
cd my-app
yarnsh
cd my-app
pnpm ish
cd my-app
bun i2. 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)当你在应用顶层使用 `@fastly/hono-fastly-compute'` 中的 `fire`(或 `buildFire()`)时,建议使用 `'hono'` 中的 `Hono` 而不是 `'hono/quick'`,因为 `fire` 会导致其路由在应用初始化阶段构建内部数据。
¥[!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
在本地运行开发服务器。然后,在你的 Web 浏览器中访问 http://localhost:7676。
¥Run the development server locally. Then, access http://localhost:7676 in your Web browser.
sh
npm run startsh
yarn startsh
pnpm run startsh
bun run start4. 部署
¥ 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 deploysh
yarn deploysh
pnpm run deploysh
bun run deploy绑定
¥Bindings
在 Fastly Compute 中,你可以绑定 Fastly 平台资源,例如键值存储、配置存储、密钥存储、后端、访问控制列表、命名日志流和环境变量。你可以通过 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。定义你的 bindings 并将其传递给 buildFire() 以获取 fire。然后在构建 Hono 时,使用 fire.Bindings 定义 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)