gocqrs

package module
v0.0.0-...-647cb14 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventTypeHeader     = "X-Event"
	EntityVersionHeader = "X-LockVersion"
	CreateEventHeader   = "X-Create"
	EntityHeader        = "X-Entity"
	EventIDHeader       = "X-EventID"
	EntityGroupHeader   = "X-Group"
	SessionHeader       = "X-Session"
	UserHeader          = "X-User"
	CookieName          = "san"
)
View Source
const (
	Created = "Created"
	Updated = "Updated"
	Deleted = "Deleted"
)
View Source
const (
	RebuiltOpt = "rebuild"
	PurgeOpt   = "purge"
)
View Source
const (
	AllRole = "all"
)
View Source
const (
	GroupEntity = "groups"
)
View Source
const UserCreatedEvent = "UserCreated"
View Source
const (
	UserEntity = "users"
)
View Source
const UserTokenUpdated = "UserTokenUpdated"

Variables

View Source
var (
	InvalidEntityError    = errors.New("Invalid entity in command")
	InvalidReferenceError = errors.New("Invalid reference")
	BaseUnseted           = errors.New("Basestruct not set, should be setted")
)
View Source
var (
	FailStoreError      = errors.New("Failed to store event, db issue")
	LockVersionError    = errors.New("Invalid lock version")
	StreamNotFoundError = errors.New("Stream don't exist")
)
View Source
var (
	EntityDeleted = errors.New("Entity deleted, new to undeletet to update")
)

Functions

func AuthHandler

func AuthHandler(c *gin.Context)

func AuthRenewHandler

func AuthRenewHandler(c *gin.Context)

func BuildToken

func BuildToken(u User) string

func DecodeEvent

func DecodeEvent(e Eventer, i interface{}) error

func DocHandler

func DocHandler(c *gin.Context)

func EntityHandler

func EntityHandler(c *gin.Context)

func EventsDocHandler

func EventsDocHandler(c *gin.Context)

func HTTPEventHandler

func HTTPEventHandler(c *gin.Context)

func ToMap

func ToMap(i interface{}) map[string]interface{}

func UpHandler

func UpHandler(c *gin.Context)

Types

type APPDocs

type APPDocs struct {
	Name      string              `json:"name"`
	Version   string              `json:"version"`
	Entities  map[string][]string `json:"entities"`
	Endpoints []Endpoint          `json:"endpoints"`
}

func GenerateDocs

func GenerateDocs(app *App) APPDocs

func (APPDocs) GetEvents

func (ad APPDocs) GetEvents(e string) []string

type Aggregator

type Aggregator interface {
	Aggregate(id string, entity interface{})
}

type App

type App struct {
	Version string `json:"version"`
	Name    string `json:"name"`
	Port    string `json:"port"`

	// main log stream
	MainLog string `json:"mlog"`

	// Documentations
	BaseURL   string     `json:"baseURL"`
	Endpoints []Endpoint `json:"endpoints"`

	// user roles for auth
	Roles map[string]Role `json:"roles"`

	Entities map[string]*EntityConf `json:"entities"`
	Store    EventStore             `json:"-"`

	// Gin router
	Router *gin.Engine

	// turn off auth service check
	FirstRun        bool   `json:"firtRun"`
	AuthOff         bool   `json:"authOff"`
	Secret          string `json:"-"`
	SessionValidity string `json:"sessionValidity"`

	Domain        string   `json:"sessionDomain"`
	LoginReferers []string `json:"loginReferers"`
	// contains filtered or unexported fields
}

func NewApp

func NewApp(name string, store EventStore) *App

func (*App) AddEndpoint

func (app *App) AddEndpoint(e Endpoint)

func (*App) AddRoles

func (app *App) AddRoles(roles ...Role)

func (*App) Auth

func (app *App) Auth(evh ...EventHandler)

Add Auth functionality

func (*App) CheckReference

func (app *App) CheckReference(e, k, value string, null bool) error

func (*App) Entity

func (app *App) Entity(name, id string) (*Entity, uint64, error)

func (*App) GenDocs

func (app *App) GenDocs() APPDocs

func (*App) HandleEvent

func (app *App) HandleEvent(entityName, id, accid, userid, role string, ev Eventer, versionLock uint64) (string, uint64, error)

func (*App) RegisterEntity

func (app *App) RegisterEntity(e *EntityConf) *App

func (*App) Run

func (app *App) Run(port string) error

Start app

func (*App) SessionTTL

func (app *App) SessionTTL(d string)

func (*App) String

func (app *App) String() string

type BaseEvent

type BaseEvent struct {
	EventID        string    `json:"eid"`
	EventTimestamp time.Time `json:"timestamp"`
	EventStream    string    `json:"stream,omitempty"`
	EventType      string    `json:"type"`
	EventVersion   uint64    `json:"version"`
}

