✨ Command registration
This commit is contained in:
parent
18ce501089
commit
ff24a43580
1
go.mod
1
go.mod
@ -77,6 +77,7 @@ require (
|
|||||||
github.com/kennygrant/sanitize v1.2.4 // indirect
|
github.com/kennygrant/sanitize v1.2.4 // indirect
|
||||||
github.com/klauspost/compress v1.17.8 // indirect
|
github.com/klauspost/compress v1.17.8 // indirect
|
||||||
github.com/leodido/go-urn v1.2.4 // indirect
|
github.com/leodido/go-urn v1.2.4 // indirect
|
||||||
|
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 // indirect
|
||||||
github.com/magiconair/properties v1.8.7 // indirect
|
github.com/magiconair/properties v1.8.7 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -233,6 +233,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||||
|
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 h1:UtyD+eBVdLYSj5/pjfSR6mtnzMgIiOVcFT024G2l4CY=
|
||||||
|
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42/go.mod h1:/peI0OaxVYh7fzA72CD7rUsyGVdF7sCiFw7GcYqOcCw=
|
||||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package directory
|
package directory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,17 +12,17 @@ import (
|
|||||||
var commandDirectory = make(map[string]*Command)
|
var commandDirectory = make(map[string]*Command)
|
||||||
var commandDirectoryMutex sync.Mutex
|
var commandDirectoryMutex sync.Mutex
|
||||||
|
|
||||||
func GetCommandKey(id, method string) string {
|
|
||||||
return id + ":" + method
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddCommand(id, method string, tags []string, handler *ServiceInstance) {
|
func AddCommand(id, method string, tags []string, handler *ServiceInstance) {
|
||||||
commandDirectoryMutex.Lock()
|
commandDirectoryMutex.Lock()
|
||||||
defer commandDirectoryMutex.Unlock()
|
defer commandDirectoryMutex.Unlock()
|
||||||
|
|
||||||
ky := GetCommandKey(id, method)
|
if tags == nil {
|
||||||
if _, ok := commandDirectory[id]; !ok {
|
tags = make([]string, 0)
|
||||||
commandDirectory[id] = &Command{
|
}
|
||||||
|
|
||||||
|
ky := nex.GetCommandKey(id, method)
|
||||||
|
if _, ok := commandDirectory[ky]; !ok {
|
||||||
|
commandDirectory[ky] = &Command{
|
||||||
ID: id,
|
ID: id,
|
||||||
Method: method,
|
Method: method,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
@ -33,13 +36,15 @@ func AddCommand(id, method string, tags []string, handler *ServiceInstance) {
|
|||||||
commandDirectory[ky].Handler = lo.UniqBy(commandDirectory[ky].Handler, func(item *ServiceInstance) string {
|
commandDirectory[ky].Handler = lo.UniqBy(commandDirectory[ky].Handler, func(item *ServiceInstance) string {
|
||||||
return item.ID
|
return item.ID
|
||||||
})
|
})
|
||||||
|
|
||||||
|
log.Info().Str("id", id).Str("method", method).Str("tags", strings.Join(tags, ",")).Msg("New command registered")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCommandHandler(id, method string) *ServiceInstance {
|
func GetCommandHandler(id, method string) *ServiceInstance {
|
||||||
commandDirectoryMutex.Lock()
|
commandDirectoryMutex.Lock()
|
||||||
defer commandDirectoryMutex.Unlock()
|
defer commandDirectoryMutex.Unlock()
|
||||||
|
|
||||||
ky := GetCommandKey(id, method)
|
ky := nex.GetCommandKey(id, method)
|
||||||
if val, ok := commandDirectory[ky]; ok {
|
if val, ok := commandDirectory[ky]; ok {
|
||||||
if len(val.Handler) == 0 {
|
if len(val.Handler) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@ -57,6 +62,6 @@ func RemoveCommand(id, method string) {
|
|||||||
commandDirectoryMutex.Lock()
|
commandDirectoryMutex.Lock()
|
||||||
defer commandDirectoryMutex.Unlock()
|
defer commandDirectoryMutex.Unlock()
|
||||||
|
|
||||||
ky := GetCommandKey(id, method)
|
ky := nex.GetCommandKey(id, method)
|
||||||
delete(commandDirectory, ky)
|
delete(commandDirectory, ky)
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ func (c CommandRpcServer) AddCommand(ctx context.Context, info *proto.CommandInf
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
service := GetServiceInstanceByType(clientId)
|
service := GetServiceInstance(clientId)
|
||||||
if service == nil {
|
if service == nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "service not found")
|
return nil, status.Errorf(codes.NotFound, "service not found")
|
||||||
}
|
}
|
||||||
@ -44,18 +45,35 @@ func (c CommandRpcServer) SendCommand(ctx context.Context, argument *proto.Comma
|
|||||||
|
|
||||||
handler := GetCommandHandler(id, method)
|
handler := GetCommandHandler(id, method)
|
||||||
if handler == nil {
|
if handler == nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "command not found")
|
return &proto.CommandReturn{
|
||||||
|
IsDelivered: false,
|
||||||
|
Status: http.StatusNotFound,
|
||||||
|
Payload: []byte("command not found"),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := handler.GetGrpcConn()
|
conn, err := handler.GetGrpcConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Unavailable, "service unavailable")
|
return &proto.CommandReturn{
|
||||||
|
IsDelivered: false,
|
||||||
|
Status: http.StatusServiceUnavailable,
|
||||||
|
Payload: []byte("service unavailable"),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
contx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return proto.NewCommandControllerClient(conn).SendCommand(contx, argument)
|
out, err := proto.NewCommandControllerClient(conn).SendCommand(ctx, argument)
|
||||||
|
if err != nil {
|
||||||
|
return &proto.CommandReturn{
|
||||||
|
IsDelivered: true,
|
||||||
|
Status: http.StatusInternalServerError,
|
||||||
|
Payload: []byte(err.Error()),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
out.IsDelivered = true
|
||||||
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CommandRpcServer) SendStreamCommand(g grpc.BidiStreamingServer[proto.CommandArgument, proto.CommandReturn]) error {
|
func (c CommandRpcServer) SendStreamCommand(g grpc.BidiStreamingServer[proto.CommandArgument, proto.CommandReturn]) error {
|
||||||
@ -77,13 +95,21 @@ func (c CommandRpcServer) SendStreamCommand(g grpc.BidiStreamingServer[proto.Com
|
|||||||
|
|
||||||
conn, err := handler.GetGrpcConn()
|
conn, err := handler.GetGrpcConn()
|
||||||
|
|
||||||
contx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
ctx, cancel := context.WithTimeout(g.Context(), time.Second*10)
|
||||||
result, _ := proto.NewCommandControllerClient(conn).SendCommand(contx, pck)
|
result, err := proto.NewCommandControllerClient(conn).SendCommand(ctx, pck)
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
_ = g.Send(&proto.CommandReturn{
|
||||||
|
IsDelivered: false,
|
||||||
|
Status: http.StatusInternalServerError,
|
||||||
|
Payload: []byte(err.Error()),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
_ = g.Send(&proto.CommandReturn{
|
_ = g.Send(&proto.CommandReturn{
|
||||||
Status: result.Status,
|
Status: result.Status,
|
||||||
Payload: result.Payload,
|
Payload: result.Payload,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func (v *ServiceRpcServer) AddService(ctx context.Context, info *proto.ServiceIn
|
|||||||
HttpAddr: info.HttpAddr,
|
HttpAddr: info.HttpAddr,
|
||||||
}
|
}
|
||||||
AddServiceInstance(in)
|
AddServiceInstance(in)
|
||||||
log.Info().Str("id", clientId).Str("label", info.GetLabel()).Msg("New service added.")
|
log.Info().Str("id", clientId).Str("label", info.GetLabel()).Msg("New service registered")
|
||||||
return &proto.AddServiceResponse{
|
return &proto.AddServiceResponse{
|
||||||
IsSuccess: true,
|
IsSuccess: true,
|
||||||
}, nil
|
}, nil
|
||||||
|
16
pkg/main.go
16
pkg/main.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fatih/color"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -21,6 +23,16 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Booting screen
|
||||||
|
fmt.Println(color.YellowString(` _ _
|
||||||
|
| \ | | _____ ___ _ ___
|
||||||
|
| \| |/ _ \ \/ / | | / __|
|
||||||
|
| |\ | __/> <| |_| \__ \
|
||||||
|
|_| \_|\___/_/\_\\__,_|___/`))
|
||||||
|
fmt.Printf("%s v%s\n", color.New(color.FgHiYellow).Add(color.Bold).Sprintf("Hypernet.Nexus"), pkg.AppVersion)
|
||||||
|
fmt.Printf("The core component of Hypernet (Solar Network)\n")
|
||||||
|
color.HiBlack("=====================================================\n")
|
||||||
|
|
||||||
// Configure settings
|
// Configure settings
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
viper.AddConfigPath("..")
|
viper.AddConfigPath("..")
|
||||||
@ -43,13 +55,9 @@ func main() {
|
|||||||
quartz.Start()
|
quartz.Start()
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
log.Info().Msgf("Nexus v%s is started...", pkg.AppVersion)
|
|
||||||
|
|
||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-quit
|
<-quit
|
||||||
|
|
||||||
log.Info().Msgf("Passport v%s is quitting...", pkg.AppVersion)
|
|
||||||
|
|
||||||
quartz.Stop()
|
quartz.Stop()
|
||||||
}
|
}
|
||||||
|
104
pkg/nex/command.go
Normal file
104
pkg/nex/command.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package nex
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
health "google.golang.org/grpc/health/grpc_health_v1"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/reflection"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommandHandler func(ctx *CommandCtx) error
|
||||||
|
|
||||||
|
func GetCommandKey(id, method string) string {
|
||||||
|
return id + ":" + method
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Conn) AddCommand(id, method string, tags []string, fn CommandHandler) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type localCommandRpcServer struct {
|
||||||
|
conn *Conn
|
||||||
|
|
||||||
|
proto.UnimplementedCommandControllerServer
|
||||||
|
health.UnimplementedHealthServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v localCommandRpcServer) SendCommand(ctx context.Context, argument *proto.CommandArgument) (*proto.CommandReturn, error) {
|
||||||
|
if handler, ok := v.conn.commandHandlers[argument.GetCommand()]; !ok {
|
||||||
|
return &proto.CommandReturn{
|
||||||
|
Status: http.StatusNotFound,
|
||||||
|
Payload: []byte(argument.GetCommand() + " not found"),
|
||||||
|
}, nil
|
||||||
|
} else {
|
||||||
|
cc := &CommandCtx{
|
||||||
|
requestBody: argument.GetPayload(),
|
||||||
|
statusCode: http.StatusOK,
|
||||||
|
}
|
||||||
|
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||||
|
for k, v := range md {
|
||||||
|
cc.values.Store(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := handler(cc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return &proto.CommandReturn{
|
||||||
|
Status: int32(cc.statusCode),
|
||||||
|
Payload: cc.responseBody,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v localCommandRpcServer) Check(ctx context.Context, request *health.HealthCheckRequest) (*health.HealthCheckResponse, error) {
|
||||||
|
return &health.HealthCheckResponse{
|
||||||
|
Status: health.HealthCheckResponse_SERVING,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v localCommandRpcServer) Watch(request *health.HealthCheckRequest, server health.Health_WatchServer) error {
|
||||||
|
for {
|
||||||
|
if server.Send(&health.HealthCheckResponse{
|
||||||
|
Status: health.HealthCheckResponse_SERVING,
|
||||||
|
}) != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Conn) RunCommands(addr string) error {
|
||||||
|
v.commandServer = grpc.NewServer()
|
||||||
|
service := &localCommandRpcServer{conn: v}
|
||||||
|
proto.RegisterCommandControllerServer(v.commandServer, service)
|
||||||
|
health.RegisterHealthServer(v.commandServer, service)
|
||||||
|
reflection.Register(v.commandServer)
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.commandServer.Serve(listener)
|
||||||
|
}
|
47
pkg/nex/command_context.go
Normal file
47
pkg/nex/command_context.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package nex
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommandCtx struct {
|
||||||
|
requestBody []byte
|
||||||
|
responseBody []byte
|
||||||
|
|
||||||
|
statusCode int
|
||||||
|
|
||||||
|
values sync.Map
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandCtx) Value(key string, newValue ...any) any {
|
||||||
|
if len(newValue) > 0 {
|
||||||
|
c.values.Store(key, newValue[0])
|
||||||
|
}
|
||||||
|
val, _ := c.values.Load(key)
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandCtx) Read() []byte {
|
||||||
|
return c.requestBody
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandCtx) ReadJSON(out any) error {
|
||||||
|
return json.Unmarshal(c.requestBody, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandCtx) Write(data []byte, statusCode ...int) error {
|
||||||
|
c.responseBody = data
|
||||||
|
if len(statusCode) > 0 {
|
||||||
|
c.statusCode = statusCode[0]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandCtx) JSON(data any, statusCode ...int) error {
|
||||||
|
raw, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Write(raw, statusCode...)
|
||||||
|
}
|
46
pkg/nex/command_test.go
Normal file
46
pkg/nex/command_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package nex_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleCommand(t *testing.T) {
|
||||||
|
conn, err := nex.NewNexusConn("127.0.0.1:7001", &proto.ServiceInfo{
|
||||||
|
Id: "echo01",
|
||||||
|
Type: "echo",
|
||||||
|
Label: "Echo",
|
||||||
|
GrpcAddr: "127.0.0.1:6001",
|
||||||
|
HttpAddr: nil,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(fmt.Errorf("unable to connect nexus: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := conn.RegisterService(); err != nil {
|
||||||
|
t.Fatal(fmt.Errorf("unable to register service: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.AddCommand("echo", "get", nil, func(ctx *nex.CommandCtx) error {
|
||||||
|
return ctx.Write(ctx.Read(), http.StatusOK)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(fmt.Errorf("unable to add command: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err := conn.RunCommands("127.0.0.1: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)
|
||||||
|
}
|
@ -2,6 +2,7 @@ package nex
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||||
@ -12,15 +13,18 @@ import (
|
|||||||
_ "github.com/mbobakov/grpc-consul-resolver"
|
_ "github.com/mbobakov/grpc-consul-resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HyperConn struct {
|
type Conn struct {
|
||||||
Addr string
|
Addr string
|
||||||
Info *proto.ServiceInfo
|
Info *proto.ServiceInfo
|
||||||
|
|
||||||
dealerConn *grpc.ClientConn
|
commandServer *grpc.Server
|
||||||
cacheGrpcConn map[string]*grpc.ClientConn
|
commandHandlers map[string]CommandHandler
|
||||||
|
|
||||||
|
nexusConn *grpc.ClientConn
|
||||||
|
clientConn map[string]*grpc.ClientConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHyperConn(addr string, info *proto.ServiceInfo) (*HyperConn, error) {
|
func NewNexusConn(addr string, info *proto.ServiceInfo) (*Conn, error) {
|
||||||
conn, err := grpc.NewClient(
|
conn, err := grpc.NewClient(
|
||||||
addr,
|
addr,
|
||||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||||
@ -29,22 +33,26 @@ func NewHyperConn(addr string, info *proto.ServiceInfo) (*HyperConn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HyperConn{
|
return &Conn{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
Info: info,
|
Info: info,
|
||||||
|
|
||||||
dealerConn: conn,
|
commandHandlers: make(map[string]CommandHandler),
|
||||||
cacheGrpcConn: make(map[string]*grpc.ClientConn),
|
|
||||||
|
nexusConn: conn,
|
||||||
|
clientConn: make(map[string]*grpc.ClientConn),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *HyperConn) RegisterService() error {
|
func (v *Conn) RegisterService() error {
|
||||||
dir := proto.NewServiceDirectoryClient(v.dealerConn)
|
dir := proto.NewServiceDirectoryClient(v.nexusConn)
|
||||||
_, err := dir.AddService(context.Background(), v.Info)
|
ctx := context.Background()
|
||||||
|
ctx = metadata.AppendToOutgoingContext(ctx, "client_id", v.Info.Id)
|
||||||
|
_, err := dir.AddService(ctx, v.Info)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *HyperConn) KeepRegisterService() error {
|
func (v *Conn) RunRegistering() error {
|
||||||
err := v.RegisterService()
|
err := v.RegisterService()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -52,9 +60,9 @@ func (v *HyperConn) KeepRegisterService() error {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
client := health.NewHealthClient(v.dealerConn)
|
client := health.NewHealthClient(v.nexusConn)
|
||||||
if _, err := client.Check(context.Background(), &health.HealthCheckRequest{}); err != nil {
|
if _, err := client.Check(context.Background(), &health.HealthCheckRequest{}); err != nil {
|
||||||
if v.KeepRegisterService() == nil {
|
if v.RunRegistering() == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,12 +71,12 @@ func (v *HyperConn) KeepRegisterService() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *HyperConn) GetNexusGrpcConn() *grpc.ClientConn {
|
func (v *Conn) GetNexusGrpcConn() *grpc.ClientConn {
|
||||||
return v.dealerConn
|
return v.nexusConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *HyperConn) GetServiceGrpcConn(t string) (*grpc.ClientConn, error) {
|
func (v *Conn) GetClientGrpcConn(t string) (*grpc.ClientConn, error) {
|
||||||
if val, ok := v.cacheGrpcConn[t]; ok {
|
if val, ok := v.clientConn[t]; ok {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if _, err := health.NewHealthClient(val).Check(ctx, &health.HealthCheckRequest{
|
if _, err := health.NewHealthClient(val).Check(ctx, &health.HealthCheckRequest{
|
||||||
@ -76,14 +84,14 @@ func (v *HyperConn) GetServiceGrpcConn(t string) (*grpc.ClientConn, error) {
|
|||||||
}); err == nil {
|
}); err == nil {
|
||||||
return val, nil
|
return val, nil
|
||||||
} else {
|
} else {
|
||||||
delete(v.cacheGrpcConn, t)
|
delete(v.clientConn, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
out, err := proto.NewServiceDirectoryClient(v.dealerConn).GetService(ctx, &proto.GetServiceRequest{
|
out, err := proto.NewServiceDirectoryClient(v.nexusConn).GetService(ctx, &proto.GetServiceRequest{
|
||||||
Type: &t,
|
Type: &t,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -95,7 +103,7 @@ func (v *HyperConn) GetServiceGrpcConn(t string) (*grpc.ClientConn, error) {
|
|||||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||||
)
|
)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
v.cacheGrpcConn[t] = conn
|
v.clientConn[t] = conn
|
||||||
}
|
}
|
||||||
return conn, err
|
return conn, err
|
||||||
}
|
}
|
||||||
|
@ -290,8 +290,9 @@ type CommandReturn struct {
|
|||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
Status int32 `protobuf:"varint,1,opt,name=status,proto3" json:"status,omitempty"`
|
IsDelivered bool `protobuf:"varint,1,opt,name=is_delivered,json=isDelivered,proto3" json:"is_delivered,omitempty"`
|
||||||
Payload []byte `protobuf:"bytes,2,opt,name=payload,proto3,oneof" json:"payload,omitempty"`
|
Status int32 `protobuf:"varint,2,opt,name=status,proto3" json:"status,omitempty"`
|
||||||
|
Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3,oneof" json:"payload,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *CommandReturn) Reset() {
|
func (x *CommandReturn) Reset() {
|
||||||
@ -324,6 +325,13 @@ func (*CommandReturn) Descriptor() ([]byte, []int) {
|
|||||||
return file_command_proto_rawDescGZIP(), []int{5}
|
return file_command_proto_rawDescGZIP(), []int{5}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *CommandReturn) GetIsDelivered() bool {
|
||||||
|
if x != nil {
|
||||||
|
return x.IsDelivered
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (x *CommandReturn) GetStatus() int32 {
|
func (x *CommandReturn) GetStatus() int32 {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Status
|
return x.Status
|
||||||
@ -364,32 +372,35 @@ var file_command_proto_rawDesc = []byte{
|
|||||||
0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74,
|
0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74,
|
||||||
0x68, 0x6f, 0x64, 0x12, 0x1d, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x03,
|
0x68, 0x6f, 0x64, 0x12, 0x1d, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x03,
|
||||||
0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x88,
|
0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x88,
|
||||||
0x01, 0x01, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x52,
|
0x01, 0x01, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x75,
|
||||||
0x0a, 0x0d, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x12,
|
0x0a, 0x0d, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x12,
|
||||||
0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52,
|
0x21, 0x0a, 0x0c, 0x69, 0x73, 0x5f, 0x64, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x65, 0x64, 0x18,
|
||||||
0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1d, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f,
|
0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72,
|
||||||
0x61, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c,
|
0x65, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01,
|
||||||
0x6f, 0x61, 0x64, 0x88, 0x01, 0x01, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f,
|
0x28, 0x05, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1d, 0x0a, 0x07, 0x70, 0x61,
|
||||||
0x61, 0x64, 0x32, 0xa8, 0x02, 0x0a, 0x11, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x43, 0x6f,
|
0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x07, 0x70,
|
||||||
0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x12, 0x3d, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x43,
|
0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x88, 0x01, 0x01, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x70, 0x61,
|
||||||
0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43,
|
0x79, 0x6c, 0x6f, 0x61, 0x64, 0x32, 0xa8, 0x02, 0x0a, 0x11, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e,
|
||||||
0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x49, 0x6e, 0x66, 0x6f, 0x1a, 0x19, 0x2e, 0x70, 0x72, 0x6f,
|
0x64, 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x12, 0x3d, 0x0a, 0x0a, 0x41,
|
||||||
0x74, 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73,
|
0x64, 0x64, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||||
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4c, 0x0a, 0x0d, 0x52, 0x65, 0x6d, 0x6f, 0x76,
|
0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x49, 0x6e, 0x66, 0x6f, 0x1a, 0x19, 0x2e,
|
||||||
0x65, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x1b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
|
||||||
0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x52, 0x65,
|
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4c, 0x0a, 0x0d, 0x52, 0x65,
|
||||||
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65,
|
0x6d, 0x6f, 0x76, 0x65, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x1b, 0x2e, 0x70, 0x72,
|
||||||
0x6d, 0x6f, 0x76, 0x65, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f,
|
0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x4c, 0x6f, 0x6f, 0x6b, 0x75,
|
||||||
0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x6f, 0x6d,
|
0x70, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||||
0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d,
|
0x2e, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65,
|
||||||
0x6d, 0x61, 0x6e, 0x64, 0x41, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x1a, 0x14, 0x2e, 0x70,
|
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64,
|
||||||
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x74, 0x75,
|
0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
|
||||||
0x72, 0x6e, 0x22, 0x00, 0x12, 0x47, 0x0a, 0x11, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x72, 0x65,
|
0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x41, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x1a,
|
||||||
0x61, 0x6d, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52,
|
||||||
0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x41, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e,
|
0x65, 0x74, 0x75, 0x72, 0x6e, 0x22, 0x00, 0x12, 0x47, 0x0a, 0x11, 0x53, 0x65, 0x6e, 0x64, 0x53,
|
||||||
0x74, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e,
|
0x74, 0x72, 0x65, 0x61, 0x6d, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x2e, 0x70,
|
||||||
0x64, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x09, 0x5a,
|
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x41, 0x72, 0x67, 0x75,
|
||||||
0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
0x6d, 0x65, 0x6e, 0x74, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6d,
|
||||||
|
0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01,
|
||||||
|
0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f,
|
||||||
|
0x74, 0x6f, 0x33,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -37,6 +37,7 @@ message CommandArgument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message CommandReturn {
|
message CommandReturn {
|
||||||
int32 status = 1;
|
bool is_delivered = 1;
|
||||||
optional bytes payload = 2;
|
int32 status = 2;
|
||||||
|
optional bytes payload = 3;
|
||||||
}
|
}
|
@ -1,24 +1,8 @@
|
|||||||
id = "passport01"
|
bind = "0.0.0.0:8001"
|
||||||
name = "Solarpass"
|
grpc_bind = "0.0.0.0:7001"
|
||||||
|
|
||||||
bind = "0.0.0.0:8442"
|
|
||||||
grpc_bind = "0.0.0.0:7442"
|
|
||||||
domain = "localhost"
|
domain = "localhost"
|
||||||
secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
|
secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
|
||||||
|
|
||||||
content_endpoint = "https://usercontent.solsynth.dev"
|
|
||||||
|
|
||||||
apns_topic = "dev.solsynth.solian.Runner"
|
|
||||||
apns_credentials = ""
|
|
||||||
apns_credentials_team = "000000000"
|
|
||||||
apns_credentials_key = "000000000"
|
|
||||||
firebase_credentials = ""
|
|
||||||
|
|
||||||
use_registration_magic_token = false
|
|
||||||
|
|
||||||
[dealer]
|
|
||||||
addr = "127.0.0.1:8442"
|
|
||||||
|
|
||||||
[debug]
|
[debug]
|
||||||
database = false
|
database = false
|
||||||
print_routes = false
|
print_routes = false
|
||||||
|
Loading…
Reference in New Issue
Block a user