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

135 lines
2.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"
2023-06-27 13:57:14 +00:00
"fmt"
2023-06-28 21:10:54 +00:00
zordTree "g.arns.lt/zordsdavini/zord-tree"
2022-08-19 22:03:00 +00:00
"google.golang.org/grpc"
2023-06-27 13:57:14 +00:00
"io"
2022-08-19 22:03:00 +00:00
"log"
2022-08-02 05:16:11 +00:00
"net"
"os"
2023-06-27 13:57:14 +00:00
"path"
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) {
2022-08-20 20:44:07 +00:00
log.Println(in)
2023-06-28 21:10:54 +00:00
tree, err := zordTree.BuildTree(source, meta)
2022-08-19 22:03:00 +00:00
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 != "" {
2023-06-28 21:10:54 +00:00
ntree, err := tree.Slice(source + *in.Path)
2022-08-19 22:03:00 +00:00
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) {
2022-08-20 20:44:07 +00:00
log.Println(in)
2023-06-28 21:10:54 +00:00
tree, err := zordTree.BuildTree(source, 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
}
2022-08-22 19:53:27 +00:00
func (TreeManagerServer *server) RebuildTree(ctx context.Context, in *TreeSecret) (*RebuildResponse, error) {
2023-06-29 12:33:25 +00:00
log.Println(in)
2022-08-22 19:53:27 +00:00
success := false
if in.Key == key {
2023-06-28 21:10:54 +00:00
Collect()
success = true
2022-08-22 19:53:27 +00:00
}
response := RebuildResponse{Success: success}
return &response, nil
}
2023-06-27 13:57:14 +00:00
func (TreeManagerServer *server) DownloadAttachment(in *AttachmentRequest, responseStream TreeManager_DownloadAttachmentServer) error {
2023-06-28 21:10:54 +00:00
file, err := os.Open(path.Clean(fmt.Sprintf("%s/%s/%s", source, zordTree.AttachmentDirName, in.Path)))
2023-06-27 13:57:14 +00:00
if err != nil {
return err
}
defer file.Close()
bufferSize := 64 * 1024
buff := make([]byte, bufferSize)
for {
bytesRead, err := file.Read(buff)
if err != nil {
if err != io.EOF {
log.Println("Error during downloadingAttachment: ", err)
}
break
}
resp := &AttachmentResponse{Chunk: buff[:bytesRead]}
err = responseStream.Send(resp)
if err != nil {
return err
}
}
return nil
}
2022-08-30 16:54:31 +00:00
func simplifyFilter(filter []*TreeRequest_Filter) map[string][]string {
simpleFilter := make(map[string][]string)
2022-08-19 22:03:00 +00:00
for _, f := range filter {
2022-08-30 16:54:31 +00:00
simpleFilter[f.Key] = append(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() {
2023-06-28 21:10:54 +00:00
if _, err := os.Stat(source); os.IsNotExist(err) {
log.Fatalf("Source 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
}
}