🎉 Initial Commit
This commit is contained in:
101
pkg/nex/conn.go
Normal file
101
pkg/nex/conn.go
Normal file
@ -0,0 +1,101 @@
|
||||
package nex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
health "google.golang.org/grpc/health/grpc_health_v1"
|
||||
|
||||
_ "github.com/mbobakov/grpc-consul-resolver"
|
||||
)
|
||||
|
||||
type HyperConn struct {
|
||||
Addr string
|
||||
Info *proto.ServiceInfo
|
||||
|
||||
dealerConn *grpc.ClientConn
|
||||
cacheGrpcConn map[string]*grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewHyperConn(addr string, info *proto.ServiceInfo) (*HyperConn, error) {
|
||||
conn, err := grpc.NewClient(
|
||||
addr,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &HyperConn{
|
||||
Addr: addr,
|
||||
Info: info,
|
||||
|
||||
dealerConn: conn,
|
||||
cacheGrpcConn: make(map[string]*grpc.ClientConn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *HyperConn) RegisterService() error {
|
||||
dir := proto.NewServiceDirectoryClient(v.dealerConn)
|
||||
_, err := dir.AddService(context.Background(), v.Info)
|
||||
return err
|
||||
}
|
||||
|
||||
func (v *HyperConn) KeepRegisterService() error {
|
||||
err := v.RegisterService()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
time.Sleep(5 * time.Second)
|
||||
client := health.NewHealthClient(v.dealerConn)
|
||||
if _, err := client.Check(context.Background(), &health.HealthCheckRequest{}); err != nil {
|
||||
if v.KeepRegisterService() == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *HyperConn) GetNexusGrpcConn() *grpc.ClientConn {
|
||||
return v.dealerConn
|
||||
}
|
||||
|
||||
func (v *HyperConn) GetServiceGrpcConn(t string) (*grpc.ClientConn, error) {
|
||||
if val, ok := v.cacheGrpcConn[t]; ok {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
if _, err := health.NewHealthClient(val).Check(ctx, &health.HealthCheckRequest{
|
||||
Service: t,
|
||||
}); err == nil {
|
||||
return val, nil
|
||||
} else {
|
||||
delete(v.cacheGrpcConn, t)
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
out, err := proto.NewServiceDirectoryClient(v.dealerConn).GetService(ctx, &proto.GetServiceRequest{
|
||||
Type: &t,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn, err := grpc.NewClient(
|
||||
out.GetData().GetGrpcAddr(),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err == nil {
|
||||
v.cacheGrpcConn[t] = conn
|
||||
}
|
||||
return conn, err
|
||||
}
|
8
pkg/nex/const.go
Normal file
8
pkg/nex/const.go
Normal file
@ -0,0 +1,8 @@
|
||||
package nex
|
||||
|
||||
const (
|
||||
ServiceTypeAuthProvider = "auth"
|
||||
ServiceTypeFileProvider = "files"
|
||||
ServiceTypeInteractiveProvider = "interactive"
|
||||
ServiceTypeMessagingProvider = "messaging"
|
||||
)
|
14
pkg/nex/encode.go
Normal file
14
pkg/nex/encode.go
Normal file
@ -0,0 +1,14 @@
|
||||
package nex
|
||||
|
||||
import jsoniter "github.com/json-iterator/go"
|
||||
|
||||
func EncodeMap(policy map[string]any) []byte {
|
||||
raw, _ := jsoniter.Marshal(policy)
|
||||
return raw
|
||||
}
|
||||
|
||||
func DecodeMap(raw []byte) map[string]any {
|
||||
var out map[string]any
|
||||
_ = jsoniter.Unmarshal(raw, &out)
|
||||
return out
|
||||
}
|
14
pkg/nex/models.go
Normal file
14
pkg/nex/models.go
Normal file
@ -0,0 +1,14 @@
|
||||
package nex
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
}
|
20
pkg/nex/ws.go
Normal file
20
pkg/nex/ws.go
Normal file
@ -0,0 +1,20 @@
|
||||
package nex
|
||||
|
||||
import "github.com/goccy/go-json"
|
||||
|
||||
type WebSocketPackage struct {
|
||||
Action string `json:"w"`
|
||||
Endpoint string `json:"e,omitempty"`
|
||||
Message string `json:"m,omitempty"`
|
||||
Payload any `json:"p"`
|
||||
}
|
||||
|
||||
func (v WebSocketPackage) Marshal() []byte {
|
||||
data, _ := json.Marshal(v)
|
||||
return data
|
||||
}
|
||||
|
||||
func (v WebSocketPackage) RawPayload() []byte {
|
||||
out, _ := json.Marshal(v.Payload)
|
||||
return out
|
||||
}
|
Reference in New Issue
Block a user