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.

48 lines
1.1 KiB

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