From a00078d332e624aff559d2297e47c269c2d6becf Mon Sep 17 00:00:00 2001 From: Arnas Udovicius Date: Sun, 7 Aug 2022 08:47:15 +0300 Subject: [PATCH] created gemini service and comunication with text-push service --- formatter.go | 57 +++++++++++++++++++++++++++ go.mod | 16 ++++++++ go.sum | 22 +++++++++++ main.go | 85 +++++++++++++++++++++++++++++++++++++++++ templates/en/index.gmi | 26 +++++++++++++ templates/sgs/index.gmi | 26 +++++++++++++ text_server.go | 18 +++++++++ 7 files changed, 250 insertions(+) create mode 100644 formatter.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 templates/en/index.gmi create mode 100644 templates/sgs/index.gmi create mode 100644 text_server.go diff --git a/formatter.go b/formatter.go new file mode 100644 index 0000000..9bcb0e8 --- /dev/null +++ b/formatter.go @@ -0,0 +1,57 @@ +package main + +import ( + zord_tree "g.arns.lt/zordsdavini/zord-tree" + "sort" +) + +type FSummery struct { + Name string + Category []string + Description string + Id string + Lang string + Created string + Copyright string + Tags []string +} + +type TSummery struct { + Files []FSummery + Tags map[string]int + Categories map[string]int +} + +type FileContent struct { + File zord_tree.File + Content string +} + +func GetLastFiles(files []FSummery) []FSummery { + sortingFiles := make(map[string]FSummery) + for _, f := range files { + sortingFiles[f.Created+f.Id] = f + } + + keys := make([]string, 0, len(sortingFiles)) + for k := range sortingFiles { + keys = append(keys, k) + } + sort.Strings(keys) + sort.Reverse(sort.StringSlice(keys)) + + length := 10 + if len(keys) < 10 { + length = len(keys) + } + lastFiles := make([]FSummery, 0, length) + for i, k := range keys { + if i == length { + break + } + + lastFiles = append(lastFiles, sortingFiles[k]) + } + + return lastFiles +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..832ae86 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module g.arns.lt/zordsdavini/arns-lt-gemini + +go 1.18 + +require ( + g.arns.lt/zordsdavini/arns-lt-tree-push-service v0.0.0-20220802051611-cf99eecc6e60 + git.sr.ht/~adnano/go-gemini v0.2.3 +) + +require ( + g.arns.lt/zordsdavini/abcex v1.0.0 // indirect + g.arns.lt/zordsdavini/zord-tree v0.2.1 // indirect + github.com/otiai10/copy v1.6.0 // indirect + golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect + golang.org/x/text v0.3.3 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..07dd249 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +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/arns-lt-tree-push-service v0.0.0-20220802051611-cf99eecc6e60 h1:+b2wWaQUzFWE3RjbRR9luPadCrocw7QeTvQNCKT5hS0= +g.arns.lt/zordsdavini/arns-lt-tree-push-service v0.0.0-20220802051611-cf99eecc6e60/go.mod h1:vwR/TAStVkP8ZHofrpqdg8b/qlBbLNS8KVFp1tsKHtE= +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= +git.sr.ht/~adnano/go-gemini v0.2.3 h1:oJ+Y0/mheZ4Vg0ABjtf5dlmvq1yoONStiaQvmWWkofc= +git.sr.ht/~adnano/go-gemini v0.2.3/go.mod h1:hQ75Y0i5jSFL+FQ7AzWVAYr5LQsaFC7v3ZviNyj46dY= +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= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..27e1c45 --- /dev/null +++ b/main.go @@ -0,0 +1,85 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + "strings" + "time" + + "git.sr.ht/~adnano/go-gemini" + "git.sr.ht/~adnano/go-gemini/certificate" +) + +func main() { + certificates := &certificate.Store{} + certificates.Register("localhost") + if err := certificates.Load("/home/arnas/out/gemcert/a"); err != nil { + log.Fatal(err) + } + + mux := &gemini.Mux{} + mux.HandleFunc("/", process) + + server := &gemini.Server{ + Handler: mux, + ReadTimeout: 30 * time.Second, + WriteTimeout: 1 * time.Minute, + GetCertificate: certificates.Get, + } + + ctx := context.Background() + if err := server.ListenAndServe(ctx); err != nil { + log.Fatal(err) + } +} + +func process(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { + fmt.Println(ctx, r, r.URL, r.Host) + renderIndex("en", w) +} + +func renderIndex(lang string, w gemini.ResponseWriter) { + langPath := lang + "/" + if lang == "sgs" { + langPath = "" + } + w.SetMediaType("text/gemini") + content, err := os.ReadFile(fmt.Sprintf("templates/%s/index.gmi", lang)) + if err != nil { + w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error") + return + } + page := string(content) + + summery := GetSummery() + fmt.Println(summery) + tree := TSummery{} + json.Unmarshal([]byte(summery), &tree) + fmt.Println(tree) + + categories := "" + for c, count := range tree.Categories { + fmt.Println(c, count) + categories = categories + fmt.Sprintf("=> /%s%s/%s %s (%d)\n", langPath, "tree/path", c, c, count) + } + fmt.Println(page, categories) + page = strings.Replace(page, "{{categories}}", categories, 1) + fmt.Println(page) + + tags := "" + for t, count := range tree.Tags { + tags = tags + fmt.Sprintf("=> /t/%s %s (%d)\n", t, t, count) + } + page = strings.Replace(page, "{{tags}}", tags, 1) + + lastFiles := "" + for _, f := range GetLastFiles(tree.Files) { + lastFiles = lastFiles + fmt.Sprintf("=> /%s/%s %s (%s)\n", strings.Join(f.Category, "/"), f.Id, f.Description, f.Created) + } + page = strings.Replace(page, "{{last_posts}}", lastFiles, 1) + + fmt.Fprintf(w, page) +} diff --git a/templates/en/index.gmi b/templates/en/index.gmi new file mode 100644 index 0000000..0840be0 --- /dev/null +++ b/templates/en/index.gmi @@ -0,0 +1,26 @@ +# Arns' capsule + +Welcome to my capsule in Gemini space. + +> Če turietom būtė mon pruotinga cėtata :-P + +=> / Žemaitėška versėjė +=> https://arns.lt http version + +Main themes are spreaded by categories. Each file may have tags what can filter the tree. + +=> /en/s search +=> /en/a about + +### Categories + +{{categories}} + +### Tags + +{{tags}} + +### Last posts + +{{last_posts}} + diff --git a/templates/sgs/index.gmi b/templates/sgs/index.gmi new file mode 100644 index 0000000..ed747f0 --- /dev/null +++ b/templates/sgs/index.gmi @@ -0,0 +1,26 @@ +# Arna alkierios + +Sveikė atvīkėn i mona kapsolė Gemini ertie. + +> Če turietom būtė mon pruotinga cėtata :-P + +=> /en English version +=> https://arns.lt http versėjė + +Pagrindėnės temas paskėrstītas par kateguorėjės. Kuožnos fails gal turietė žīmas, katruos gal atsėjuotė miedi. + +=> /s ėiškuok +=> /a aple + +### Kateguorėjės + +{{categories}} + +### Žīmas + +{{tags}} + +### Paskotėnē īrašā + +{{last_posts}} + diff --git a/text_server.go b/text_server.go new file mode 100644 index 0000000..16682bc --- /dev/null +++ b/text_server.go @@ -0,0 +1,18 @@ +package main + +import ( + "bufio" + "fmt" + "net" +) + +func GetSummery() string { + conn, err := net.Dial("tcp", "127.0.0.1:8001") + if err != nil { + panic(err.Error()) + } + fmt.Fprintf(conn, "\n") + message, err := bufio.NewReader(conn).ReadString('\n') + conn.Close() + return message +}