feat: standardize transport contracts across Gin and nethttp
This commit is contained in:
@@ -1,71 +1,47 @@
|
||||
// Package nethttp provides a small standard-library router for GoOmni.
|
||||
// It is intentionally independent of Gin and serves as the reference for a
|
||||
// future project-owned network layer.
|
||||
// Package nethttp provides a standard-library router for GoOmni, independent
|
||||
// of Gin. Register routes before serving requests.
|
||||
package nethttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/transport/internal/routing"
|
||||
)
|
||||
|
||||
// Router implements httpx.Router with net/http only. Register routes before
|
||||
// serving requests.
|
||||
type Router struct {
|
||||
mu sync.RWMutex
|
||||
routes []route
|
||||
}
|
||||
type Router struct{ routes []route }
|
||||
|
||||
type route struct {
|
||||
method string
|
||||
pattern string
|
||||
segments []segment
|
||||
segments []routing.Segment
|
||||
handler http.Handler
|
||||
score int
|
||||
}
|
||||
|
||||
type segment struct {
|
||||
literal string
|
||||
name string
|
||||
wildcard bool
|
||||
}
|
||||
|
||||
// New creates an empty standard-library router.
|
||||
func New() *Router { return &Router{} }
|
||||
|
||||
// ServeHTTP dispatches a request and injects route params and the matched
|
||||
// pattern into its context for the transport-neutral handler chain.
|
||||
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
r.mu.RLock()
|
||||
routes := append([]route(nil), r.routes...)
|
||||
r.mu.RUnlock()
|
||||
|
||||
var selected *route
|
||||
var selectedParams map[string]string
|
||||
pathMatched := false
|
||||
allow := make(map[string]struct{})
|
||||
for index := range routes {
|
||||
candidate := &routes[index]
|
||||
params, matched := match(candidate.segments, request.URL.Path)
|
||||
allow := make(map[string]bool)
|
||||
for index := range r.routes {
|
||||
candidate := &r.routes[index]
|
||||
params, matched := routing.Match(candidate.segments, request.URL.Path)
|
||||
if !matched {
|
||||
continue
|
||||
}
|
||||
pathMatched = true
|
||||
if candidate.method != "*" && candidate.method != request.Method {
|
||||
allow[candidate.method] = struct{}{}
|
||||
if candidate.method != request.Method {
|
||||
allow[candidate.method] = true
|
||||
continue
|
||||
}
|
||||
if selected == nil || candidate.score > selected.score {
|
||||
selected = candidate
|
||||
selectedParams = params
|
||||
if selected == nil || moreSpecific(candidate.segments, selected.segments) {
|
||||
selected, selectedParams = candidate, params
|
||||
}
|
||||
}
|
||||
|
||||
if selected == nil {
|
||||
if pathMatched {
|
||||
if len(allow) > 0 {
|
||||
methods := make([]string, 0, len(allow))
|
||||
for method := range allow {
|
||||
methods = append(methods, method)
|
||||
@@ -73,37 +49,34 @@ 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)
|
||||
return
|
||||
} else {
|
||||
http.NotFound(writer, request)
|
||||
}
|
||||
http.NotFound(writer, request)
|
||||
return
|
||||
}
|
||||
|
||||
requestContext := httpx.WithRouteParams(request.Context(), selectedParams)
|
||||
requestContext = httpx.WithRoutePattern(requestContext, selected.pattern)
|
||||
selected.handler.ServeHTTP(writer, request.WithContext(requestContext))
|
||||
ctx := httpx.WithRouteParams(request.Context(), selectedParams)
|
||||
ctx = httpx.WithRoutePattern(ctx, selected.pattern)
|
||||
selected.handler.ServeHTTP(writer, request.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (r *Router) Handle(method, path string, handler http.Handler) {
|
||||
if handler == nil {
|
||||
panic("go-omni/nethttp: nil route handler")
|
||||
segments := routing.Parse(method, path, handler)
|
||||
for _, existing := range r.routes {
|
||||
if existing.method == method {
|
||||
routing.CheckConflict(existing.segments, segments)
|
||||
}
|
||||
}
|
||||
segments := parsePattern(path)
|
||||
r.mu.Lock()
|
||||
r.routes = append(r.routes, route{
|
||||
method: method,
|
||||
pattern: cleanPattern(path),
|
||||
segments: segments,
|
||||
handler: handler,
|
||||
score: routeScore(method, segments),
|
||||
})
|
||||
r.mu.Unlock()
|
||||
r.routes = append(r.routes, route{method, path, segments, handler})
|
||||
}
|
||||
|
||||
func (r *Router) Any(path string, handler http.Handler) { r.Handle("*", path, handler) }
|
||||
func (r *Router) Any(path string, handler http.Handler) {
|
||||
for _, method := range routing.Methods {
|
||||
r.Handle(method, path, handler)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) Group(path string) httpx.RouterGroup {
|
||||
return &group{router: r, prefix: cleanPattern(path)}
|
||||
return &group{router: r, prefix: routing.Join("", path)}
|
||||
}
|
||||
|
||||
type group struct {
|
||||
@@ -112,107 +85,24 @@ type group struct {
|
||||
}
|
||||
|
||||
func (g *group) Handle(method, path string, handler http.Handler) {
|
||||
g.router.Handle(method, joinPattern(g.prefix, path), handler)
|
||||
g.router.Handle(method, routing.Join(g.prefix, path), handler)
|
||||
}
|
||||
|
||||
func (g *group) Any(path string, handler http.Handler) { g.Handle("*", path, handler) }
|
||||
func (g *group) Any(path string, handler http.Handler) {
|
||||
g.router.Any(routing.Join(g.prefix, path), handler)
|
||||
}
|
||||
|
||||
func (g *group) Group(path string) httpx.RouterGroup {
|
||||
return &group{router: g.router, prefix: joinPattern(g.prefix, path)}
|
||||
return &group{router: g.router, prefix: routing.Join(g.prefix, path)}
|
||||
}
|
||||
|
||||
func cleanPattern(pattern string) string {
|
||||
if pattern == "" {
|
||||
return "/"
|
||||
// Compare from the left: an earlier static segment wins even if another
|
||||
// matching route has more static segments later in its path.
|
||||
func moreSpecific(a, b []routing.Segment) bool {
|
||||
for i := 0; i < len(a) && i < len(b); i++ {
|
||||
if (a[i].Name == "") != (b[i].Name == "") {
|
||||
return a[i].Name == ""
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(pattern, "/") {
|
||||
pattern = "/" + pattern
|
||||
}
|
||||
pattern = strings.TrimRight(pattern, "/")
|
||||
if pattern == "" {
|
||||
return "/"
|
||||
}
|
||||
return pattern
|
||||
}
|
||||
|
||||
func joinPattern(prefix, path string) string {
|
||||
if prefix == "" || prefix == "/" {
|
||||
return cleanPattern(path)
|
||||
}
|
||||
if path == "" || path == "/" {
|
||||
return cleanPattern(prefix)
|
||||
}
|
||||
return cleanPattern(strings.TrimRight(prefix, "/") + "/" + strings.Trim(path, "/"))
|
||||
}
|
||||
|
||||
func parsePattern(pattern string) []segment {
|
||||
pattern = cleanPattern(pattern)
|
||||
if pattern == "/" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(strings.Trim(pattern, "/"), "/")
|
||||
segments := make([]segment, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
switch {
|
||||
case strings.HasPrefix(part, ":") && len(part) > 1:
|
||||
segments = append(segments, segment{name: part[1:]})
|
||||
case strings.HasPrefix(part, "*") && len(part) > 1:
|
||||
segments = append(segments, segment{name: part[1:], wildcard: true})
|
||||
default:
|
||||
segments = append(segments, segment{literal: part})
|
||||
}
|
||||
}
|
||||
return segments
|
||||
}
|
||||
|
||||
func match(segments []segment, path string) (map[string]string, bool) {
|
||||
parts := splitPath(path)
|
||||
params := make(map[string]string)
|
||||
partIndex := 0
|
||||
for segmentIndex, current := range segments {
|
||||
if current.wildcard {
|
||||
params[current.name] = strings.Join(parts[partIndex:], "/")
|
||||
return params, true
|
||||
}
|
||||
if partIndex >= len(parts) {
|
||||
return nil, false
|
||||
}
|
||||
if current.name != "" {
|
||||
params[current.name] = parts[partIndex]
|
||||
} else if current.literal != parts[partIndex] {
|
||||
return nil, false
|
||||
}
|
||||
partIndex++
|
||||
if segmentIndex == len(segments)-1 && partIndex != len(parts) {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
if partIndex != len(parts) {
|
||||
return nil, false
|
||||
}
|
||||
return params, true
|
||||
}
|
||||
|
||||
func splitPath(path string) []string {
|
||||
path = strings.Trim(path, "/")
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(path, "/")
|
||||
}
|
||||
|
||||
func routeScore(method string, segments []segment) int {
|
||||
score := len(segments)
|
||||
if method != "*" {
|
||||
score += 1000
|
||||
}
|
||||
for _, current := range segments {
|
||||
if current.literal != "" {
|
||||
score += 10
|
||||
}
|
||||
if current.wildcard {
|
||||
score -= 5
|
||||
}
|
||||
}
|
||||
return score
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user