n0cte / jsonrpc

JSON-RPC 2.0 server and client pure implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jsonrpc

JSON-RPC 2.0 server and client pure implementation

Server usage

package main

import (
  "net/http"

  "github.com/n0cte/jsonrpc"
)

type MathParams struct {
  A int64
  B int64
}

func main() {
  srv := jsonrpc.NewServer(map[string]jsonrpc.Handler{
    "sum": jsonrpc.TypedHandlerFunc(func(params MathParams) (int64, error) {
      return params.A + params.B, nil
    }),
  })
  srv.HandleFunc("mul", jsonrpc.TypedHandlerFunc(func(params MathParams) (int64, error) {
    return params.A * params.B, nil
  }))
  http.ListenAndServe(":8080", srv)
}

Client usage

package main

import (
  "net/http"

  "github.com/n0cte/jsonrpc"
)

type MathParams struct {
  A int64
  B int64
}

func main() {
  client := jsonrpc.NewClient("127.0.0.1:8080", *http.DefaultClient)
  jsonrpc.TypedCall[MathParams, int64](client, "sum", MathParams{A: 0, B: 0})
}

About

JSON-RPC 2.0 server and client pure implementation

License:Apache License 2.0


Languages

Language:Go 100.0%