crpc-go

module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT

README

CellsWeb Logo

CellsWeb RPC Go

Native Go RPC functions, schemas, middleware, fasthttp/net-http adapters, multipart intake, Unix IPC and server-rendered documentation.

Framework overview

Capability Go implementation
Registration Generic typed function registration or explicit FunctionDefinition
Middleware Ordered handlers with options and context Set/Get; startup-only registration
Schemas Native schema builders/struct metadata, HTTP input/output checks
HTTP fasthttp server and net/http adapter, optional custom routes
Uploads Multipart routing, bounds, temporary-file ownership and typed file injection
IPC Unix newline/length framing, bounded stdio and maintained Node client
Documentation Global enable/path controls, function and middleware schema views
Tooling Go CLI initialization/generators, tests, standalone SDK resolution

Node/Bun engine, HTTP client/React SDK and Go/Node IPC example are distinct integration paths. A browser uses HTTP, while a local Node daemon client can use Unix IPC. HTTP File/Blob uploads do not become live file objects over JSON IPC.

v2.0.1 scaffolder safety

Version v2.0.1 adds non-destructive project, function and middleware generation. An existing final target is refused, even when empty. Public v2.0.0 tools predate these safeguards. Check official module availability before using the versioned installation commands below; during prepublication review, use approved private candidate resolution. Scaffolder migration.

Install the versioned SDK and CLI

github.com/CellsWeb/crpc-go/v2@v2.0.1 retains the hardening, documentation parity, explicit HTTP null and standalone scaffold fixes, and adds strict non-destructive generation. Use Go>=1.27.4:

go install github.com/CellsWeb/crpc-go/v2/cmd/crpc-go@v2.0.1
crpc-go version
crpc-go init my-api
cd my-api
go mod tidy
go build ./...
go test ./...
crpc-go dev

Put GOBIN (or $(go env GOPATH)/bin) on PATH. The versioned CLI generates a matching tagged SDK dependency without a workspace or local replacement. For an existing module: go get github.com/CellsWeb/crpc-go/v2@v2.0.1.

Source/VCS-built CLIs target SDK v2.0.1; before public availability, use an explicit workspace when testing local SDK changes. The CLI display version alone does not identify its generated dependency. See installation and migration.

Generate before customizing

CellsWeb RPC does not provide built-in authentication, identity, sessions or RBAC. Middleware and auth/rate/storage starter examples are ordinary application code.

crpc-go create:middleware --name tenantResolver
crpc-go create:function --ns catalog --name describe

Go requires flags and has no function wizard. Every generator refuses an existing final target, including an empty directory; it never replaces customizations. Release availability. Import new packages in main, then edit generated contracts.go, middlewares.go and run.go. Ordered middleware and context remain application-defined; typed retrieval does not prove dependencies at compile time.

CLI-first quickstart · Annotated files · Middleware and context.

Executable typed server

In a fresh Go module, go get github.com/CellsWeb/crpc-go/v2@v2.0.1, then save this as main.go. The example registers before serving and uses explicit CORS/config. The documentation decision below is application configuration, not an implicit Go core environment parser.

package main

import (
    "log"
    "os"
    "github.com/CellsWeb/crpc-go/v2/pkg/rpc"
    "github.com/CellsWeb/crpc-go/v2/pkg/schema"
)

type Input struct { Name string `json:"name"` }
type Output struct { Greeting string `json:"greeting"` }

func main() {
    rpc.Register(rpc.FunctionConfig[Input, Output]{
        Namespace: "public", FunctionName: "hello",
        Metadata: rpc.FunctionMetadata{Description: "Greet a named caller"},
        InputSchema: schema.Object(map[string]*schema.SchemaRule{
            "name": schema.String().Min(1),
        }),
        OutputSchema: schema.FromStruct(Output{}),
        Handler: func(_ *rpc.Context, input Input) (*Output, error) {
            return &Output{Greeting: "Hello, " + input.Name + "!"}, nil
        },
    })
    app, err := rpc.New(rpc.Config{
        RpcPath: "/rpc", Host: "127.0.0.1", Port: 4000,
        CORS: &rpc.CorsConfig{AllowOrigins: []string{"*"}},
        DocsEnabled: os.Getenv("NODE_ENV") != "production", DocsPath: "/docs",
    })
    if err != nil { log.Fatal(err) }
    app.UseGlobalRegistry()
    if err := app.ListenAndServe(); err != nil { log.Fatal(err) }
}

