arns-lt-tree-push-service/server.go

92 lines
1.7 KiB
Go
Raw Normal View History

2022-08-02 05:16:11 +00:00
package main
import (
2022-08-19 22:03:00 +00:00
"context"
"google.golang.org/grpc"
"log"
2022-08-02 05:16:11 +00:00
"net"
"os"
2022-08-19 22:03:00 +00:00
zordTree "g.arns.lt/zordsdavini/zord-tree"
2022-08-02 05:16:11 +00:00
)
2022-08-19 22:03:00 +00:00
type server struct {
UnimplementedTreeManagerServer
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
func (TreeManagerServer *server) GetSummery(_ context.Context, in *TreeRequest) (*Tree, error) {
tree, err := zordTree.BuildTree(dest, meta)
if err != nil {
log.Fatal(err)
return nil, err
}
2022-08-02 05:16:11 +00:00
2022-08-19 22:03:00 +00:00
if *in.Path != "" {
ntree, err := tree.Slice(dest + *in.Path)
if err != nil {
log.Fatal(err)
return &Tree{}, err
}
tree = ntree
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
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) {
tree, err := zordTree.BuildTree(dest, meta)
2022-08-02 05:16:11 +00:00
if err != nil {
2022-08-19 22:03:00 +00:00
log.Fatal(err)
return nil, err
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
file, err := tree.FileById(in.Id)
2022-08-02 05:16:11 +00:00
if err != nil {
2022-08-19 22:03:00 +00:00
return nil, err
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
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
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
return simpleFilter
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
func Run() {
if _, err := os.Stat(dest); os.IsNotExist(err) {
log.Fatalf("Destination directory does not exist.")
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
ln, err := net.Listen("tcp", ":8301")
2022-08-02 05:16:11 +00:00
if err != nil {
2022-08-19 22:03:00 +00:00
log.Fatalf("Failed to listen: " + err.Error())
2022-08-02 05:16:11 +00:00
}
2022-08-19 22:03:00 +00:00
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)
2022-08-02 05:16:11 +00:00
}
}