crudp

package module
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 6 Imported by: 0

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 defines role-based access control (required if CRUD implemented)

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(data ...any) any
}

Separate CRUD interfaces - handlers implement only what they need Return `any` which internally can be slice for multiple items

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 with binary codec by default

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) 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, data ...any) error
}

DataValidator validates complete data before action (required if CRUD implemented)

type Deleter

type Deleter interface {
	Delete(data ...any) any
}

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 unique name for the entity (required if CRUD implemented)

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(data ...any) any
}

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(data ...any) any
}

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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