Golang implementation of JSON-RPC 2.0 server with generics

JSON-RPC 2.0

Golang implementation of JSON-RPC 2.0 server with generics.

Go 1.18+ required

Features:

  • Batch request and responses
  • WebSockets

Usage

  1. Create JSON-RPC 2.0 server:
    s := jsonrpc2.New()
  2. Write handler:
    func Multiply(ctx context.Context, args *Args) (int, error) {
        return args.A * args.B, nil
    }
    Handler must have exact two arguments (context and input of any json serializable type) and exact two return values (output of any json serializable type and error)
  3. Wrap handler with jsonrpc2.Wrap method and register it in server:
    s.Register("multiply", jsonrpc2.Wrap(Multiply))
  4. Use server as common http handler:
    http.ListenAndServe(":8000", s)

Complete example

Full code

package main

import (
	"context"
	"net/http"

	"github.com/neonxp/jsonrpc2"
)

func main() {
	s := jsonrpc2.New()
	s.Register("multiply", jsonrpc2.Wrap(Multiply)) // Register handlers
	s.Register("divide", jsonrpc2.Wrap(Divide))

	http.ListenAndServe(":8000", s)
}

func Multiply(ctx context.Context, args *Args) (int, error) {
    //...
}

func Divide(ctx context.Context, args *Args) (*Quotient, error) {
    //...
}

type Args struct {
	A int `json:"a"`
	B int `json:"b"`
}

type Quotient struct {
	Quo int `json:"quo"`
	Rem int `json:"rem"`
}
Owner
Alex
All info: https://github.com/neonxp/neonxp
Alex
Similar Resources

Server and client implementation of the grpc go libraries to perform unary, client streaming, server streaming and full duplex RPCs from gRPC go introduction

Description This is an implementation of a gRPC client and server that provides route guidance from gRPC Basics: Go tutorial. It demonstrates how to u

Nov 24, 2021

Envoy-eds-server - Envoy EDS server is a working Envoy Discovery Service implementation

envoy-eds-server Intro Envoy EDS server is a working Envoy Discovery Service imp

Apr 2, 2022

Simple, fast and scalable golang rpc library for high load

gorpc Simple, fast and scalable golang RPC library for high load and microservices. Gorpc provides the following features useful for highly loaded pro

Dec 19, 2022

A Light Golang RPC Framework

Glory Glory框架为一款Go语言的轻量级RPC框架,您可以使用它快速开发你的服务实例。如果您希望在微服务场景下使用gRPC进行网络通信,那么Glory会使您的开发、运维工作量减轻不少。 欢迎访问Glory主页: glory-go.github.io 示例仓库:github.com/glory

Oct 28, 2022

Golang `net/rpc` over SSH using installed SSH program

Golang net/rpc over SSH using installed SSH program This package implements a helper functions to launch an RPC client and server. It uses the install

Nov 16, 2022

Golang based RPC client to communicate with Metasploit

gomsf Golang based RPC client to communicate with Metasploit https://docs.rapid7.com/metasploit/rpc-api ⚠️ This is experimental and subject to breakin

Jul 14, 2022

🧙 High-performance PHP-to-Golang IPC/RPC bridge

High-performance PHP-to-Golang IPC bridge Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/r

Dec 28, 2022

go-jsonc provides a way to work with commented json by converting it to plain json.

JSON with comments for GO Decodes a "commented json" to "json". Provided, the input must be a valid jsonc document. Supports io.Reader With this, we c

Apr 6, 2022

A protoc-gen-go wrapper including an RPC stub generator

// Copyright 2013 Google. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE fi

Nov 17, 2022
Comments
  • support requests without params

    support requests without params

    Hi, is there any way to add handlers for methods that don't require params?

    I implemented the following handler:

    r.Register("test", rpc.H(testHandler))
    
    func testHandler(ctx context.Context, args *any) (int, error) {
    	return 0, nil
    }
    

    A request without sending empty parameteres returns an Parse error.

    $ curl -X POST \
            http://localhost:8080 \
            -H 'Content-Type: application/json' \
            -d '{
              "jsonrpc": "2.0",
              "id": "0",
              "method": "test",
      }'
    
     {"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":"0"}
    

    Sending empty args seems to work but that's not very user friendly.

    $ curl -X POST \
            http://localhost:8080 \
            -H 'Content-Type: application/json' \
            -d '{
              "jsonrpc": "2.0",
              "id": "0",
              "method": "test",
              "params": {}
      }'
      
    {"jsonrpc":"2.0","result":0,"id":"0"}
    
  • feat(transport): Add websocket transport;

    feat(transport): Add websocket transport;

    This pull request provides support for the WebSocket transport layer; allowing the bi-directional transfer of messages from client to server using an upgraded HTTP connection. Additionally, the Pr includes support for chaining middlewares together, avoiding the need to repeatedly call WithMiddleware(mw Middleware) Option {}

  • feat: add wrapper supporting no request params

    feat: add wrapper supporting no request params

    This PR adds support for requests without any parameters. Unfortunately I could figure out a way to modify the current handler to do the job without breaking anything so I added a second wrapper to support that use case.

    closes #2

Related tags
Antenna RPC is an RPC protocol for distributed computing, it's based on QUIC and Colfer. its currently an WIP.

aRPC - Antenna Remote Procedure Call Antenna remote procedure call (aRPC) is an RPC protocol focused on distributed processing and HPC. aRPC is implem

Jun 16, 2021
Go Substrate RPC Client (GSRPC)Go Substrate RPC Client (GSRPC)

Go Substrate RPC Client (GSRPC) Substrate RPC client in Go. It provides APIs and types around Polkadot and any Substrate-based chain RPC calls. This c

Nov 11, 2021
Json to rpc example with envoy, go, grpc, nats

grpc-nats-envoy json to rpc example with envoy, go, grpc, redis This repo is a mirror of https://github.com/charlesonunze/grpc-redis-envoy-example It

Dec 7, 2021
It's a typed HTTP server in Go, using generics that is expected to come out with 1.18

typed-http-server-in-go This is a prototype of a typed HTTP server in Go via generics. Generics are scheduled to come out with Go 1.18, and you can pl

Dec 22, 2022
The Go language implementation of gRPC. HTTP/2 based RPC

gRPC-Go The Go implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. For more information

Jan 6, 2023
A simple server which can be used as an RPC endpoint in popular Ethereum wallets.

RPC Endpoint This repository contains code for a simple server which can be used as an RPC endpoint in popular Ethereum wallets. The endpoint is https

Jan 2, 2023
Client-Server App Using RPC to Communicate

rpc-client-server Client-Server App Using RPC to Communicate How to run the application Start the server From the root execute the following command i

Nov 24, 2021
Episode VI: The sentients (RPC autonomously communicating server-client)

APITrials0.6 Episode VI: The sentients (RPC autonomously communicating server-client) Captain's log: As we stray deeper into the machine inner convers

Jan 10, 2022
An experimental package that rely on go generics to implement collection functions utilities

go-underscore go-underscore is a utility-belt library for Golang that provides s

Mar 20, 2022
Netkit - A type parameter(generics) net kit, support tcp kcp, customize packet

Netkit Netkit is a type parameter(generics) golang package Get Started Need Go i

Jan 12, 2022