arns-lt-tree-push-service/server.go
Arnas Udovicius 2c25bab053
All checks were successful
continuous-integration/drone/push Build is passing
rootFiles
2022-08-20 23:44:07 +03:00

93 lines
1.7 KiB
Go

package main
import (
"context"
"google.golang.org/grpc"
"log"
"net"
"os"
zordTree "g.arns.lt/zordsdavini/zord-tree"
)
type server struct {
UnimplementedTreeManagerServer
}
func (TreeManagerServer *server) GetSummery(_ context.Context, in *TreeRequest) (*Tree, error) {
log.Println(in)
tree, err := zordTree.BuildTree(dest, meta)
if err != nil {
log.Fatal(err)
return nil, err
}
if *in.Path != "" {
ntree, err := tree.Slice(dest + *in.Path)
if err != nil {
log.Fatal(err)
return &Tree{}, err
}
tree = ntree
}
if len(in.Filter) != 0 {
ntree, found := tree.Filter(simplifyFilter(in.Filter))
if !found {
return &Tree{}, err
}
tree = ntree
}
formattedTree := FormatTreeSummery(tree)
return &formattedTree, nil
}
func (TreeManagerServer *server) GetFile(_ context.Context, in *FileRequest) (*FileContent, error) {
log.Println(in)
tree, err := zordTree.BuildTree(dest, meta)
if err != nil {
log.Fatal(err)
return nil, err
}
file, err := tree.FileById(in.Id)
if err != nil {
return nil, err
}
fileContent := GetContent(file)
return &fileContent, nil
}
func simplifyFilter(filter []*TreeRequest_Filter) map[string]string {
simpleFilter := make(map[string]string)
for _, f := range filter {
simpleFilter[f.Key] = f.Value
}
return simpleFilter
}
func Run() {
if _, err := os.Stat(dest); os.IsNotExist(err) {
log.Fatalf("Destination directory does not exist.")
}
ln, err := net.Listen("tcp", ":8301")
if err != nil {
log.Fatalf("Failed to listen: " + err.Error())
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
RegisterTreeManagerServer(grpcServer, &server{})
err = grpcServer.Serve(ln)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
}