2022-01-31 02:31:43 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
httpRPC "github.com/neonxp/jsonrpc2/http"
|
|
|
|
"github.com/neonxp/jsonrpc2/rpc"
|
2022-01-31 02:31:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-01-31 20:17:31 +03:00
|
|
|
s := httpRPC.New()
|
|
|
|
|
|
|
|
s.Register("multiply", rpc.Wrap(Multiply))
|
|
|
|
s.Register("divide", rpc.Wrap(Divide))
|
2022-01-31 02:31:43 +03:00
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|