✨ Basic http gateway
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"google.golang.org/grpc/reflection"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -19,20 +20,33 @@ func GetCommandKey(id, method string) string {
|
||||
}
|
||||
|
||||
func (v *Conn) AddCommand(id, method string, tags []string, fn CommandHandler) error {
|
||||
method = strings.ToLower(method)
|
||||
dir := proto.NewCommandControllerClient(v.nexusConn)
|
||||
ctx := context.Background()
|
||||
ctx = metadata.AppendToOutgoingContext(ctx, "client_id", v.Info.Id)
|
||||
_, err := dir.AddCommand(ctx, &proto.CommandInfo{
|
||||
Id: id,
|
||||
Method: method,
|
||||
Tags: tags,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
v.commandHandlers[GetCommandKey(id, method)] = fn
|
||||
var addingMethodQueue []string
|
||||
if method == "all" {
|
||||
addingMethodQueue = []string{"get", "post", "put", "patch", "delete"}
|
||||
} else {
|
||||
addingMethodQueue = append(addingMethodQueue, method)
|
||||
}
|
||||
|
||||
return err
|
||||
for _, method := range addingMethodQueue {
|
||||
ky := GetCommandKey(id, method)
|
||||
_, err := dir.AddCommand(ctx, &proto.CommandInfo{
|
||||
Id: id,
|
||||
Method: method,
|
||||
Tags: tags,
|
||||
})
|
||||
if err == nil {
|
||||
v.commandHandlers[ky] = fn
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type localCommandRpcServer struct {
|
||||
@ -43,7 +57,8 @@ type localCommandRpcServer struct {
|
||||
}
|
||||
|
||||
func (v localCommandRpcServer) SendCommand(ctx context.Context, argument *proto.CommandArgument) (*proto.CommandReturn, error) {
|
||||
if handler, ok := v.conn.commandHandlers[argument.GetCommand()]; !ok {
|
||||
ky := GetCommandKey(argument.GetCommand(), argument.GetMethod())
|
||||
if handler, ok := v.conn.commandHandlers[ky]; !ok {
|
||||
return &proto.CommandReturn{
|
||||
Status: http.StatusNotFound,
|
||||
Payload: []byte(argument.GetCommand() + " not found"),
|
||||
|
@ -25,7 +25,15 @@ func TestHandleCommand(t *testing.T) {
|
||||
t.Fatal(fmt.Errorf("unable to register service: %v", err))
|
||||
}
|
||||
|
||||
err = conn.AddCommand("echo", "get", nil, func(ctx *nex.CommandCtx) error {
|
||||
err = conn.AddCommand("say.hi", "all", nil, func(ctx *nex.CommandCtx) error {
|
||||
return ctx.Write([]byte("Hello, World!"), http.StatusOK)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(fmt.Errorf("unable to add command: %v", err))
|
||||
return
|
||||
}
|
||||
err = conn.AddCommand("echo", "all", nil, func(ctx *nex.CommandCtx) error {
|
||||
t.Log("Received command: ", string(ctx.Read()))
|
||||
return ctx.Write(ctx.Read(), http.StatusOK)
|
||||
})
|
||||
if err != nil {
|
||||
@ -34,13 +42,13 @@ func TestHandleCommand(t *testing.T) {
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := conn.RunCommands("127.0.0.1:6001")
|
||||
err := conn.RunCommands("0.0.0.0:6001")
|
||||
if err != nil {
|
||||
t.Error(fmt.Errorf("unable to run commands: %v", err))
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
t.Log("Waiting 10 seconds for calling command...")
|
||||
time.Sleep(time.Second * 10)
|
||||
t.Log("Waiting 60 seconds for calling command...")
|
||||
time.Sleep(time.Second * 60)
|
||||
}
|
||||
|
Reference in New Issue
Block a user