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

package plug
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"net"
"git.dragse.it/anthrove/otter-space-sdk/pkg/graph"
pb "git.dragse.it/anthrove/plug-sdk/pkg/grpc"
"google.golang.org/grpc"
)
type TaskExecution func(ctx context.Context, graph graph.OtterSpace, sourceUsername string, anthroveUser models.AnthroveUser, deepScrape bool, cancelFunction func()) error
type Server struct {
address string
port string
ctx context.Context
database graph.OtterSpace
taskExecutionFunction TaskExecution
}
func NewServer(ctx context.Context, address string, port string) Server {
return Server{
ctx: ctx,
address: address,
port: port,
}
}
func (s *Server) Listen() error {
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))
err = grpcServer.Serve(lis)
if err != nil {
return err
}
return nil
}
func (s *Server) WithGraphConnection(graph graph.OtterSpace) {
s.database = graph
}
func (s *Server) TaskExecutionFunction(function TaskExecution) {
s.taskExecutionFunction = function
}