Cruda Basis

This commit is contained in:
2024-10-20 19:55:52 +08:00
parent 4adbfe9c19
commit eaa1b04b75
9 changed files with 142 additions and 204 deletions

View File

@ -39,6 +39,15 @@ func invokeCommand(c *fiber.Ctx) error {
strings.Join(v, "\n"),
)
}
for k, v := range c.Queries() {
meta = append(
meta,
strings.ToLower(fmt.Sprintf("query.%s", strings.ReplaceAll(k, "-", "_"))),
v,
)
}
ctx := metadata.AppendToOutgoingContext(c.Context(), meta...)
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()

View File

@ -2,11 +2,14 @@ package cruda
import (
"context"
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"google.golang.org/grpc/metadata"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func (v *CudaConn) AllocDatabase(name string) (string, error) {
func (v *CrudConn) AllocDatabase(name string) (string, error) {
conn := v.Conn.GetNexusGrpcConn()
ctx := context.Background()
ctx = metadata.AppendToOutgoingContext(ctx, "client_id", v.Conn.Info.Id)
@ -16,5 +19,18 @@ func (v *CudaConn) AllocDatabase(name string) (string, error) {
if err != nil || !out.GetIsSuccess() {
return "", err
}
return out.GetDsn(), nil
dsn := out.GetDsn()
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return "", err
}
v.db = db
return dsn, nil
}
func MigrateModel[T any](v *CrudConn, model T) error {
if v.db == nil {
return fmt.Errorf("database has not been allocated")
}
return v.db.AutoMigrate(model)
}

View File

@ -24,7 +24,7 @@ func TestAllocDatabase(t *testing.T) {
t.Fatal(fmt.Errorf("unable to register service: %v", err))
}
cc := cruda.NewCudaConn(conn)
cc := cruda.NewCrudaConn(conn)
dsn, err := cc.AllocDatabase("test")
if err != nil {
t.Fatal(fmt.Errorf("unable to allocate database: %v", err))

42
pkg/nex/cruda/command.go Normal file
View File

@ -0,0 +1,42 @@
package cruda
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"net/http"
"strconv"
)
type CrudAction func(v *CrudConn) nex.CommandHandler
func AddModel[T any](v *CrudConn, model T, id, prefix string, tags []string) error {
funcList := []CrudAction{cmdList[T]}
funcCmds := []string{".list", ".get", ".create", ".update", ".delete"}
for idx, fn := range funcList {
if err := v.Conn.AddCommand(prefix+id+funcCmds[idx], "get", tags, fn(v)); err != nil {
return err
}
}
return nil
}
func cmdList[T any](c *CrudConn) nex.CommandHandler {
return func(ctx *nex.CommandCtx) error {
rawTake := ctx.ValueOrElse("query.take", "10").(string)
rawSkip := ctx.ValueOrElse("query.skip", "0").(string)
take, err := strconv.Atoi(rawTake)
if err != nil {
take = 10
}
skip, err := strconv.Atoi(rawSkip)
if err != nil {
skip = 0
}
var out []T
if err := c.db.Offset(skip).Limit(take).Find(&out).Error; err != nil {
return err
}
return ctx.JSON(out, http.StatusOK)
}
}

View File

@ -0,0 +1,58 @@
package cruda_test
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"testing"
"time"
)
type Test struct {
cruda.BaseModel
Content string `json:"content"`
}
func TestCrudaCommand(t *testing.T) {
conn, err := nex.NewNexusConn("127.0.0.1:7001", &proto.ServiceInfo{
Id: "cruda01",
Type: "cruda",
Label: "CRUD Accelerator",
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))
}
cc := cruda.NewCrudaConn(conn)
dsn, err := cc.AllocDatabase("test")
if err != nil {
t.Fatal(fmt.Errorf("unable to allocate database: %v", err))
}
t.Log(fmt.Sprintf("Allocated database: %s", dsn))
if err := cruda.MigrateModel(cc, Test{}); err != nil {
t.Fatal(fmt.Errorf("unable to migrate database: %v", err))
}
if err := cruda.AddModel(cc, Test{}, "tm", "test.", nil); err != nil {
t.Fatal(fmt.Errorf("unable to add commands: %v", err))
}
go func() {
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 180 seconds for calling command...")
time.Sleep(time.Second * 180)
}

View File

@ -1,13 +1,18 @@
package cruda
import "git.solsynth.dev/hypernet/nexus/pkg/nex"
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"gorm.io/gorm"
)
type CudaConn struct {
type CrudConn struct {
Conn *nex.Conn
db *gorm.DB
}
func NewCudaConn(conn *nex.Conn) *CudaConn {
return &CudaConn{
func NewCrudaConn(conn *nex.Conn) *CrudConn {
return &CrudConn{
Conn: conn,
}
}

View File

@ -1,9 +1,8 @@
package nex
package cruda
import (
"time"
"gorm.io/gorm"
"time"
)
type BaseModel struct {