aboutsummaryrefslogblamecommitdiff
path: root/examples/http/main.go
blob: 5783c05bd5f07a8d17d40e5b7a0e1197d93bfd25 (plain) (tree)
1
2
3
4
5
6
7
8
9






                  

                                                 


             



                                                  


























                                                                 
package main

import (
	"context"
	"errors"
	"net/http"

	httpRPC "github.com/neonxp/jsonrpc2/http"
	"github.com/neonxp/jsonrpc2/rpc"
)

func main() {
	s := httpRPC.New()

	s.Register("multiply", rpc.Wrap(Multiply))
	s.Register("divide", rpc.Wrap(Divide))

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

func Multiply(ctx context.Context, args *Args) (int, error) {
	return args.A * args.B, nil
}

func Divide(ctx context.Context, args *Args) (*Quotient, error) {
	if args.B == 0 {
		return nil, errors.New("divide by zero")
	}
	quo := new(Quotient)
	quo.Quo = args.A / args.B
	quo.Rem = args.A % args.B
	return quo, nil
}

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

type Quotient struct {
	Quo int `json:"quo"`
	Rem int `json:"rem"`
}