mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Replaces beego's ControllerRegister, filter chain and context with the native web package end to end. The controllers, filters and routes are retyped from beego.Context/beego.Controller to their web equivalents (web.Context mirrors beego's API, so the swap is mechanical), and the 307 explicit routes register on a shared routers.App (web.Router). The dead NSInclude namespace is dropped: without a generated commentsRouter it registered nothing. - controllers/base.go embeds web.Controller; the 4 streaming/util controllers, the 17 filters, util/cookie.go and object/record.go take *web.Context. - routers.InstallFilters inserts the 14 BeforeRouter + 2 AfterExec filters on App in the same order the standalone used; bootstrap and the route tests share it. - The session provider binds through App.UseSessions (its Store matches web.Store); Bootstrap publishes App as the request handler instead of beego.BeeApp.Handlers. - web gains the Controller compatibility surface the handlers use: EnableRender, GetFile, Context.SetCookie/Output.Cookie; RunMode defaults to prod (compact ServeJSON), matching the runtime default. The /swagger static (beego SetStaticPath) is dropped — the edge serves statics. Verified on SQLite (UseMemoryDB): /v1/cloud/* rewrites and dispatches, the /v1/iam/* alias is selective (in-set dispatches, out-of-set 404s), an unauthenticated gated /v1/cloud/get-providers is denied 401/403 before dispatch, and the real filter chain short-circuits on auth-reject in order. go build ./... and the web/routers/controllers/route tests are green; the beego module stays until the session and lifecycle cuts.
56 lines
1.9 KiB
Go
56 lines
1.9 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 "net/http"
|
|
|
|
// Context carries one request's reader and writer: the Input for request
|
|
// data, the Output for the response, and the raw Request plus the wrapped
|
|
// ResponseWriter.
|
|
type Context struct {
|
|
Input *Input
|
|
Output *Output
|
|
Request *http.Request
|
|
ResponseWriter *Response
|
|
}
|
|
|
|
// NewContext returns a Context with an initialized Input and Output.
|
|
func NewContext() *Context {
|
|
return &Context{Input: NewInput(), Output: &Output{}}
|
|
}
|
|
|
|
// Reset rebinds the Context to a request/response pair and clears the
|
|
// per-request state on the Input, Output and ResponseWriter.
|
|
func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) {
|
|
ctx.Request = r
|
|
if ctx.ResponseWriter == nil {
|
|
ctx.ResponseWriter = &Response{}
|
|
}
|
|
ctx.ResponseWriter.reset(rw)
|
|
ctx.Input.Reset(ctx)
|
|
ctx.Output.Reset(ctx)
|
|
}
|
|
|
|
// Redirect writes an HTTP redirect to localurl with the given status.
|
|
func (ctx *Context) Redirect(status int, localurl string) {
|
|
http.Redirect(ctx.ResponseWriter, ctx.Request, localurl, status)
|
|
}
|
|
|
|
// SetCookie adds a Set-Cookie response header. See Output.Cookie for the
|
|
// positional option contract.
|
|
func (ctx *Context) SetCookie(name, value string, others ...interface{}) {
|
|
ctx.Output.Cookie(name, value, others...)
|
|
}
|