crudp

package module
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 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)
  • 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.
  • Explicit Error Handling: CRUD methods return explicit errors for better flow control.

Quick Start

1. Define a Handler
type UserHandler struct{}

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

// On server:
// req, _ := decodeRequest(body)
// resp, _ := cp.Execute(ctx, 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(ctx context.Context, data ...any) (any, error)
}

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 EncodeFunc, decode DecodeFunc) *CrudP

New creates a new CrudP instance with mandatory serialization functions

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(ctx context.Context, 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(ctx context.Context, req *BatchRequest) (*BatchResponse, error)

Execute processes a BatchRequest and returns a BatchResponse

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 the default CRUD API endpoint and any custom routes from handlers

func (*CrudP) SetLog

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

SetLog configures a custom logging function

type DecodeFunc

type DecodeFunc func(input any, output any) error

type Deleter

type Deleter interface {
	Delete(ctx context.Context, data ...any) (any, error)
}

type EncodeFunc

type EncodeFunc func(input any, output any) error

type FieldValidator

type FieldValidator interface {
	ValidateField(fieldName string, value string) error
}

FieldValidator validates individual fields for UI (optional)

type HttpRouteProvider

type HttpRouteProvider interface {
	RegisterRoutes(mux *http.ServeMux)
}

HttpRouteProvider allows handlers to register custom HTTP routes

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(ctx context.Context, data ...any) (any, error)
}

type Updater

type Updater interface {
	Update(ctx context.Context, data ...any) (any, error)
}

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