feat: added Message Functions and renamed to be more clean
This commit is contained in:
parent
9b8321f5f6
commit
6be508532e
@ -3,43 +3,62 @@ package plug
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database"
|
||||
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
|
||||
"net"
|
||||
|
||||
pb "git.dragse.it/anthrove/plug-sdk/pkg/grpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type TaskExecution func(ctx context.Context, graph database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, cancelFunction func()) error
|
||||
type TaskExecution func(ctx context.Context, database database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error
|
||||
type SendMessageExecution func(ctx context.Context, userSourceID string, message string)
|
||||
|
||||
type Server struct {
|
||||
type Plug struct {
|
||||
address string
|
||||
port string
|
||||
ctx context.Context
|
||||
database database.OtterSpace
|
||||
taskExecutionFunction TaskExecution
|
||||
sendMessageExecution SendMessageExecution
|
||||
source models.Source
|
||||
}
|
||||
|
||||
func NewServer(ctx context.Context, address string, port string) Server {
|
||||
return Server{
|
||||
func NewPlug(ctx context.Context, address string, port string, source models.Source) Plug {
|
||||
return Plug{
|
||||
ctx: ctx,
|
||||
address: address,
|
||||
port: port,
|
||||
source: source,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Listen() error {
|
||||
func (p *Plug) Listen() error {
|
||||
var err error
|
||||
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", s.address, s.port))
|
||||
plugSource, err := p.database.GetSourceByDomain(p.ctx, models.AnthroveSourceDomain(p.source.Domain))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if plugSource == nil {
|
||||
log.Printf("no source found for domain %p, initilazing source!", p.source.Domain)
|
||||
err := p.database.CreateSource(p.ctx, &p.source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf("%p:%p", p.address, p.port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
|
||||
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(s.database, s.taskExecutionFunction))
|
||||
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction))
|
||||
|
||||
err = grpcServer.Serve(lis)
|
||||
if err != nil {
|
||||
@ -49,10 +68,14 @@ func (s *Server) Listen() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) WithGraphConnection(graph database.OtterSpace) {
|
||||
s.database = graph
|
||||
func (p *Plug) WithOtterSpace(graph database.OtterSpace) {
|
||||
p.database = graph
|
||||
}
|
||||
|
||||
func (s *Server) TaskExecutionFunction(function TaskExecution) {
|
||||
s.taskExecutionFunction = function
|
||||
func (p *Plug) TaskExecutionFunction(function TaskExecution) {
|
||||
p.taskExecutionFunction = function
|
||||
}
|
||||
|
||||
func (p *Plug) SendMessageExecution(function SendMessageExecution) {
|
||||
p.sendMessageExecution = function
|
||||
}
|
||||
|
Reference in New Issue
Block a user