123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- //go:build v1.1.0
- package main
- import (
- "flag"
- "fmt"
- "io"
- "io/ioutil"
- "git.riomhaire.com/gremlin/jrpcserver/infrastructure/api/rpc"
- "git.riomhaire.com/gremlin/jrpcserver/model"
- "git.riomhaire.com/gremlin/jrpcserver/model/jrpcerror"
- "git.riomhaire.com/gremlin/jrpcserver/usecases/defaultcommand"
- )
- // A simple example 'helloworld' program to show how use the framework
- //
- // to execute (after compiling):
- //
- // helloworld -port=9999 -consul=consul:8500
- func main() {
- name := flag.String("name", "helloworld", "name of service")
- path := flag.String("path", "/api/v1/helloworld", "Path to which to <path>/<command> points to action")
- consulHost := flag.String("consul", "", "consul host usually something like 'localhost:8500'. Leave blank if not required")
- port := flag.Int("port", 9999, "port to use")
- flag.Parse()
- config := model.DefaultConfiguration{
- Server: model.ServerConfig{
- ServiceName: *name,
- BaseURI: *path,
- Port: *port,
- Commands: Commands(),
- Consul: *consulHost,
- },
- }
- rpc.StartAPI(config) // Start service - wont return
- }
- func Commands() []model.JRPCCommand {
- commands := make([]model.JRPCCommand, 0)
- commands = append(commands, model.JRPCCommand{"example.helloworld", HelloWorldCommand, false})
- commands = append(commands, model.JRPCCommand{"system.commands", defaultcommand.ListCommandsCommand, false})
- return commands
- }
- func HelloWorldCommand(config interface{}, metadata map[string]string, payload io.ReadCloser) (interface{}, jrpcerror.JrpcError) {
- data, err := ioutil.ReadAll(payload)
- if err != nil {
- return "", jrpcerror.JrpcError{500, err.Error()}
- } else {
- fmt.Println(string(data))
- response := fmt.Sprintf("Hello %v", string(data))
- return response, jrpcerror.JrpcError{}
- }
- }
|