easyapi

package module
v0.0.0-...-389a64c Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2021 License: MIT Imports: 15 Imported by: 0

README

Easy Api

Build on top of gin and gorm, Easyapi is a an extension of services to manage easily automatic logic in the creation of resources.

Documentation

Built on top of
Summary

Installation

# 

Documentation

Router

Go api has by default 1 simple orm manager and 1 simple odm manager.

In each CRUDL method, you can specify the basic method you want :

  • C (Create) : Will create a POST /{resource}/{id} route
  • R (Read) : Will create a GET /{resource}/{id} route
  • U (Update) : Will create a PATCH /{resource}/{id} route
  • D (Delete) : Will create a DELETE /{resource}/{id} route
  • L (List) : Will create a GET /{resource} route and return a collection of resources

To enable all route just pass "" as the fourth argument. To enable only some methods you can pass a parameter like CR to enable only Create and Read routes.

...

func main() {
    r := gin.Default()

    // Init ORM
    initORM()
    initODM()

    // Resources CRUD routes
    easyapi.CRUDL(r, "/users", new(model.User), "CRUL")
    easyapi.CRUDL(r, "/banks", new(model.Bank), "")
}

func initORM() {
    err := orm.Init(mysql.Open(os.Getenv("DSN")), true, &logger.Config{
        SlowThreshold:             time.Second,
        LogLevel:                  logger.Info,
        IgnoreRecordNotFoundError: false,
        Colorful:                  true,
    })
    if err != nil {
        log.Fatal("Error init database")
    }
}

func initODM() {
    err := odm.Init(odm.Config{
        ConnectionString: os.Getenv("MONGO_SERVER"),
        Database:         os.Getenv("MONGO_DATABASE"),
    }, false)
    if err != nil {
        log.Fatal("Error init mongodb")
    }
}
Security & Access management
# 
Resource validation
# 
Resource serializer
# 
Filtering & Pagination
# 
Event manager
# 

Documentation

Index

Constants

View Source
const (
	SERIALIZER_CONTEXT_KEY_ONE  = "one"
	SERIALIZER_CONTEXT_KEY_LIST = "list"
)
View Source
const (
	CONTEXT_KEY_TOKEN = "ctx.auth.token"
)

Variables

View Source
var (
	BinderConfig = &binderConfig{

		KeepBody: false,
	}
)

Functions

func AppendBindings

func AppendBindings(item interface{}) error

Append bindings based on GetUUIDBindings resource method

func BindAndValidate

func BindAndValidate(c *gin.Context, i interface{}) error

Bind and validate recursively a request body to a resource

func CRUDL

func CRUDL(r gin.IRoutes, path string, i interface{}, methods string)

Shortcut to handle multiple crud requests

func CheckPassword

func CheckPassword(password string, p layer.PasswordEncoderAware) bool

Check the strong password

func EncodePassword

func EncodePassword(p layer.PasswordEncoderAware) error

Strongly encode a password based on the resource ID

func HandleDelete

func HandleDelete(c *gin.Context, i interface{}, id string)

Gin handler for a DELETE request

func HandleGet

func HandleGet(c *gin.Context, i interface{}, id string)

Gin handler for a GET request

func HandleList

func HandleList(c *gin.Context, i interface{})

Gin handler for a LIST request

func HandlePatch

func HandlePatch(c *gin.Context, i interface{}, id string)

Gin handler for a PATCH request

func HandlePost

func HandlePost(c *gin.Context, i interface{})

Gin handler for a POST request

func HttpError

func HttpError(c *gin.Context, code int, message string, data interface{}) error

Create a http error and display it on gin context

func NewItem

func NewItem(i interface{}, sc *layer.SerializeGroups) interface{}

Create a new item single response

func NewValidationError

func NewValidationError(tag string, field string, resource interface{}) layer.ValidationError

Create a new validation error from a tag and a field

func ParseToken

func ParseToken(tokenString string) (interface{}, error)

Parse a string into a valid JWT token and returns the token information or return an error

func RemoveUUIDBindings

func RemoveUUIDBindings(item interface{})

Remove bindings of a resource

func Serialize

func Serialize(i interface{}, sc *layer.SerializeGroups) interface{}

Serialize a resource (aware of Interface or not)

Types

type CollectonItem

type CollectonItem struct {
	Items []interface{} `json:"items"`
	Count int           `json:"count,omitempty"`
	Total int           `json:"total,omitempty"`
	Links *layer.Links  `json:"_links,omitempty"`
}

func NewCollectionItem

func NewCollectionItem(items []interface{}, sc *layer.SerializeGroups) *CollectonItem

Create a new item collection response

type Token

type Token struct {
	Value     string
	ExpiresAt int
}

func GenerateToken

func GenerateToken(t layer.TokenInterface, duration time.Duration) *Token

Generate a JWT token

Directories

Path Synopsis
db
dao
odm
orm

Jump to

Keyboard shortcuts

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