helloworld.go 1.7 KB

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