commands.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package defaultcommand
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "jrpcserver/model"
  8. "jrpcserver/model/jrpcerror"
  9. )
  10. func PingCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  11. return "pong", jrpcerror.JrpcError{}
  12. }
  13. func PongCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  14. return "ping", jrpcerror.JrpcError{}
  15. }
  16. func EchoCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  17. for name, value := range metadata {
  18. fmt.Printf("%s = %s\n", name, value)
  19. }
  20. data, _ := ioutil.ReadAll(payload)
  21. msg := make(map[string]interface{})
  22. json.Unmarshal(data, &msg)
  23. return msg, jrpcerror.JrpcError{}
  24. }
  25. func ListCommandsCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  26. names := make([]string, 0)
  27. serverConfigAccessor, ok := config.(model.ServerConfigReader)
  28. if !ok {
  29. return "unknown", jrpcerror.JrpcError{}
  30. }
  31. server, _ := serverConfigAccessor.ReadServerConfig()
  32. for _, cmd := range server.Commands {
  33. names = append(names, cmd.Name)
  34. }
  35. return names, jrpcerror.JrpcError{}
  36. }
  37. func VersionCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  38. serverConfigAccessor, ok := config.(model.ServerConfigReader)
  39. if !ok {
  40. return "unknown", jrpcerror.JrpcError{}
  41. }
  42. server, _ := serverConfigAccessor.ReadServerConfig()
  43. return server.Version, jrpcerror.JrpcError{}
  44. }
  45. func InfoCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  46. return config, jrpcerror.JrpcError{}
  47. }
  48. func HealthCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  49. return "UP", jrpcerror.JrpcError{}
  50. }