Files
ai/controllers/request.go
zeekay 43c4b66ef5 the paginator takes a page, and the stream owns the connection
Three structural pieces of the move off web.

SSE. The chat-completions stream and the single-chunk reply now run inside
SendStreamWriter: zip hands the handler a writer and flushes each frame as it is
written, so a chunk reaches the client when it is produced. The billing capture
runs inside that same closure, so the usage it settles on is the usage of the
bytes the client actually received.

Pagination. NewPaginator wanted an *http.Request for exactly one thing: reading
"p". It takes the page as a number now, and the parameter name is read by the
caller — 22 call sites, and util no longer imports net/http for this. pageAsked
is a pure function so the name and the not-a-number cases are assertable without
a live request; its table moved with it.

Per-request values. Input.SetParam/GetData are Locals, which is where a request
carries something for a later filter to read.

909 compile errors down to 76. Left: 14 bare Request values, 28 ResponseWriter
in the remaining streamers, the session, and routers/ + app.go + the tests.
2026-08-16 22:00:37 -07:00

85 lines
2.4 KiB
Go

package controllers
import (
"mime/multipart"
"net/url"
"strconv"
)
// How a handler reads what it was sent.
//
// The embedded zip context already answers most of it — Body, Param, Query,
// Header. These three are the readings that do not have a one-call equivalent
// there: the merged form+query set that ~320 handlers read by name, and the two
// multipart accessors.
// Input is every value the request carries by name, query and form merged, with
// the form winning — the same order net/http's ParseForm produces. Handlers read
// an identifier as Input().Get("id") whether it arrived as ?id=, as a form field,
// or as a route parameter the router bound.
func (c *ApiController) Input() url.Values {
f := c.Fiber()
out := url.Values{}
for k, v := range f.Queries() {
out.Set(k, v)
}
if form, err := f.MultipartForm(); err == nil && form != nil {
for k, vs := range form.Value {
if len(vs) > 0 {
out.Set(k, vs[0])
}
}
}
// A route parameter is readable by name too, so /v1/ai/chats/:owner/:name
// hands its handler an id without the handler learning a second way to ask.
for _, p := range c.Fiber().Route().Params {
if v := c.Param(p); v != "" {
out.Set(p, v)
}
}
return out
}
// GetString is Input().Get with a fallback, for the reads that have a sensible
// default rather than an error.
func (c *ApiController) GetString(key string, def ...string) string {
if v := c.Input().Get(key); v != "" {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetFile returns one uploaded part by name.
func (c *ApiController) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
h, err := c.Fiber().FormFile(key)
if err != nil {
return nil, nil, err
}
f, err := h.Open()
if err != nil {
return nil, nil, err
}
return f, h, nil
}
// PageAsked is the page the caller asked for, from the "p" query parameter.
// Zero when absent or unreadable, which the paginator clamps to the first page.
func (c *ApiController) PageAsked() int {
return pageAsked(string(c.Fiber().Request().URI().QueryString()))
}
// pageAsked reads "p" out of a raw query string. It is separate from the method
// so the parameter NAME and the not-a-number cases can be asserted without a
// live request.
func pageAsked(rawQuery string) int {
v, err := url.ParseQuery(rawQuery)
if err != nil {
return 0
}
n, _ := strconv.Atoi(v.Get("p"))
return n
}