81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
|
|
zord_tree "g.arns.lt/zordsdavini/zord-tree"
|
|
)
|
|
|
|
type Request struct {
|
|
Id string
|
|
Path []string
|
|
Filter []Filter
|
|
}
|
|
|
|
type Filter struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
func Run() {
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
|
panic("Destination directory does not exist.")
|
|
}
|
|
|
|
tree, err := zord_tree.BuildTree(dest, meta)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ln, err := net.Listen("tcp", ":8001")
|
|
if err != nil {
|
|
fmt.Println("Error starting server: " + err.Error())
|
|
}
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
fmt.Println("Error listening to client: " + err.Error())
|
|
continue
|
|
}
|
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
|
if err != nil {
|
|
conn.Close()
|
|
} else {
|
|
go sendData(conn, message, tree)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sendData(conn net.Conn, message string, tree zord_tree.Tree) {
|
|
message = strings.ReplaceAll(message, "\n", "")
|
|
if message == "" {
|
|
response, err := json.Marshal(FormatTreeSummery(tree))
|
|
writtenBytes, err := fmt.Fprintf(conn, string(response)+"\n")
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Printf("sent %d bytes\n", writtenBytes)
|
|
return
|
|
}
|
|
|
|
var request Request
|
|
err := json.Unmarshal([]byte(message), &request)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if request.Id != "" {
|
|
fmt.Println("servynam faila")
|
|
return
|
|
}
|
|
|
|
// einam i tree path
|
|
fmt.Fprintf(conn, "qq\n")
|
|
return
|
|
}
|