You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. stdlog "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "github.com/tendermint/tendermint/libs/log"
  11. rpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  12. )
  13. var routes = map[string]*rpcserver.RPCFunc{
  14. "hello_world": rpcserver.NewRPCFunc(HelloWorld, "name", "num"),
  15. }
  16. func HelloWorld(ctx context.Context, name string, num int) (Result, error) {
  17. return Result{fmt.Sprintf("hi %s %d", name, num)}, nil
  18. }
  19. type Result struct {
  20. Result string
  21. }
  22. func main() {
  23. mux := http.NewServeMux()
  24. logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
  25. if err != nil {
  26. stdlog.Fatalf("configuring logger: %v", err)
  27. }
  28. ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
  29. defer cancel()
  30. rpcserver.RegisterRPCFuncs(mux, routes, logger)
  31. config := rpcserver.DefaultConfig()
  32. listener, err := rpcserver.Listen("tcp://127.0.0.1:8008", config.MaxOpenConnections)
  33. if err != nil {
  34. stdlog.Fatalf("rpc listening: %v", err)
  35. }
  36. if err = rpcserver.Serve(ctx, listener, mux, logger, config); err != nil {
  37. logger.Error("rpc serve: %v", err)
  38. }
  39. }