aboutsummaryrefslogtreecommitdiff
path: root/examples/http/main.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 02:31:43 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 02:31:43 +0300
commit440f3f4604f3489113f6705b8da67d839e52360e (patch)
tree9bc04f896cc79cbb4bb41adbe19aaaaa0592acba /examples/http/main.go
initial
Diffstat (limited to 'examples/http/main.go')
-rw-r--r--examples/http/main.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/http/main.go b/examples/http/main.go
new file mode 100644
index 0000000..fdef509
--- /dev/null
+++ b/examples/http/main.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "context"
+ "errors"
+ "net/http"
+
+ "github.com/neonxp/rpc"
+)
+
+func main() {
+ s := jsonrpc2.New()
+ s.Register("multiply", jsonrpc2.Wrap(Multiply))
+ s.Register("divide", jsonrpc2.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"`
+}