Crud API for gorm.io Object with opentracing support
Overview
Provide the crud API of gorm object
Support the validation interface
type Validation interface {
TableName() string
Validate() (map[string]interface{}, bool)
OrderColumns() []string
FilterColumns() map[string]string
FindFromRequest(r *http.Request) error
QueryAllFromRequest(r *http.Request, q *gorm.DB) *gorm.DB
}
Enable the gorm backend
db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
api.SetDB(db)
db.AutoMigrate(&TestObject{})
db.Use(gormopentracing.New()) //if using tracing
Set your route to the object
router := mux.NewRouter().StrictSlash(true)
router.Use(middlewares.JaeggerLogger) //use of jaegger logger
router.Use(middlewares.RecoverWrap)
router.Use(middlewares.JwtAuthentication)
middlewares.Routes = utils.Routes{}.Append(api.CrudRoutes(&TestObject{},
utils.DefaultQueryAll, utils.NoRight, //QueryAll
api.DefaultRightAccess, utils.NoRight, //Read
api.DefaultRightAccess, utils.NoRight, //Create
api.DefaultRightEdit, utils.NoRight, //Edit
api.DefaultRightAccess, utils.NoRight, //Delete
))
for _, route := range middlewares.Routes {
router.Methods(route.Method).Path(route.Pattern).Handler(route.HandlerFunc).Name(route.Name)
}
srv := http.Server{
ReadTimeout: 0,
WriteTimeout: 600 * time.Second,
IdleTimeout: 0,
ReadHeaderTimeout: 0,
Addr: ":" + port,
Handler: router,
}
srv.ListenAndServe()
full example (main.go)