# Hono Service (Bun Runtime) - AI Generator Guidelines

These guidelines are for generating Hono microservices that run with **Bun runtime**.

---

## Entry Point Template (src/index.ts)

**Basic REST API:**
```ts
import '@crossdelta/telemetry'

import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

// Your routes here
app.get('/', (c) => c.text('Hello Hono!'))

Bun.serve({ port, fetch: app.fetch })
```

**Event Consumer (NATS) - Multiple Streams:**
```ts
import '@crossdelta/telemetry'

import { consumeJetStreamStreams, ensureJetStreamStreams } from '@crossdelta/cloudevents'
import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

Bun.serve({ port, fetch: app.fetch })

await ensureJetStreamStreams({
  streams: [
    { stream: 'ORDERS', subjects: ['orders.*'] },
    { stream: 'CUSTOMERS', subjects: ['customers.*'] },
  ]
})

consumeJetStreamStreams({
  streams: ['ORDERS', 'CUSTOMERS'],
  consumer: 'my-service',
  discover: './src/events/**/*.event.ts',
})
```

**Event Publisher (REST + CloudEvents):**
```ts
import '@crossdelta/telemetry'

import { publish } from '@crossdelta/cloudevents'
import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

app.post('/orders', async (c) => {
  const data = await c.req.json()
  await publish('orders.created', data, { source: 'my-service' })
  return c.json({ success: true })
})

Bun.serve({ port, fetch: app.fetch })
```

---

## Key Points

- ✅ Use `Bun.serve({ port, fetch: app.fetch })` for server
- ✅ Import telemetry FIRST (before any other imports)
- ✅ Use the port env var name provided in the prompt (e.g., `PUSH_NOTIFICATIONS_PORT`)
- ✅ Always include `/health` endpoint
- ✅ Keep routes simple and delegate logic to use-cases
- ✅ Use `ensureJetStreamStreams` + `consumeJetStreamStreams` for multiple event types

**DO NOT:**
- ❌ `export default Bun.serve(...)` - Bun.serve returns Server, not for export
- ❌ `export default app` - Not needed for Bun runtime
- ❌ Multiple `ensureJetStreamStream` calls - use batch version instead

---

## Dev Command

```json
{
  "scripts": {
    "dev": "bun run --hot src/index.ts"
  }
}
```
