mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-02-17 22:06:08 +00:00
Massive refactor and introduction of restheadspec
This commit is contained in:
@@ -3,145 +3,179 @@ package resolvespec
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Warky-Devs/ResolveSpec/pkg/common/adapters/database"
|
||||
"github.com/Warky-Devs/ResolveSpec/pkg/common/adapters/router"
|
||||
"github.com/Warky-Devs/ResolveSpec/pkg/modelregistry"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bunrouter"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NewAPIHandler creates a new APIHandler with GORM (backward compatibility)
|
||||
func NewAPIHandlerWithGORM(db *gorm.DB) *APIHandlerCompat {
|
||||
return NewAPIHandler(db)
|
||||
}
|
||||
|
||||
// NewHandlerWithGORM creates a new Handler with GORM adapter
|
||||
func NewHandlerWithGORM(db *gorm.DB) *Handler {
|
||||
gormAdapter := NewGormAdapter(db)
|
||||
registry := NewModelRegistry()
|
||||
gormAdapter := database.NewGormAdapter(db)
|
||||
registry := modelregistry.NewModelRegistry()
|
||||
return NewHandler(gormAdapter, registry)
|
||||
}
|
||||
|
||||
// NewStandardRouter creates a router with standard HTTP handlers
|
||||
func NewStandardRouter() *StandardMuxAdapter {
|
||||
return NewStandardMuxAdapter()
|
||||
// NewHandlerWithBun creates a new Handler with Bun adapter
|
||||
func NewHandlerWithBun(db *bun.DB) *Handler {
|
||||
bunAdapter := database.NewBunAdapter(db)
|
||||
registry := modelregistry.NewModelRegistry()
|
||||
return NewHandler(bunAdapter, registry)
|
||||
}
|
||||
|
||||
// SetupRoutes sets up routes for the ResolveSpec API with backward compatibility
|
||||
func SetupRoutes(router *mux.Router, handler *APIHandlerCompat) {
|
||||
router.HandleFunc("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request) {
|
||||
// NewStandardMuxRouter creates a router with standard Mux HTTP handlers
|
||||
func NewStandardMuxRouter() *router.StandardMuxAdapter {
|
||||
return router.NewStandardMuxAdapter()
|
||||
}
|
||||
|
||||
// NewStandardBunRouter creates a router with standard BunRouter handlers
|
||||
func NewStandardBunRouter() *router.StandardBunRouterAdapter {
|
||||
return router.NewStandardBunRouterAdapter()
|
||||
}
|
||||
|
||||
// SetupMuxRoutes sets up routes for the ResolveSpec API with Mux
|
||||
func SetupMuxRoutes(muxRouter *mux.Router, handler *Handler) {
|
||||
muxRouter.HandleFunc("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
handler.Handle(w, r, vars)
|
||||
reqAdapter := router.NewHTTPRequest(r)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.Handle(respAdapter, reqAdapter, vars)
|
||||
}).Methods("POST")
|
||||
|
||||
router.HandleFunc("/{schema}/{entity}/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
muxRouter.HandleFunc("/{schema}/{entity}/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
handler.Handle(w, r, vars)
|
||||
reqAdapter := router.NewHTTPRequest(r)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.Handle(respAdapter, reqAdapter, vars)
|
||||
}).Methods("POST")
|
||||
|
||||
router.HandleFunc("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request) {
|
||||
muxRouter.HandleFunc("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
handler.HandleGet(w, r, vars)
|
||||
reqAdapter := router.NewHTTPRequest(r)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.HandleGet(respAdapter, reqAdapter, vars)
|
||||
}).Methods("GET")
|
||||
}
|
||||
|
||||
// Example usage functions for documentation:
|
||||
|
||||
// ExampleWithGORM shows how to use ResolveSpec with GORM (current default)
|
||||
// ExampleWithGORM shows how to use ResolveSpec with GORM
|
||||
func ExampleWithGORM(db *gorm.DB) {
|
||||
// Create handler using GORM (backward compatible)
|
||||
handler := NewAPIHandlerWithGORM(db)
|
||||
|
||||
// Create handler using GORM
|
||||
handler := NewHandlerWithGORM(db)
|
||||
|
||||
// Setup router
|
||||
router := mux.NewRouter()
|
||||
SetupRoutes(router, handler)
|
||||
|
||||
muxRouter := mux.NewRouter()
|
||||
SetupMuxRoutes(muxRouter, handler)
|
||||
|
||||
// Register models
|
||||
// handler.RegisterModel("public", "users", &User{})
|
||||
}
|
||||
|
||||
// ExampleWithNewAPI shows how to use the new abstracted API
|
||||
func ExampleWithNewAPI(db *gorm.DB) {
|
||||
// Create database adapter
|
||||
dbAdapter := NewGormAdapter(db)
|
||||
|
||||
// Create model registry
|
||||
registry := NewModelRegistry()
|
||||
// registry.RegisterModel("public.users", &User{})
|
||||
|
||||
// Create handler with new API
|
||||
handler := NewHandler(dbAdapter, registry)
|
||||
|
||||
// Create router adapter
|
||||
routerAdapter := NewStandardRouter()
|
||||
|
||||
// Register routes using new API
|
||||
routerAdapter.RegisterRoute("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
reqAdapter := NewHTTPRequest(r)
|
||||
respAdapter := NewHTTPResponseWriter(w)
|
||||
handler.Handle(respAdapter, reqAdapter, params)
|
||||
})
|
||||
}
|
||||
|
||||
// ExampleWithBun shows how to switch to Bun ORM
|
||||
func ExampleWithBun(bunDB *bun.DB) {
|
||||
// Create Bun adapter
|
||||
dbAdapter := NewBunAdapter(bunDB)
|
||||
|
||||
dbAdapter := database.NewBunAdapter(bunDB)
|
||||
|
||||
// Create model registry
|
||||
registry := NewModelRegistry()
|
||||
registry := modelregistry.NewModelRegistry()
|
||||
// registry.RegisterModel("public.users", &User{})
|
||||
|
||||
|
||||
// Create handler
|
||||
handler := NewHandler(dbAdapter, registry)
|
||||
|
||||
// Setup routes same as with GORM
|
||||
router := NewStandardRouter()
|
||||
router.RegisterRoute("/{schema}/{entity}", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
reqAdapter := NewHTTPRequest(r)
|
||||
respAdapter := NewHTTPResponseWriter(w)
|
||||
|
||||
// Setup routes
|
||||
muxRouter := mux.NewRouter()
|
||||
SetupMuxRoutes(muxRouter, handler)
|
||||
}
|
||||
|
||||
// SetupBunRouterRoutes sets up bunrouter routes for the ResolveSpec API
|
||||
func SetupBunRouterRoutes(bunRouter *router.StandardBunRouterAdapter, handler *Handler) {
|
||||
r := bunRouter.GetBunRouter()
|
||||
|
||||
r.Handle("POST", "/:schema/:entity", func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
params := map[string]string{
|
||||
"schema": req.Param("schema"),
|
||||
"entity": req.Param("entity"),
|
||||
}
|
||||
reqAdapter := router.NewHTTPRequest(req.Request)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.Handle(respAdapter, reqAdapter, params)
|
||||
return nil
|
||||
})
|
||||
|
||||
r.Handle("POST", "/:schema/:entity/:id", func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
params := map[string]string{
|
||||
"schema": req.Param("schema"),
|
||||
"entity": req.Param("entity"),
|
||||
"id": req.Param("id"),
|
||||
}
|
||||
reqAdapter := router.NewHTTPRequest(req.Request)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.Handle(respAdapter, reqAdapter, params)
|
||||
return nil
|
||||
})
|
||||
|
||||
r.Handle("GET", "/:schema/:entity", func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
params := map[string]string{
|
||||
"schema": req.Param("schema"),
|
||||
"entity": req.Param("entity"),
|
||||
}
|
||||
reqAdapter := router.NewHTTPRequest(req.Request)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.HandleGet(respAdapter, reqAdapter, params)
|
||||
return nil
|
||||
})
|
||||
|
||||
r.Handle("GET", "/:schema/:entity/:id", func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
params := map[string]string{
|
||||
"schema": req.Param("schema"),
|
||||
"entity": req.Param("entity"),
|
||||
"id": req.Param("id"),
|
||||
}
|
||||
reqAdapter := router.NewHTTPRequest(req.Request)
|
||||
respAdapter := router.NewHTTPResponseWriter(w)
|
||||
handler.HandleGet(respAdapter, reqAdapter, params)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// ExampleWithBunRouter shows how to use bunrouter from uptrace
|
||||
func ExampleWithBunRouter(db *gorm.DB) {
|
||||
// Create handler (can use any database adapter)
|
||||
handler := NewAPIHandler(db)
|
||||
|
||||
func ExampleWithBunRouter(bunDB *bun.DB) {
|
||||
// Create handler with Bun adapter
|
||||
handler := NewHandlerWithBun(bunDB)
|
||||
|
||||
// Create bunrouter
|
||||
router := NewStandardBunRouterAdapter()
|
||||
|
||||
bunRouter := router.NewStandardBunRouterAdapter()
|
||||
|
||||
// Setup ResolveSpec routes with bunrouter
|
||||
SetupBunRouterWithResolveSpec(router.GetBunRouter(), handler)
|
||||
|
||||
SetupBunRouterRoutes(bunRouter, handler)
|
||||
|
||||
// Start server
|
||||
// http.ListenAndServe(":8080", router.GetBunRouter())
|
||||
// http.ListenAndServe(":8080", bunRouter.GetBunRouter())
|
||||
}
|
||||
|
||||
// ExampleBunRouterWithBunDB shows the full uptrace stack (bunrouter + Bun ORM)
|
||||
func ExampleBunRouterWithBunDB(bunDB *bun.DB) {
|
||||
// Create Bun database adapter
|
||||
dbAdapter := NewBunAdapter(bunDB)
|
||||
|
||||
dbAdapter := database.NewBunAdapter(bunDB)
|
||||
|
||||
// Create model registry
|
||||
registry := NewModelRegistry()
|
||||
registry := modelregistry.NewModelRegistry()
|
||||
// registry.RegisterModel("public.users", &User{})
|
||||
|
||||
|
||||
// Create handler with Bun
|
||||
handler := NewHandler(dbAdapter, registry)
|
||||
|
||||
// Create compatibility wrapper for existing APIs
|
||||
compatHandler := &APIHandlerCompat{
|
||||
legacyHandler: nil, // No legacy handler needed
|
||||
newHandler: handler,
|
||||
db: nil, // No GORM dependency
|
||||
}
|
||||
|
||||
|
||||
// Create bunrouter
|
||||
router := NewStandardBunRouterAdapter()
|
||||
|
||||
bunRouter := router.NewStandardBunRouterAdapter()
|
||||
|
||||
// Setup ResolveSpec routes
|
||||
SetupBunRouterWithResolveSpec(router.GetBunRouter(), compatHandler)
|
||||
|
||||
SetupBunRouterRoutes(bunRouter, handler)
|
||||
|
||||
// This gives you the full uptrace stack: bunrouter + Bun ORM
|
||||
// http.ListenAndServe(":8080", router.GetBunRouter())
|
||||
}
|
||||
// http.ListenAndServe(":8080", bunRouter.GetBunRouter())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user