created gemini service and comunication with text-push service

This commit is contained in:
Arnas Udovicius 2022-08-07 08:47:15 +03:00
parent efac20ad2a
commit a00078d332
7 changed files with 250 additions and 0 deletions

57
formatter.go Normal file
View file

@ -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
}

16
go.mod Normal file
View file

@ -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
)

22
go.sum Normal file
View file

@ -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=

85
main.go Normal file
View file

@ -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)
}

26
templates/en/index.gmi Normal file
View file

@ -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}}

26
templates/sgs/index.gmi Normal file
View file

@ -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}}

18
text_server.go Normal file
View file

@ -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
}