helloworld.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //go:build v1.1.0
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "jrpcserver/infrastructure/api/rpc"
  9. "jrpcserver/model"
  10. "jrpcserver/model/jrpcerror"
  11. "jrpcserver/usecases/defaultcommand"
  12. )
  13. // A simple example 'helloworld' program to show how use the framework
  14. //
  15. // to execute (after compiling):
  16. //
  17. // helloworld -port=9999 -consul=consul:8500
  18. func main() {
  19. name := flag.String("name", "helloworld", "name of service")
  20. path := flag.String("path", "/api/v1/helloworld", "Path to which to <path>/<command> points to action")
  21. consulHost := flag.String("consul", "", "consul host usually something like 'localhost:8500'. Leave blank if not required")
  22. port := flag.Int("port", 9999, "port to use")
  23. flag.Parse()
  24. config := model.DefaultConfiguration{
  25. Server: model.ServerConfig{
  26. ServiceName: *name,
  27. BaseURI: *path,
  28. Port: *port,
  29. Commands: Commands(),
  30. Consul: *consulHost,
  31. },
  32. }
  33. rpc.StartAPI(config) // Start service - wont return
  34. }
  35. func Commands() []model.JRPCCommand {
  36. commands := make([]model.JRPCCommand, 0)
  37. commands = append(commands, model.JRPCCommand{"example.helloworld", HelloWorldCommand, false})
  38. commands = append(commands, model.JRPCCommand{"system.commands", defaultcommand.ListCommandsCommand, false})
  39. return commands
  40. }
  41. func HelloWorldCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
  42. data, err := ioutil.ReadAll(payload)
  43. if err != nil {
  44. return "", jrpcerror.JrpcError{500, err.Error()}
  45. } else {
  46. fmt.Println(string(data))
  47. response := fmt.Sprintf("Hello %v", string(data))
  48. return response, jrpcerror.JrpcError{}
  49. }
  50. }