init
This commit is contained in:
parent
7dcd2e6e10
commit
cf99eecc6e
8 changed files with 343 additions and 1 deletions
30
README.md
30
README.md
|
@ -1,3 +1,31 @@
|
|||
# arns-lt-tree-push-service
|
||||
|
||||
Servėsos apduoruotė tekstinius failus i zord-tree miedi. Vieliau par socket'a padoutė gemini ė web pruojektams
|
||||
Servėsos apduoruotė tekstinius failus i zord-tree miedi. Vieliau par socket'a padoutė gemini ė web pruojektams
|
||||
|
||||
## Ruodas pruotuokuols
|
||||
|
||||
__tošts kreipėmāsis tor gronžintė vėsa miedi__
|
||||
|
||||
__tekstu kuolekcėjė__
|
||||
```json
|
||||
{
|
||||
"path": ["linux"],
|
||||
"filter": [
|
||||
{
|
||||
"key":"tag",
|
||||
"value":"koja"
|
||||
},
|
||||
{
|
||||
"key":"lang",
|
||||
"value":"sgs"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
__vėina teksta__
|
||||
```json
|
||||
{
|
||||
"id": "1a"
|
||||
}
|
||||
```
|
||||
|
|
18
collector.go
Normal file
18
collector.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
zord_tree "g.arns.lt/zordsdavini/zord-tree"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Collect() {
|
||||
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
||||
panic("Destination directory does not exist.")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(source); os.IsNotExist(err) {
|
||||
panic("Source directory does not exist.")
|
||||
}
|
||||
|
||||
_ = zord_tree.PopulateTree(source, dest, meta, customMeta)
|
||||
}
|
54
formatter.go
Normal file
54
formatter.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package main
|
||||
|
||||
import zord_tree "g.arns.lt/zordsdavini/zord-tree"
|
||||
|
||||
type FSummery struct {
|
||||
Name string
|
||||
Category []string
|
||||
Description string
|
||||
Id string
|
||||
Lang string
|
||||
Tags []string
|
||||
}
|
||||
|
||||
type TSummery struct {
|
||||
Files []FSummery
|
||||
Tags map[string]int
|
||||
}
|
||||
|
||||
type FileContent struct {
|
||||
File zord_tree.File
|
||||
Content string
|
||||
}
|
||||
|
||||
func FormatTreeSummery(tree zord_tree.Tree) TSummery {
|
||||
ts := TSummery{Tags: map[string]int{}}
|
||||
readTree(tree, &ts)
|
||||
|
||||
return ts
|
||||
}
|
||||
|
||||
func readTree(tree zord_tree.Tree, ts *TSummery) {
|
||||
for _, subtree := range tree.Dirs {
|
||||
readTree(subtree, ts)
|
||||
}
|
||||
|
||||
for _, file := range tree.Files {
|
||||
ts.Files = append(ts.Files, FSummery{
|
||||
Name: file.Name,
|
||||
Category: file.Category,
|
||||
Description: file.Meta["description"],
|
||||
Id: file.Id,
|
||||
Lang: file.Meta["lang"],
|
||||
Tags: file.Tags,
|
||||
})
|
||||
for _, tag := range file.Tags {
|
||||
ts.Tags[tag]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetContent(file zord_tree.File) FileContent {
|
||||
content, _ := zord_tree.ReadFileContent(file)
|
||||
return FileContent{File: file, Content: content}
|
||||
}
|
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -0,0 +1,10 @@
|
|||
module g.arns.lt/zordsdavini/arns-lt-tree-push-service
|
||||
|
||||
go 1.18
|
||||
|
||||
require g.arns.lt/zordsdavini/zord-tree v0.2.1
|
||||
|
||||
require (
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0 // indirect
|
||||
github.com/otiai10/copy v1.6.0 // indirect
|
||||
)
|
15
go.sum
Normal file
15
go.sum
Normal file
|
@ -0,0 +1,15 @@
|
|||
g.arns.lt/zordsdavini/abcex v1.0.0 h1:qQqlZ4DMfethCGK4I6yGaLqMrTzKNIshqpINd1l3t0E=
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.1.5 h1:BVb4vll3ZfDPKvzo5yfL5RqU14UqVAzFZ9y2x6nukHs=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.1.5/go.mod h1:TfZmt224elDnfsLoHQHqFhleLOOsKDQMkyvzJYOA+zo=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.2.0 h1:FI0QO9qVjDW7GEwjmB4aHwxr0JHws0Ukl/+XWDG8cgE=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.2.0/go.mod h1:R9VMBwVwW6pu8yFIIgsMUv7YuWa48dj6DglkyXeTPik=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.2.1 h1:GYbwojTqCTcr/e4oYNxTUCP7V/2cD8xn/AYOecYGnxo=
|
||||
g.arns.lt/zordsdavini/zord-tree v0.2.1/go.mod h1:R9VMBwVwW6pu8yFIIgsMUv7YuWa48dj6DglkyXeTPik=
|
||||
github.com/otiai10/copy v1.6.0 h1:IinKAryFFuPONZ7cm6T6E2QX/vcJwSnlaA5lfoaXIiQ=
|
||||
github.com/otiai10/copy v1.6.0/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E=
|
||||
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
|
||||
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
|
||||
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
||||
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
|
||||
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
|
36
main.go
Normal file
36
main.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
source string
|
||||
dest string
|
||||
meta = []string{"description", "lang", "copyright", "created"} //_ = zord_tree.PopulateTree(cfg.MdTree.Path, cfg.MdTree.DPath, meta)
|
||||
customMeta = make(map[string]func() string)
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&dest, "dest", "", "destination directory to read from")
|
||||
flag.StringVar(&source, "source", "", "source to collect texts.")
|
||||
flag.Parse()
|
||||
|
||||
customMeta["copyright"] = func() string {
|
||||
return "CC-BY-SA 4.0"
|
||||
}
|
||||
customMeta["created"] = func() string {
|
||||
currentTime := time.Now()
|
||||
return currentTime.Format("2006-01-02")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
switch action := flag.Arg(0); action {
|
||||
case "collect":
|
||||
Collect()
|
||||
default:
|
||||
Run()
|
||||
}
|
||||
}
|
81
server.go
Normal file
81
server.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
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
|
||||
}
|
100
tree.go
Normal file
100
tree.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
name string
|
||||
fullPath string
|
||||
category []string
|
||||
description string
|
||||
id string
|
||||
lang string
|
||||
tags []string
|
||||
}
|
||||
|
||||
type Tree struct {
|
||||
path string
|
||||
dirs []Tree
|
||||
files []File
|
||||
}
|
||||
|
||||
func BuildTree(dirPath string) Tree {
|
||||
return readPath(dirPath, []string{})
|
||||
}
|
||||
|
||||
func readPath(dirPath string, category []string) Tree {
|
||||
tree := Tree{}
|
||||
|
||||
tree.path = dirPath
|
||||
|
||||
files, err := ioutil.ReadDir(dirPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
fullPath := path.Join(dirPath, file.Name())
|
||||
if file.IsDir() {
|
||||
tree.dirs = append(tree.dirs, readPath(fullPath, append(category, file.Name())))
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := ioutil.ReadFile(fullPath)
|
||||
if err == nil {
|
||||
tree.files = append(tree.files, readFile(file, fullPath, category))
|
||||
}
|
||||
}
|
||||
|
||||
return tree
|
||||
}
|
||||
|
||||
func readFile(file fs.FileInfo, fullPath string, category []string) File {
|
||||
f := File{
|
||||
name: file.Name(),
|
||||
fullPath: fullPath,
|
||||
category: category,
|
||||
}
|
||||
|
||||
osf, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open %s", fullPath)
|
||||
}
|
||||
scanner := bufio.NewScanner(osf)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "---" {
|
||||
break
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "tags:") {
|
||||
line = strings.TrimPrefix(line, "tags:")
|
||||
t := strings.Split(line, ",")
|
||||
tags := []string{}
|
||||
for _, tag := range t {
|
||||
tags = append(tags, strings.Trim(tag, " "))
|
||||
}
|
||||
|
||||
f.tags = tags
|
||||
}
|
||||
if strings.HasPrefix(line, "description:") {
|
||||
line = strings.TrimPrefix(line, "description:")
|
||||
f.description = strings.Trim(line, " ")
|
||||
}
|
||||
if strings.HasPrefix(line, "lang:") {
|
||||
line = strings.TrimPrefix(line, "lang:")
|
||||
f.lang = strings.Trim(line, " ")
|
||||
}
|
||||
}
|
||||
osf.Close()
|
||||
|
||||
return f
|
||||
}
|
Loading…
Reference in a new issue