Files
zeekay af238b3a50 feat(web): native beego-web slice for the ai runtime (unwired foundation)
Adds github.com/hanzoai/ai/web: the request/response and routing layer the ai
controllers need, reimplemented over net/http so the framework swap in the
next cut is a mechanical beego.*->web.* rename.

- Context/Input/Output/Response: request readers, response writers, and the
  ResponseWriter wrapper. Input.CopyBody keeps the read-once/cache/rewind and
  gzip semantics the RequestBody readers rely on; Output.Body keeps
  Content-Length and deferred-status behavior; Response passes Flush and
  Hijack through for streaming.
- Controller: the base controllers embed — Ctx/Data, Input/GetString,
  ServeJSON, the session accessors (fail-secure on a nil store) and StopRun.
- Router: :param matching, reflection dispatch of a fresh controller per
  request, and the BeforeRouter/AfterExec filter chain with returnOnOutput
  short-circuit.

Tests differential-check the drift surface (RequestBody cache/rewind/gzip,
Output.Body/SetStatus/Header, the Response writer) against beego's own types.
The package is not yet wired into the routers.
2026-07-17 18:34:26 -07:00

81 lines
2.2 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"bufio"
"errors"
"net"
"net/http"
)
// Response wraps http.ResponseWriter and records whether the reply has begun
// (Started) and the status written (Status). It passes Flush, Hijack,
// CloseNotify and Pusher through to the underlying writer so streaming and
// server-sent-event handlers keep working.
type Response struct {
http.ResponseWriter
Started bool
Status int
}
func (r *Response) reset(rw http.ResponseWriter) {
r.ResponseWriter = rw
r.Status = 0
r.Started = false
}
// Write sends body bytes and marks the reply as started.
func (r *Response) Write(p []byte) (int, error) {
r.Started = true
return r.ResponseWriter.Write(p)
}
// WriteHeader sends the status line once; a second call is ignored so the
// handler chain cannot emit two WriteHeader calls.
func (r *Response) WriteHeader(code int) {
if r.Status > 0 {
return
}
r.Status = code
r.Started = true
r.ResponseWriter.WriteHeader(code)
}
// Flush forwards to the underlying writer when it is an http.Flusher, so
// streaming responses can push buffered bytes.
func (r *Response) Flush() {
if f, ok := r.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// Hijack forwards to the underlying writer when it is an http.Hijacker.
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("web: response writer does not support hijacking")
}
return hj.Hijack()
}
// Pusher forwards to the underlying writer when it is an http.Pusher.
func (r *Response) Pusher() http.Pusher {
if p, ok := r.ResponseWriter.(http.Pusher); ok {
return p
}
return nil
}