feat: release configurable API response contracts

Define reusable response bodies in .api with typed code mappings, per-route overrides and matching OpenAPI schemas. Add application and callback encoders, early response policy selection, and isolated remote-consumer release verification.

Release v0.8.0-preview after full race, vet, build and local MySQL workflow acceptance.
This commit is contained in:
2026-09-23 20:42:38 +08:00
parent bef7ea2641
commit 7670b222ac
47 changed files with 2689 additions and 120 deletions

View File

@@ -11,7 +11,10 @@ import (
"git.yuntuo.hk/k2safe/go-omni/transport/internal/routing"
)
type Router struct{ routes []route }
type Router struct {
routes []route
notFound, methodNotAllowed http.Handler
}
type route struct {
method string
@@ -22,6 +25,12 @@ type route struct {
func New() *Router { return &Router{} }
// SetFallbacks must be called before serving requests. Nil preserves the plain
// net/http fallback, useful when using this router without an Omni application.
func (r *Router) SetFallbacks(notFound, methodNotAllowed http.Handler) {
r.notFound, r.methodNotAllowed = notFound, methodNotAllowed
}
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var selected *route
var selectedParams map[string]string
@@ -48,9 +57,17 @@ func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
sort.Strings(methods)
writer.Header().Set("Allow", strings.Join(methods, ", "))
http.Error(writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
if r.methodNotAllowed != nil {
r.methodNotAllowed.ServeHTTP(writer, request)
} else {
http.Error(writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
} else {
http.NotFound(writer, request)
if r.notFound != nil {
r.notFound.ServeHTTP(writer, request)
} else {
http.NotFound(writer, request)
}
}
return
}