This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
plug-sdk/pkg/plug/server.go

59 lines
1.3 KiB
Go
Raw Normal View History

2024-02-20 18:11:34 +00:00
package plug
import (
"context"
"fmt"
2024-07-06 22:26:24 +00:00
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database"
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
2024-02-20 18:11:34 +00:00
"net"
pb "git.dragse.it/anthrove/plug-sdk/pkg/grpc"
"google.golang.org/grpc"
)
2024-07-06 22:26:24 +00:00
type TaskExecution func(ctx context.Context, graph database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, cancelFunction func()) error
2024-02-20 18:11:34 +00:00
type Server struct {
address string
port string
ctx context.Context
2024-07-06 22:26:24 +00:00
database database.OtterSpace
taskExecutionFunction TaskExecution
2024-02-20 18:11:34 +00:00
}
func NewServer(ctx context.Context, address string, port string) Server {
return Server{
ctx: ctx,
address: address,
port: port,
}
}
func (s *Server) Listen() error {
2024-02-20 18:11:34 +00:00
var err error
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", s.address, s.port))
if err != nil {
return err
}
grpcServer := grpc.NewServer()
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(s.database, s.taskExecutionFunction))
2024-02-20 18:11:34 +00:00
err = grpcServer.Serve(lis)
if err != nil {
return err
}
return nil
}
2024-07-06 22:26:24 +00:00
func (s *Server) WithGraphConnection(graph database.OtterSpace) {
2024-02-20 18:11:34 +00:00
s.database = graph
}
func (s *Server) TaskExecutionFunction(function TaskExecution) {
s.taskExecutionFunction = function
}