crudp

package module
v0.2.31 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 5 Imported by: 1

README

CRUDP

Project Badges

A JSON/binary CRUD protocol for isomorphic Go applications. Limits its responsibility to handler registration and execution, delegating transport to separate packages.

Features

  • Isomorphic: Same handler code on frontend (WASM) and backend (Server).
  • Automatic Endpoints: Generates HTTP routes (GET /users/{id}, POST /users) automatically.
  • Protocol Agnostic: Decoupled from HTTP, SSE or any transport layer.
  • Batch Processing: Logic to execute multiple operations from a single request.
  • TinyGo Compatible: Optimized for small WASM binaries.

Documentation


Contributing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActionToMethod

func ActionToMethod(action byte) string

ActionToMethod converts CRUD action byte to HTTP method

func MethodToAction

func MethodToAction(method string) byte

MethodToAction converts HTTP method to CRUD action byte

Types

type AccessDeniedHandler added in v0.2.0

type AccessDeniedHandler func(handler string, action byte, userRoles []byte, allowedRoles []byte, errMsg string)

AccessDeniedHandler defines the callback for failed access attempts

type AccessLevel added in v0.2.0

type AccessLevel interface {
	AllowedRoles(action byte) []byte
}

AccessLevel declares which role codes are allowed per action. Used by standalone mode (without tinywasm/rbac).

type BatchRequest

type BatchRequest struct {
	Packets []Packet `json:"packets"`
}

BatchRequest is what is sent in the POST /sync

type BatchResponse

type BatchResponse struct {
	Results []PacketResult `json:"results"`
}

BatchResponse is what is received by SSE or as HTTP response

type Creator

type Creator interface {
	Create(payload any) (any, error)
}

Creator handles entity creation. payload is the entity to create (concrete type asserted internally by the handler). Returns the created entity or an error.

type CrudP

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

CrudP handles automatic handler processing

func New

func New() *CrudP

New creates a new CrudP instance. No codec is configured by default. Use SetCodecs() to provide serialization functions before execution.

func (*CrudP) ApplyMiddleware

func (cp *CrudP) ApplyMiddleware(handler http.Handler) http.Handler

ApplyMiddleware collects all middleware from handlers and wraps the provided handler

func (*CrudP) CallHandler

func (cp *CrudP) CallHandler(handlerID uint8, action byte, data ...any) (any, error)

CallHandler searches and calls the handler directly by shared index

func (*CrudP) Execute

func (cp *CrudP) Execute(req *BatchRequest, inject ...any) (*BatchResponse, error)

Execute processes a BatchRequest and returns a BatchResponse inject contains values to prepend to handler data (e.g., context, http.Request)

func (*CrudP) GetHandlerName

func (cp *CrudP) GetHandlerName(handlerID uint8) string

GetHandlerName returns the handler name by its ID

func (*CrudP) IsDevMode added in v0.2.10

func (cp *CrudP) IsDevMode() bool

IsDevMode returns the current development mode status

func (*CrudP) RegisterHandlers added in v0.1.0

func (cp *CrudP) RegisterHandlers(handlers ...any) error

RegisterHandlers prepares the shared handler table

func (*CrudP) RegisterRoutes

func (cp *CrudP) RegisterRoutes(mux *http.ServeMux)

func (*CrudP) SetAccessCheck added in v0.2.25

func (cp *CrudP) SetAccessCheck(fn func(resource string, action byte, data ...any) bool)

SetAccessCheck configures an external access check function. When set, AllowedRoles() interface is NOT required on handlers. The function receives the handler's resource name, the action byte ('c','r','u','d'), and the raw request data (same variadic as SetUserRoles closure). Must be called before RegisterHandlers(). Mutually exclusive with SetUserRoles — use one or the other.

func (*CrudP) SetAccessDeniedHandler added in v0.2.0

func (cp *CrudP) SetAccessDeniedHandler(fn AccessDeniedHandler)

SetAccessDeniedHandler configures a callback for failed access attempts

func (*CrudP) SetCodecs added in v0.1.0

func (cp *CrudP) SetCodecs(encode, decode func(input any, output any) error)

SetCodecs configures custom serialization functions

func (*CrudP) SetDevMode added in v0.2.0

func (cp *CrudP) SetDevMode(enabled bool)

SetDevMode enables or disables development mode (bypasses access checks)

func (*CrudP) SetLog

func (cp *CrudP) SetLog(log func(...any))

SetLog configures a custom logging function

func (*CrudP) SetUserRoles added in v0.2.1

func (cp *CrudP) SetUserRoles(fn func(data ...any) []byte)

SetUserRoles configures the current user's roles extractor. Access checks are enabled when RegisterRoutes is called on the server.

type DataValidator added in v0.1.0

type DataValidator interface {
	ValidateData(action byte, payload any) error
}

DataValidator validates payload before execution. action: 'c' create, 'r' read, 'u' update, 'd' delete.

type Deleter

type Deleter interface {
	Delete(id string) error
}

Deleter handles entity removal by ID.

type MiddlewareProvider

type MiddlewareProvider interface {
	Middleware(next http.Handler) http.Handler
}

MiddlewareProvider allows handlers to provide global HTTP middleware

type NamedHandler

type NamedHandler interface {
	HandlerName() string
}

NamedHandler provides the resource name used for routing and RBAC.

type Packet

type Packet struct {
	Action    byte     `json:"action"`
	HandlerID uint8    `json:"handler_id"`
	ReqID     string   `json:"req_id"`
	Data      [][]byte `json:"data"`
}

Packet represents both requests and responses of the protocol

type PacketResult

type PacketResult struct {
	Packet             // Embed Packet complete for symmetry with BatchRequest
	MessageType uint8  `json:"message_type"` // 0=Normal, 1=Info, 2=Error, 3=Warning, 4=Success
	Message     string `json:"message"`      // Message for the user
}

type Reader

type Reader interface {
	Read(id string) (any, error)
	List() (any, error)
}

Reader handles entity retrieval. Read returns a single entity by its string ID. List returns all entities (no filter).

type Request added in v0.0.20

type Request struct {
	ReqID string   `json:"req_id"`
	Data  [][]byte `json:"data"`
}

Request represents a single operation request for automatic endpoints

type Response added in v0.0.20

type Response struct {
	ReqID       string   `json:"req_id"`
	Data        [][]byte `json:"data"`
	MessageType uint8    `json:"message_type"`
	Message     string   `json:"message"`
}

Response represents a single operation response for automatic endpoints

type Updater

type Updater interface {
	Update(payload any) (any, error)
}

Updater handles entity mutation. payload is the entity with updated fields. Returns the updated entity or an error.

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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