Files
godoxy/internal/route/routes/routes.go
yusing 35a3e3fef6 refactor(api): restructured API for type safety, maintainability and docs generation
- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
2025-08-16 13:04:05 +08:00

57 lines
1002 B
Go

package routes
import (
"github.com/yusing/go-proxy/internal/types"
"github.com/yusing/go-proxy/internal/utils/pool"
)
var (
HTTP = pool.New[types.HTTPRoute]("http_routes")
Stream = pool.New[types.StreamRoute]("stream_routes")
// All is a pool of all routes, including HTTP, Stream routes and also excluded routes.
All = pool.New[types.Route]("all_routes")
)
func init() {
All.DisableLog()
}
func Iter(yield func(r types.Route) bool) {
for _, r := range All.Iter {
if !yield(r) {
break
}
}
}
func IterKV(yield func(alias string, r types.Route) bool) {
for k, r := range All.Iter {
if !yield(k, r) {
break
}
}
}
func NumRoutes() int {
return All.Size()
}
func Clear() {
HTTP.Clear()
Stream.Clear()
All.Clear()
}
func GetHTTPRouteOrExact(alias, host string) (types.HTTPRoute, bool) {
r, ok := HTTP.Get(alias)
if ok {
return r, true
}
// try find with exact match
return HTTP.Get(host)
}
func Get(alias string) (types.Route, bool) {
return All.Get(alias)
}