Files
opantoantro/docs/wiki/OpenAIHandler.md
T
Beyhan Ogur a408821410 first commit
2026-05-11 15:08:50 +03:00

37 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# OpenAIHandler
**Özet:** `/v1/*` route'unu karşılar (`handlers/openai.go:29`), gelen istekleri olduğu gibi `OPENAI_BACKEND` adresine iletir. OpenAI-compatible bir proxy'dir.
**Kütüphaneler:** Fiber v3, GORM, Go `net/http`
**Bağlantılar:** [[Main]], [[ProxyEngine]], [[RequestLog]], [[Config]], [[OpenAI Flow]]
## Davranış
1. Gelen isteği alır, header'ları `net/http.Header` formatına çevirir (`fiberToHTTPHeaders()`)
2. Eğer `OPENAI_KEY` tanımlanmışsa ve client Authorization göndermemişse, otomatik ekler
3. [[ProxyEngine]] ile isteği `OPENAI_BACKEND + originalURL` adresine forward eder
4. Response header'larını kopyalar (`copyResponseHeaders()` — Transfer-Encoding, Content-Length, Connection atlanır)
5. Yanıtı olduğu gibi client'a döndürür
6. [[RequestLog]]'a fire-and-forget goroutine ile kayıt ekler
## Endpoint
```
ANY /v1/chat/completions
ANY /v1/models
Authorization: Bearer <api-key>
```
## Yardımcı Fonksiyonlar
| Fonksiyon | Açıklama |
|---|---|
| `fiberToHTTPHeaders(c fiber.Ctx) http.Header` | Fiber header'larını `net/http` formatına çevirir |
| `copyResponseHeaders(c fiber.Ctx, headers http.Header)` | Upstream response header'larını Fiber response'a kopyalar (hop-by-hop atlanır) |
## Hata Durumu
- Upstream hatasında → `502 Bad Gateway` + hata mesajı
- Loglama hatası → ana isteği etkilemez (fire-and-forget)