feat(database): Update SDK to v3 and refactor database

This commit is contained in:
SoXX 2024-08-14 16:06:53 +02:00
parent 5e6b9e0990
commit cf4a5bc9a2
5 changed files with 38 additions and 47 deletions

View File

@ -7,14 +7,14 @@ Anthrove Plug SDK is a Golang-based Software Development Kit (SDK) that provides
To install the Anthrove Plug SDK, you will need to have Go installed on your system. You can then use the go get command to fetch the SDK:
```bash
go get git.anthrove.art/anthrove/plug-sdk/v2
go get git.anthrove.art/Anthrove/plug-sdk/v3
```
## Usage
Below is a basic example of how to use the SDK:
````go
import "git.anthrove.art/anthrove/plug-sdk/v2/pkg/plug"
import "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/plug"
// Define what Source this Plug is used for
source := models.Source{

14
go.mod
View File

@ -1,9 +1,9 @@
module git.anthrove.art/anthrove/plug-sdk/v2
module git.anthrove.art/Anthrove/plug-sdk/v3
go 1.22.0
require (
git.anthrove.art/anthrove/otter-space-sdk/v2 v2.1.0
git.anthrove.art/Anthrove/otter-space-sdk/v3 v3.0.0
github.com/golang/protobuf v1.5.4
github.com/matoous/go-nanoid/v2 v2.1.0
google.golang.org/grpc v1.61.1
@ -19,14 +19,14 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/rubenv/sql-migrate v1.6.1 // indirect
github.com/rubenv/sql-migrate v1.7.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
gorm.io/driver/postgres v1.5.9 // indirect
gorm.io/gorm v1.25.10 // indirect
gorm.io/gorm v1.25.11 // indirect
)

View File

@ -2,19 +2,11 @@ package otter
import (
"context"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/database"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
)
func ConnectToDatabase(ctx context.Context, config models.DatabaseConfig) (database.OtterSpace, error) {
var otterSpace database.OtterSpace
var err error
otterSpace = database.NewPostgresqlConnection()
err = otterSpace.Connect(ctx, config)
if err != nil {
return nil, err
}
return otterSpace, err
func ConnectToDatabase(ctx context.Context, config models.DatabaseConfig) error {
return database.Connect(ctx, config)
}

View File

@ -4,27 +4,24 @@ import (
"context"
"log"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/database"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
"google.golang.org/protobuf/types/known/timestamppb"
gRPC "git.anthrove.art/anthrove/plug-sdk/v2/pkg/grpc"
gRPC "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
gonanoid "github.com/matoous/go-nanoid/v2"
)
type server struct {
gRPC.UnimplementedPlugConnectorServer
ctx map[string]context.CancelFunc
database database.OtterSpace
taskExecutionFunction TaskExecution
sendMessageExecution SendMessageExecution
getMessageExecution GetMessageExecution
}
func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution, getMessageExecution GetMessageExecution) gRPC.PlugConnectorServer {
func NewGrpcServer(taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution, getMessageExecution GetMessageExecution) gRPC.PlugConnectorServer {
return &server{
ctx: make(map[string]context.CancelFunc),
database: database,
taskExecutionFunction: taskExecutionFunction,
sendMessageExecution: sendMessageExecution,
getMessageExecution: getMessageExecution,
@ -32,7 +29,7 @@ func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecu
}
func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) (*gRPC.PlugTaskStatus, error) {
var anthroveUser models.User
var user models.User
var plugTaskState gRPC.PlugTaskStatus
var err error
@ -44,7 +41,7 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation)
plugTaskState.TaskId = id
plugTaskState.TaskState = gRPC.PlugTaskState_RUNNING
anthroveUser.ID = models.AnthroveUserID(creation.UserId)
user.ID = models.UserID(creation.UserId)
// gRPC closes the context after the call ended. So the whole scrapping stopped without waiting
// by using this method we assign a new context to each new request we get.
@ -54,7 +51,7 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation)
go func() {
// FIXME: better implement this methode, works for now but needs refactoring
err := s.taskExecutionFunction(ctx, s.database, creation.UserSourceName, anthroveUser, creation.DeepScrape, creation.ApiKey, func() {
err := s.taskExecutionFunction(ctx, creation.UserSourceName, user, creation.DeepScrape, creation.ApiKey, func() {
s.removeTask(id)
})
if err != nil {

View File

@ -2,17 +2,17 @@ package plug
import (
"context"
"errors"
"fmt"
"log"
"net"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/database"
otterError "git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
"github.com/golang/protobuf/ptypes/timestamp"
pb "git.anthrove.art/anthrove/plug-sdk/v2/pkg/grpc"
pb "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
"google.golang.org/grpc"
)
@ -22,7 +22,7 @@ type Message struct {
CreatedAt *timestamp.Timestamp
}
type TaskExecution func(ctx context.Context, database database.OtterSpace, userSourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error
type TaskExecution func(ctx context.Context, userSourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error
type SendMessageExecution func(ctx context.Context, userSourceID string, userSourceUsername string, message string) error
type GetMessageExecution func(ctx context.Context, userSourceID string, userSourceUsername string) ([]Message, error)
@ -30,7 +30,6 @@ type Plug struct {
address string
port string
ctx context.Context
database database.OtterSpace
taskExecutionFunction TaskExecution
sendMessageExecution SendMessageExecution
getMessageExecution GetMessageExecution
@ -49,14 +48,21 @@ func NewPlug(ctx context.Context, address string, port string, source models.Sou
func (p *Plug) Listen() error {
var err error
log.Printf("initilazing source!")
err = p.database.CreateSource(p.ctx, &p.source)
log.Print("Check if source exists")
_, err = database.GetSourceByDomain(p.ctx, p.source.Domain)
if err != nil {
if !errors.Is(err, &otterError.NoDataWritten{}) {
log.Panic(err)
if err.Error() == otterError.NoDataFound {
log.Printf("iIitializing source!")
if _, err = database.CreateSource(p.ctx, p.source); err != nil {
panic(err)
}
} else {
panic(err)
}
}
log.Print("Source exists")
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", p.address, p.port))
if err != nil {
return err
@ -64,7 +70,7 @@ func (p *Plug) Listen() error {
grpcServer := grpc.NewServer()
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction, p.sendMessageExecution, p.getMessageExecution))
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.taskExecutionFunction, p.sendMessageExecution, p.getMessageExecution))
err = grpcServer.Serve(lis)
if err != nil {
@ -74,10 +80,6 @@ func (p *Plug) Listen() error {
return nil
}
func (p *Plug) WithOtterSpace(graph database.OtterSpace) {
p.database = graph
}
func (p *Plug) TaskExecutionFunction(function TaskExecution) {
p.taskExecutionFunction = function
}