crudp

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 6 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.

Quick Start

1. Define a Handler
type UserHandler struct{}

func (h *UserHandler) Create(data ...any) any {
    // Process data and return result (which can be an error)
    return &User{ID: 1, Name: "John"}
}
2. Register and Execute
cp := crudp.New(binary.Encode, binary.Decode)
cp.RegisterHandler(&UserHandler{})

// On server:
// req, _ := decodeRequest(body)
// resp, _ := cp.Execute(req)

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 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(encode, decode func(any, any) error) *CrudP

New creates a new CrudP instance with custom serialization functions

func NewDefault added in v0.0.20

func NewDefault() *CrudP

NewDefault creates a CrudP instance using the recommended binary codec

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) RegisterHandler

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

RegisterHandler prepares the shared handler table

func (*CrudP) RegisterRoutes

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

RegisterRoutes registers endpoints for each handler and a global /batch endpoint

func (*CrudP) SetLog

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

SetLog configures a custom logging function

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 allows override of automatic name (optional) If not implemented, reflection is used: TypeName -> snake_case

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
}

type Validator

type Validator interface {
	Validate(action byte, data ...any) error
}

Validator validates complete data before action (optional)

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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