func HTTP(hf Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
			return
		}
		if hdr := r.Header.Get("Content-Type"); !strings.HasPrefix(hdr, ContentType) {
			http.Error(w, fmt.Sprintf("unknown content type %q", hdr), http.StatusBadRequest)
			return
		}
		req := new(Request)
		err := json.NewDecoder(r.Body).Decode(req)
		if err != nil {
			http.Error(w, "invalid request: "+err.Error(), http.StatusBadRequest)
			return
		}
		req.raddr = r.RemoteAddr
		res := &response{Version: "2.0", ID: req.ID}
		hf.ServeJSONRPC(res, req)
		w.Header().Set("Content-Type", ContentType)
		json.NewEncoder(w).Encode(res)
	})
}
HTTP adapts a JSON-RPC Handler to a HTTP Handler for use in
HTTP(S) exchanges.
package jsonrpc
import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
)
type Handler interface {
	ServeJSONRPC(w ResponseWriter, r *Request)
}
type HandlerFunc func(ResponseWriter, *Request)
func (hf HandlerFunc) ServeJSONRPC(w ResponseWriter, r *Request) {
	hf(w, r)
}
type Request struct {
	Version string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
	ID      json.RawMessage `json:"id"`
	ctx   context.Context
	raddr string
}
func (r *Request) Context() context.Context {
	if r.ctx == nil {
		return context.Background()
	}
	return r.ctx
}
func (r *Request) RemoteAddr() string {
	return r.raddr
}
type ResponseWriter interface {
	Write(interface{}) error
}
type response struct {
	Version string          `json:"jsonrpc"`
	Result  interface{}     `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
	ID      json.RawMessage `json:"id"`
}
func (r *response) Write(res interface{}) error {
	if r.Result != nil && r.Error != nil {
		return fmt.Errorf("response already written")
	}
	if rpcErr, ok := res.(*Error); ok {
		r.Error = rpcErr
	} else if err, ok := res.(error); ok {
		r.Error = InternalError(err.Error(), nil)
	} else {
		r.Result = res
	}
	return nil
}
const ContentType = "application/json"