type BasicEntity

type BasicEntity struct {
	AccountID string    `json:"accID"`
	CreatedBy string    `json:"createBy"`
	Created   time.Time `json:"created,omitempty"`

	UpdatedBy string    `json:"createBy"`
	Updated   time.Time `json:"updated,omitempty"`
}

type CRUDHandler

type CRUDHandler struct {
	EntityName   string `json:"entityName"`
	CheckVersion bool   `json:"checkVersion"`
}

func NewCRUDHandler

func NewCRUDHandler(name string) CRUDHandler

func (CRUDHandler) CheckBase

func (ch CRUDHandler) CheckBase(e Eventer) bool

func (CRUDHandler) CreateEvent

func (ch CRUDHandler) CreateEvent() string

func (CRUDHandler) DeletedEvent

func (ch CRUDHandler) DeletedEvent() string

func (CRUDHandler) EventName

func (ch CRUDHandler) EventName() []string

func (CRUDHandler) Handle

func (ch CRUDHandler) Handle(id string, accountid, userid, role string, ev Eventer, en *Entity, replay bool) (StoreOptions, error)

Handler CRUD events

func (CRUDHandler) UnDeletedEvent

func (ch CRUDHandler) UnDeletedEvent() string

func (CRUDHandler) UpdateEvent

func (ch CRUDHandler) UpdateEvent() string

type Endpoint

type Endpoint struct {
	Path        string `json:"path"`
	Method      string `json:"method"`
	Description string `json:"description,omitempty"`
	ExampleBody string `json:"exampleBody,omitempty"`
}

func NewEndpoint

func NewEndpoint(path, method string, exampleBody interface{}) Endpoint

type Entity

type Entity struct {
	ID      string                 `json:"id"`
	Group   string                 `json:"group"`
	Version uint64                 `json:"version"`
	Deleted bool                   `json:"deleted"`
	Data    map[string]interface{} `json:"data"`
}

func (*Entity) Decode

func (e *Entity) Decode(i interface{}) error

type EntityConf

type EntityConf struct {
	Name string `json:"name"`

	CRUD bool `json:"crud"`

	Description string `json:"desc"`
	// basic entity prefix
	StreamPrefix      string `json:"stream_prefix"`
	CorrelationStream string `json:"correlation_stream"`

	// Entity ID references
	EntityReferences []EntityReference `json:"references"`

	// Event Handlers/Aggregators
	EventHandlers map[string]EventHandler `json:"handlers"`

	Validators map[string]Validator `json:"validators"`
	BaseStruct interface{}          `json:"base,omitempty"`
	BaseSeted  bool

	ReadRoles []string `json:"roles,omitempty"`
}

func NewEntityConf

func NewEntityConf(name string) *EntityConf

func (*EntityConf) AddCRUD

func (e *EntityConf) AddCRUD(checkVersion bool) *EntityConf

func (*EntityConf) AddEventHandler

func (e *EntityConf) AddEventHandler(eh ...EventHandler) error

func (*EntityConf) AddValidator

func (e *EntityConf) AddValidator(v ...Validator) error

func (*EntityConf) Aggregate

func (ec *EntityConf) Aggregate(id string, events chan Eventer) (*Entity, error)

func (*EntityConf) Reference

func (e *EntityConf) Reference(en, k string, null bool)

func (*EntityConf) SetBaseStruct

func (e *EntityConf) SetBaseStruct(i interface{})

type EntityProperty

type EntityProperty struct {
	Name        string
	Validations []func(interface{}) error
}

func NewProperty

func NewProperty(name string) *EntityProperty

func (*EntityProperty) Email

func (ep *EntityProperty) Email() *EntityProperty

func (*EntityProperty) ID

func (ep *EntityProperty) ID() *EntityProperty

func (*EntityProperty) IP

func (ep *EntityProperty) IP() *EntityProperty

func (*EntityProperty) NotNull

func (ep *EntityProperty) NotNull() *EntityProperty

func (*EntityProperty) String

func (ep *EntityProperty) String() *EntityProperty

func (*EntityProperty) URL

func (ep *EntityProperty) URL() *EntityProperty

type EntityReference

type EntityReference struct {
	Entity string `json:"entity"`
	Key    string `json:"key"`
	Null   bool   `json:"null"`
}

type Event

type Event struct {
	BaseEvent
	Group             string                 `json:"group,omitempty"`
	Entity            string                 `json:"ent,omitepty"`
	CorrelationStream string                 `json:"cid,omitempty"`
	EntityID          string                 `json:"id,omitempty"`
	StreamPrefix      string                 `json:"streamPre,omitempty"`
	EventData         map[string]interface{} `json:"data,omitempty"`
}

func NewEvent

func NewEvent(id, t string, data map[string]interface{}) *Event

func (*Event) ClearData

