Middleware Handler Documentation
This code defines middleware functions for an Echo web server and PocketBase application. These middleware functions include setting default headers, printing routes, and restricting the deletion of records that have references in other tables.
Functions
BindDefaultMiddleware
func BindDefaultMiddleware(pbx *core.ServeEvent) error
Binds default middleware to the Echo router.
pbx
: The PocketBase serve event.
PrintRoutes
func PrintRoutes(pbx *core.ServeEvent) error
Prints all the routes registered in the Echo router.
pbx
: The PocketBase serve event.
RestrictRelationDelete
func RestrictRelationDelete(pbx *core.ServeEvent) error
Restricts the deletion of records that have references in other tables.
pbx
: The PocketBase serve event.
Example Usage
Initializing the Middleware
package main
import (
"fio-backend/handler"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
func main() {
// Initialize Echo
e := echo.New()
// Initialize PocketBase
pb := pocketbase.New()
// Bind default middleware
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return handler.BindDefaultMiddleware(e)
})
// Print routes
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return handler.PrintRoutes(e)
})
// Restrict relation delete
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return handler.RestrictRelationDelete(e)
})
// Start the server
e.Logger.Fatal(e.Start(":8080"))
}
Making Requests
Example Request to Print Routes
curl -X GET http://localhost:8080/print-routes
Example Request to Restrict Relation Delete
curl -X DELETE http://localhost:8080/api/v1/records/record123?restrict=true&excludes=table1,table2
This documentation provides an overview of the middleware functions, their purpose, and example usage. By following the example, you can initialize the middleware, bind routes, and make requests to the various endpoints.