mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-20 17:44:26 +00:00
Massive refactor and introduction of restheadspec
This commit is contained in:
193
pkg/common/adapters/router/bunrouter.go
Normal file
193
pkg/common/adapters/router/bunrouter.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Warky-Devs/ResolveSpec/pkg/common"
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
|
||||
// BunRouterAdapter adapts uptrace/bunrouter to work with our Router interface
|
||||
type BunRouterAdapter struct {
|
||||
router *bunrouter.Router
|
||||
}
|
||||
|
||||
// NewBunRouterAdapter creates a new bunrouter adapter
|
||||
func NewBunRouterAdapter(router *bunrouter.Router) *BunRouterAdapter {
|
||||
return &BunRouterAdapter{router: router}
|
||||
}
|
||||
|
||||
// NewBunRouterAdapterDefault creates a new bunrouter adapter with default router
|
||||
func NewBunRouterAdapterDefault() *BunRouterAdapter {
|
||||
return &BunRouterAdapter{router: bunrouter.New()}
|
||||
}
|
||||
|
||||
func (b *BunRouterAdapter) HandleFunc(pattern string, handler common.HTTPHandlerFunc) common.RouteRegistration {
|
||||
route := &BunRouterRegistration{
|
||||
router: b.router,
|
||||
pattern: pattern,
|
||||
handler: handler,
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
||||
func (b *BunRouterAdapter) ServeHTTP(w common.ResponseWriter, r common.Request) {
|
||||
// This method would be used when we need to serve through our interface
|
||||
// For now, we'll work directly with the underlying router
|
||||
panic("ServeHTTP not implemented - use GetBunRouter() for direct access")
|
||||
}
|
||||
|
||||
// GetBunRouter returns the underlying bunrouter for direct access
|
||||
func (b *BunRouterAdapter) GetBunRouter() *bunrouter.Router {
|
||||
return b.router
|
||||
}
|
||||
|
||||
// BunRouterRegistration implements RouteRegistration for bunrouter
|
||||
type BunRouterRegistration struct {
|
||||
router *bunrouter.Router
|
||||
pattern string
|
||||
handler common.HTTPHandlerFunc
|
||||
}
|
||||
|
||||
func (b *BunRouterRegistration) Methods(methods ...string) common.RouteRegistration {
|
||||
// bunrouter handles methods differently - we'll register for each method
|
||||
for _, method := range methods {
|
||||
b.router.Handle(method, b.pattern, func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
// Convert bunrouter.Request to our BunRouterRequest
|
||||
reqAdapter := &BunRouterRequest{req: req}
|
||||
respAdapter := &HTTPResponseWriter{resp: w}
|
||||
b.handler(respAdapter, reqAdapter)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *BunRouterRegistration) PathPrefix(prefix string) common.RouteRegistration {
|
||||
// bunrouter doesn't have PathPrefix like mux, but we can modify the pattern
|
||||
newPattern := prefix + b.pattern
|
||||
b.pattern = newPattern
|
||||
return b
|
||||
}
|
||||
|
||||
// BunRouterRequest adapts bunrouter.Request to our Request interface
|
||||
type BunRouterRequest struct {
|
||||
req bunrouter.Request
|
||||
body []byte
|
||||
}
|
||||
|
||||
// NewBunRouterRequest creates a new BunRouterRequest adapter
|
||||
func NewBunRouterRequest(req bunrouter.Request) *BunRouterRequest {
|
||||
return &BunRouterRequest{req: req}
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) Method() string {
|
||||
return b.req.Method
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) URL() string {
|
||||
return b.req.URL.String()
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) Header(key string) string {
|
||||
return b.req.Header.Get(key)
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) Body() ([]byte, error) {
|
||||
if b.body != nil {
|
||||
return b.body, nil
|
||||
}
|
||||
|
||||
if b.req.Body == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Create HTTPRequest adapter and use its Body() method
|
||||
httpAdapter := NewHTTPRequest(b.req.Request)
|
||||
body, err := httpAdapter.Body()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.body = body
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) PathParam(key string) string {
|
||||
return b.req.Param(key)
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) QueryParam(key string) string {
|
||||
return b.req.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func (b *BunRouterRequest) AllHeaders() map[string]string {
|
||||
headers := make(map[string]string)
|
||||
for key, values := range b.req.Header {
|
||||
if len(values) > 0 {
|
||||
headers[key] = values[0]
|
||||
}
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
// StandardBunRouterAdapter creates routes compatible with standard bunrouter handlers
|
||||
type StandardBunRouterAdapter struct {
|
||||
*BunRouterAdapter
|
||||
}
|
||||
|
||||
func NewStandardBunRouterAdapter() *StandardBunRouterAdapter {
|
||||
return &StandardBunRouterAdapter{
|
||||
BunRouterAdapter: NewBunRouterAdapterDefault(),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoute registers a route that works with the existing Handler
|
||||
func (s *StandardBunRouterAdapter) RegisterRoute(method, pattern string, handler func(http.ResponseWriter, *http.Request, map[string]string)) {
|
||||
s.router.Handle(method, pattern, func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
// Extract path parameters
|
||||
params := make(map[string]string)
|
||||
|
||||
// bunrouter doesn't provide a direct way to get all params
|
||||
// You would typically access them individually with req.Param("name")
|
||||
// For this example, we'll create the map based on the request context
|
||||
|
||||
handler(w, req.Request, params)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// RegisterRouteWithParams registers a route with explicit parameter extraction
|
||||
func (s *StandardBunRouterAdapter) RegisterRouteWithParams(method, pattern string, paramNames []string, handler func(http.ResponseWriter, *http.Request, map[string]string)) {
|
||||
s.router.Handle(method, pattern, func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
// Extract specified path parameters
|
||||
params := make(map[string]string)
|
||||
for _, paramName := range paramNames {
|
||||
params[paramName] = req.Param(paramName)
|
||||
}
|
||||
|
||||
handler(w, req.Request, params)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// BunRouterConfig holds bunrouter-specific configuration
|
||||
type BunRouterConfig struct {
|
||||
UseStrictSlash bool
|
||||
RedirectTrailingSlash bool
|
||||
HandleMethodNotAllowed bool
|
||||
HandleOPTIONS bool
|
||||
GlobalOPTIONS http.Handler
|
||||
GlobalMethodNotAllowed http.Handler
|
||||
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
|
||||
}
|
||||
|
||||
// DefaultBunRouterConfig returns default bunrouter configuration
|
||||
func DefaultBunRouterConfig() *BunRouterConfig {
|
||||
return &BunRouterConfig{
|
||||
UseStrictSlash: false,
|
||||
RedirectTrailingSlash: true,
|
||||
HandleMethodNotAllowed: true,
|
||||
HandleOPTIONS: true,
|
||||
}
|
||||
}
|
||||
|
||||
208
pkg/common/adapters/router/mux.go
Normal file
208
pkg/common/adapters/router/mux.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Warky-Devs/ResolveSpec/pkg/common"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// MuxAdapter adapts Gorilla Mux to work with our Router interface
|
||||
type MuxAdapter struct {
|
||||
router *mux.Router
|
||||
}
|
||||
|
||||
// NewMuxAdapter creates a new Mux adapter
|
||||
func NewMuxAdapter(router *mux.Router) *MuxAdapter {
|
||||
return &MuxAdapter{router: router}
|
||||
}
|
||||
|
||||
func (m *MuxAdapter) HandleFunc(pattern string, handler common.HTTPHandlerFunc) common.RouteRegistration {
|
||||
route := &MuxRouteRegistration{
|
||||
router: m.router,
|
||||
pattern: pattern,
|
||||
handler: handler,
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
||||
func (m *MuxAdapter) ServeHTTP(w common.ResponseWriter, r common.Request) {
|
||||
// This method would be used when we need to serve through our interface
|
||||
// For now, we'll work directly with the underlying router
|
||||
panic("ServeHTTP not implemented - use GetMuxRouter() for direct access")
|
||||
}
|
||||
|
||||
// MuxRouteRegistration implements RouteRegistration for Mux
|
||||
type MuxRouteRegistration struct {
|
||||
router *mux.Router
|
||||
pattern string
|
||||
handler common.HTTPHandlerFunc
|
||||
route *mux.Route
|
||||
}
|
||||
|
||||
func (m *MuxRouteRegistration) Methods(methods ...string) common.RouteRegistration {
|
||||
if m.route == nil {
|
||||
m.route = m.router.HandleFunc(m.pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
reqAdapter := &HTTPRequest{req: r, vars: mux.Vars(r)}
|
||||
respAdapter := &HTTPResponseWriter{resp: w}
|
||||
m.handler(respAdapter, reqAdapter)
|
||||
})
|
||||
}
|
||||
m.route.Methods(methods...)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *MuxRouteRegistration) PathPrefix(prefix string) common.RouteRegistration {
|
||||
if m.route == nil {
|
||||
m.route = m.router.HandleFunc(m.pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
reqAdapter := &HTTPRequest{req: r, vars: mux.Vars(r)}
|
||||
respAdapter := &HTTPResponseWriter{resp: w}
|
||||
m.handler(respAdapter, reqAdapter)
|
||||
})
|
||||
}
|
||||
m.route.PathPrefix(prefix)
|
||||
return m
|
||||
}
|
||||
|
||||
// HTTPRequest adapts standard http.Request to our Request interface
|
||||
type HTTPRequest struct {
|
||||
req *http.Request
|
||||
vars map[string]string
|
||||
body []byte
|
||||
}
|
||||
|
||||
func NewHTTPRequest(r *http.Request) *HTTPRequest {
|
||||
return &HTTPRequest{
|
||||
req: r,
|
||||
vars: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) Method() string {
|
||||
return h.req.Method
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) URL() string {
|
||||
return h.req.URL.String()
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) Header(key string) string {
|
||||
return h.req.Header.Get(key)
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) Body() ([]byte, error) {
|
||||
if h.body != nil {
|
||||
return h.body, nil
|
||||
}
|
||||
if h.req.Body == nil {
|
||||
return nil, nil
|
||||
}
|
||||
defer h.req.Body.Close()
|
||||
body, err := io.ReadAll(h.req.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.body = body
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) PathParam(key string) string {
|
||||
return h.vars[key]
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) QueryParam(key string) string {
|
||||
return h.req.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func (h *HTTPRequest) AllHeaders() map[string]string {
|
||||
headers := make(map[string]string)
|
||||
for key, values := range h.req.Header {
|
||||
if len(values) > 0 {
|
||||
headers[key] = values[0]
|
||||
}
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
// HTTPResponseWriter adapts our ResponseWriter interface to standard http.ResponseWriter
|
||||
type HTTPResponseWriter struct {
|
||||
resp http.ResponseWriter
|
||||
w common.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func NewHTTPResponseWriter(w http.ResponseWriter) *HTTPResponseWriter {
|
||||
return &HTTPResponseWriter{resp: w}
|
||||
}
|
||||
|
||||
func (h *HTTPResponseWriter) SetHeader(key, value string) {
|
||||
h.resp.Header().Set(key, value)
|
||||
}
|
||||
|
||||
func (h *HTTPResponseWriter) WriteHeader(statusCode int) {
|
||||
h.status = statusCode
|
||||
h.resp.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (h *HTTPResponseWriter) Write(data []byte) (int, error) {
|
||||
return h.resp.Write(data)
|
||||
}
|
||||
|
||||
func (h *HTTPResponseWriter) WriteJSON(data interface{}) error {
|
||||
h.SetHeader("Content-Type", "application/json")
|
||||
return json.NewEncoder(h.resp).Encode(data)
|
||||
}
|
||||
|
||||
// StandardMuxAdapter creates routes compatible with standard http.HandlerFunc
|
||||
type StandardMuxAdapter struct {
|
||||
*MuxAdapter
|
||||
}
|
||||
|
||||
func NewStandardMuxAdapter() *StandardMuxAdapter {
|
||||
return &StandardMuxAdapter{
|
||||
MuxAdapter: NewMuxAdapter(mux.NewRouter()),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoute registers a route that works with the existing Handler
|
||||
func (s *StandardMuxAdapter) RegisterRoute(pattern string, handler func(http.ResponseWriter, *http.Request, map[string]string)) *mux.Route {
|
||||
return s.router.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
handler(w, r, vars)
|
||||
})
|
||||
}
|
||||
|
||||
// GetMuxRouter returns the underlying mux router for direct access
|
||||
func (s *StandardMuxAdapter) GetMuxRouter() *mux.Router {
|
||||
return s.router
|
||||
}
|
||||
|
||||
// PathParamExtractor extracts path parameters from different router types
|
||||
type PathParamExtractor interface {
|
||||
ExtractParams(*http.Request) map[string]string
|
||||
}
|
||||
|
||||
// MuxParamExtractor extracts parameters from Gorilla Mux
|
||||
type MuxParamExtractor struct{}
|
||||
|
||||
func (m MuxParamExtractor) ExtractParams(r *http.Request) map[string]string {
|
||||
return mux.Vars(r)
|
||||
}
|
||||
|
||||
// RouterConfig holds router configuration
|
||||
type RouterConfig struct {
|
||||
PathPrefix string
|
||||
Middleware []func(http.Handler) http.Handler
|
||||
ParamExtractor PathParamExtractor
|
||||
}
|
||||
|
||||
// DefaultRouterConfig returns default router configuration
|
||||
func DefaultRouterConfig() *RouterConfig {
|
||||
return &RouterConfig{
|
||||
PathPrefix: "",
|
||||
Middleware: make([]func(http.Handler) http.Handler, 0),
|
||||
ParamExtractor: MuxParamExtractor{},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user