func (e *Event) ClearData()

func (*Event) GetData

func (e *Event) GetData() map[string]interface{}

func (*Event) GetId

func (e *Event) GetId() string
func (e *Event) GetLinks() []string

func (*Event) GetStream

func (e *Event) GetStream() string

func (*Event) GetType

func (e *Event) GetType() string

func (*Event) GetVersion

func (e *Event) GetVersion() uint64

func (*Event) Has

func (e *Event) Has(k string) bool

func (*Event) SetData

func (e *Event) SetData(k string, i interface{})

func (*Event) SetStructData

func (e *Event) SetStructData(i interface{}) error

func (*Event) String

func (event *Event) String() string

type EventHandler

type EventHandler interface {
	EventName() []string
	Handle(id, accid, userid, role string, e Eventer, entity *Entity, replay bool) (StoreOptions, error)
	CheckBase(e Eventer) bool
}

type EventStore

type EventStore interface {
	Store(e Eventer, opt StoreOptions) (uint64, error)
	Range(streamid string) (chan Eventer, uint64)
	Version(streamid string) (uint64, error)
	Scan(streamid string, from, to uint64) chan Event
}

Eventstore interface

type Eventer

type Eventer interface {
	GetId() string
	GetStream() string
	GetVersion() uint64
	GetType() string
	GetData() map[string]interface{}
	SetData(k string, i interface{})
	ClearData()
	GetLinks() []string
	Has(string) bool
}

type Group

type Group struct {
	BasicEntity
	ID   string            `json:"id"`
	Data map[string]string `json:"data"`
}

type Reader

type Reader interface {
	Sync(entity, id string) error
}

type Role

type Role struct {
	Name string `json:"name"`

	// Access to entities
	Entities []string
	// events and read cmd that can be executed ,default ALL
	Allowed []string `json:"allowed"`

	NotAllowed []string `json:"noAllowed"`
}

func NewRole

func NewRole(r string) *Role

func (*Role) Allow

func (r *Role) Allow(cmd ...string)

func (*Role) Can

func (r *Role) Can(e string) bool

func (*Role) CanRead

func (r *Role) CanRead(e string) bool

func (*Role) NotAllow

func (r *Role) NotAllow(cmd ...string)

type SessionClaims

type SessionClaims struct {
	Username string `json:"use"`
	Role     string `json:"rol"`
	jwt.StandardClaims
}

func AuthToken

func AuthToken(t, secret string) (*SessionClaims, error)

type SimpleValidator

type SimpleValidator struct {
	// contains filtered or unexported fields
}

func NewSimpleValidator

func NewSimpleValidator(ep ...*EntityProperty) *SimpleValidator

func (SimpleValidator) GetName

func (sv SimpleValidator) GetName() string

func (SimpleValidator) Validate

func (sv SimpleValidator) Validate(e Entity) error

type StoreOptions

type StoreOptions struct {
	LockVersion uint64 `json:"lockversion"`
	Retry       bool   `json:"retry"`
	Create      bool   `json:"create"`
}

Storing event options

type User

type User struct {
	Username string    `json:"username"`
	Password string    `json:"password"`
	Role     string    `json:"role"`
	Created  time.Time `json:"created`

	AccountID string `json:"accid"`

	ActiveToken bool   `json:"activeToken"`
	Token       string `json:"token"`

	Groups []string               `json:"groups"`
	Data   map[string]interface{} `json:"data"`
}

func (*User) CheckPassword

func (u *User) CheckPassword(password string) error

func (*User) CheckToken

func (u *User) CheckToken(token string) error

func (*User) Encrypt

func (u *User) Encrypt() error

func (*User) GenerateToken

func (u *User) GenerateToken()

func (*User) Valid

func (u *User) Valid() error

type UserEventHandler

type UserEventHandler struct {
}

func (UserEventHandler) CheckBase

func (uh UserEventHandler) CheckBase(e Eventer) bool

func (UserEventHandler) EventName

func (uh UserEventHandler) EventName() []string

func (UserEventHandler) Handle

func (uh UserEventHandler) Handle(id, accid, userid, role string, event Eventer, entity *Entity, replay bool) (StoreOptions, error)

type Validator

type Validator interface {
	GetName() string
	Validate(e Entity) error
}

type View

type View struct {
	Name string `json:"view"`

	All     bool `json:"all"`
	CatchUp bool `json:"catchup"`

	V     Viewer
	Store EventStore
	// contains filtered or unexported fields
}

func NewView

func NewView(name string, i Viewer) *View

func (*View) Run

func (v *View) Run(action string, params map[string]string, dev bool) error

type Viewer

type Viewer interface {
	Init(params map[string]string, dev bool)
	Purge() error
	Status() (uint64, error)
	Stream() string
	Rebuild() error
	Apply(event Event) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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