rpc

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: MIT Imports: 4 Imported by: 0

README

RPC

Simple RPC style APIs with generated clients & servers.

About

All RPC methods are invoked with the POST method, and the RPC method name is placed in the URL path. Input is passed as a JSON object in the body, following a JSON response for the output as shown here:

$ curl -d '{ "project_id": "ping_production" }' https://api.example.com/get_alerts
{
  "alerts": [...]
}

All inputs are objects, all outputs are objects, this improves future-proofing as additional fields can be added without breaking existing clients. This is similar to the approach AWS takes with their APIs.

Commands

There are several commands provided for generating clients, servers, and documentation. Each of these commands accept a -schema flag defaulting to schema.json, see the -h help output for additional usage details.

Clients
  • rpc-dotnet-client generates .NET clients
  • rpc-ruby-client generates Ruby clients
  • rpc-php-client generates PHP clients
  • rpc-elm-client generates Elm clients
  • rpc-go-client generates Go clients
  • rpc-go-types generates Go type definitions
  • rpc-ts-client generates TypeScript clients
Servers
  • rpc-go-server generates Go servers
Documentation
  • rpc-md-docs generates markdown documentation

Schemas

Currently the schemas are loosely a superset of JSON Schema, however, this is a work in progress. See the example schema.

FAQ

Why did you create this project? There are many great options when it comes to building APIs, but to me the most important aspect is simplicity, for myself and for the end user. Simple JSON in, and JSON out is appropriate for 99% of my API work, there's no need for the additional performance provided by alternative encoding schemes, and rarely a need for more complex features such as bi-directional streaming provided by gRPC.
Should I use this in production? Only if you're confident that it supports everything you need, or you're comfortable with forking. I created this project for my work at Apex Software, it may not suit your needs.
Why JSON schemas? I think concise schemas using a DSL are great, until they're a limiting factor. Personally I have no problem with JSON, and it's easy to expand upon when you introduce a new feature, such as inline examples for documentation.
Why doesn't it follow the JSON-RPC spec? I would argue this spec is outdated, there is little reason to support batching at the request level, as HTTP/2 handles this for you.
What does the client output look like? See the Apex Logs Go client for an example, client code is designed to be concise and idiomatic.

GoDoc

Sponsored by my GitHub sponsors:

Documentation

Overview

Package rpc provides simple RPC style APIs with generated clients & servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(message string) error

BadRequest returns a new bad request error.

func Error

func Error(status int, kind, message string) error

Error returns a new ServerError with HTTP status code, kind and message.

func Invalid

func Invalid(message string) error

Invalid returns a validation error.

func NewRequestContext

func NewRequestContext(ctx context.Context, v *http.Request) context.Context

NewRequestContext returns a new context with ctx.

func ReadRequest

func ReadRequest(r *http.Request, value interface{}) error

ReadRequest parses application/json request bodies into value, or returns an error.

func RequestFromContext

func RequestFromContext(ctx context.Context) (*http.Request, bool)

RequestFromContext returns ctx from context.

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError writes an error.

If err is a StatusProvider the status code provided is used, otherwise it defaults to StatusInternalServerError.

If err is a TypeProvider the type provided is used, otherwise it defaults to "internal".

The message in the response uses the Error() implementation.

func WriteHealth

func WriteHealth(w http.ResponseWriter, s interface{})

WriteHealth responds with 200 OK or invokes the Health() method on the server if it implements the HealthChecker interface.

func WriteResponse

func WriteResponse(w http.ResponseWriter, value interface{})

WriteResponse writes a JSON response, or 204 if the value is nil to indicate there is no content.

Types

type HealthChecker

type HealthChecker interface {
	Health() error
}

HealthChecker is the interface used for servers providing a health check.

type ServerError

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

ServerError is a server error which implements StatusProvider and TypeProvider.

func (ServerError) Error

func (e ServerError) Error() string

Error implementation.

func (ServerError) StatusCode

func (e ServerError) StatusCode() int

StatusCode implementation.

func (ServerError) Type

func (e ServerError) Type() string

Type implementation.

type StatusProvider

type StatusProvider interface {
	StatusCode() int
}

StatusProvider is the interface used for providing an HTTP status code.

type TypeProvider

type TypeProvider interface {
	Type() string
}

TypeProvider is the interface used for providing an error type.

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

ValidationError is a field validation error.

func (ValidationError) Error

func (e ValidationError) Error() string

Error implementation.

type Validator

type Validator interface {
	Validate() error
}

Validator is the interface used for validating input.

Directories

Path Synopsis
cmd
generators
internal
Package schema provides the Apex RPC schema.
Package schema provides the Apex RPC schema.

Jump to

Keyboard shortcuts

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