go-crud-api

command module
v0.0.0-...-5f6e6a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 18 Imported by: 0

README

Crud API for gorm.io Object with opentracing support

Report Report doc Build Status

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)

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL