Azure Functions

Azure Functions هي منصة بدون خادم (serverless) من Microsoft Azure. يمكنك تشغيل كودك استجابةً للأحداث، وهي تدير تلقائيًا موارد الحوسبة الأساسية نيابة عنك.

لم يُصمَّم Hono لـ Azure Functions في البداية، ولكن مع مُهايئ Azure Functions، يمكنه العمل عليه أيضًا.

يعمل مع Azure Functions V4 التي تعمل على Node.js 18 أو أحدث.

1. تثبيت CLI

لإنشاء Azure Function، يجب أولًا تثبيت Azure Functions Core Tools.

على macOS

brew tap azure/functions
brew install azure-functions-core-tools@4

اتبع هذا الرابط لأنظمة التشغيل الأخرى:

2. الإعداد

أنشئ مشروع TypeScript Node.js V4 في المجلد الحالي.

func init --typescript

غيّر بادئة المسار الافتراضية للمضيف (host). أضف هذه الخاصية إلى كائن json الجذري الخاص بـ host.json:

"extensions": {
    "http": {
        "routePrefix": ""
    }
}

بادئة مسار Azure Functions الافتراضية هي /api. إذا لم تغيّره كما هو موضح أعلاه، فتأكد من بدء جميع مسارات Hono الخاصة بك بـ /api

أنت الآن مستعد لتثبيت Hono ومحوّل (adapter) Azure Functions باستخدام:

npm i @marplex/hono-azurefunc-adapter hono
yarn add @marplex/hono-azurefunc-adapter hono
pnpm add @marplex/hono-azurefunc-adapter hono
bun add @marplex/hono-azurefunc-adapter hono

3. مرحبًا بالعالم

إنشاء src/app.ts:

// src/app.ts
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Azure Functions!'))

export default app

إنشاء src/functions/httpTrigger.ts:

// src/functions/httpTrigger.ts
import { app } from '@azure/functions'
import { azureHonoHandler } from '@marplex/hono-azurefunc-adapter'
import honoApp from '../app'

app.http('httpTrigger', {
  methods: [
    //Add all your supported HTTP methods here
    'GET',
    'POST',
    'DELETE',
    'PUT',
  ],
  authLevel: 'anonymous',
  route: '{*proxy}',
  handler: azureHonoHandler(honoApp.fetch),
})

4. تشغيل

شغّل خادم التطوير محليًا. ثم، ادخل إلى http://localhost:7071 في متصفح الويب الخاص بك.

npm run start
yarn start
pnpm start
bun run start

5. النشر

قبل أن تتمكن من النشر إلى Azure، تحتاج إلى إنشاء بعض الموارد في بنيتك التحتية السحابية. يرجى زيارة وثائق Microsoft حول أنشئ موارد Azure الداعمة لدالتك

ابنِ المشروع للنشر:

npm run build
yarn build
pnpm build
bun run build

انشر مشروعك إلى تطبيق الدوال (function app) في Azure Cloud. استبدل <YourFunctionAppName> باسم تطبيقك.

func azure functionapp publish <YourFunctionAppName>