Run go mod tidy, go run ., then:

curl http://localhost:4000/rpc -H 'Content-Type: application/json' \
  --data '{"namespace":"public","functionName":"hello","data":{"name":"Ada"}}'

Success contains {"status":"ok","data":{"greeting":"Hello, Ada!"}}; a numeric name produces the existing structured validation error with default HTTP 200. Use schema.Optional, arrays and native builders for precise nullable/scalar contracts; Go is not an implementation of every TypeBox/JSON Schema keyword.

Middleware and application context

Register middleware through rpc.RegisterMiddleware or an instance's RegisterMiddleware, then attach it with rpc.DefineMiddlewares(rpc.M("auth", options)) in FunctionConfig.Middlewares. A middleware receives (*rpc.Context, headers, options) and returns an error to stop execution. Verify credentials before calling ctx.Set; handlers can retrieve that server-owned state with ctx.Get or rpc.GetContext.

Go middleware options/header schemas are documentation metadata, not automatic runtime middleware validation. Validate credentials and configuration in the middleware itself. Unlike remote HTTP/IPC input, local test setup is trusted application code. There is no supported remote skip/context-injection auth path. Register functions, middleware and custom routes before active serving.

Multipart, documentation and IPC

Use schema.File() or file arrays and *rpc.UploadedFile fields in typed input. Its API uses methods: Filename(), Size(), MimeType(), Bytes(), Reader(), TempPath(), SaveTo(path). Persist to a safe application-owned location during processing; framework temporary files are cleaned afterward. Files are supported at top level/direct arrays. Routing headers must agree with redundant body fields; headerless fallback is supported. fasthttp buffers transport intake before application multipart processing, so this is not zero network buffering.

Raw rpc.Config controls DocsEnabled/DocsPath explicitly. The generated server uses exported DOCS_ENABLED (trimmed/case-insensitive true/false/1/0), DOCS_PATH and NODE_ENV; omitted visibility defaults on outside production/off in production. Invalid nonblank booleans fail startup; generated Go servers do not load .env. Disabling docs blocks function/middleware/detail routes without unregistering RPCs. Metadata schemas/descriptions should never contain application secrets.

For a Node process, use the maintained example-node client and its matching contract declarations. Go exposes nodebridge.ServeUnixSocket and ServeStdio. Unix supports JSON-Lines or a four-byte big-endian length prefix; stdio is newline-only. The maximum is2,097,152 bytes, counting newline delimiters for line frames. Socket-close, anonymous connection errors and malformed response JSON reject pending Node calls promptly; later calls can reconnect without replay. See IPC guide for framing and daemon lifecycle details.

Contracts

HTTP POST /rpc accepts {namespace,functionName,data}. Objects, arrays, scalars and explicit null retain shape through validation; omitted HTTP data defaults to {}. Success/error envelopes and default HTTP 200 application-error behavior are shared logically with Node. Native schema vocabularies/error details are not identical. Standard HTTP mapping remains opt-in.

Middleware owns authentication and authorization for HTTP, in-memory and IPC. Remote callers cannot bypass it or establish identity by injecting context. Generated middleware is a customization stub, not an identity provider. Metadata IsPublic is deprecated and ignored; it controls neither docs nor auth. Docs are globally configured. Their middleware schema views never render live configured option values/defaults.

Guides and validation

go build ./...
CRPC_STANDALONE_INTEGRATION=1 go test -count=1 ./...
CRPC_STANDALONE_INTEGRATION=1 CRPC_SCAFFOLD_RACE=1 go test -race -count=1 ./...
go vet ./...

The Node example has a separate npm build/typecheck/IPC suite. The coordinated release plan lives in the sibling Node repository's RELEASE.md. No zero-memory, universal latency or security certification is implied by benchmark fixtures.

Contribute

Read CONTRIBUTING.md for setup, focused changes and review. The development guide and source map explain internals and subsystem tests. Please follow the Code of Conduct; report vulnerabilities through the private reporting policy. Licensed under MIT.

Directories

Path Synopsis
cmd
crpc-go command
pkg
rpc

Jump to

Keyboard shortcuts

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