page counter; last build

This commit is contained in:
Arnas Udovic 2024-07-18 14:16:25 +03:00
parent 5877520b86
commit 73e78e6db6
5 changed files with 51 additions and 3 deletions

7
go.mod
View file

@ -1,6 +1,6 @@
module g.arns.lt/zordsdavini/arns-lt-gemini
go 1.18
go 1.22.5
require (
git.sr.ht/~adnano/go-gemini v0.2.3
@ -10,10 +10,13 @@ require (
google.golang.org/protobuf v1.28.1
)
require g.arns.lt/zordsdavini/abcex v1.0.0 // indirect
require (
g.arns.lt/zordsdavini/zordfsdb v1.0.0
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
)

5
go.sum
View file

@ -1,5 +1,9 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
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/zordfsdb v1.0.0 h1:dV7FipB3Zx231cl7I6GosMcArlAAJ0yeqhq/bnaI2Hc=
g.arns.lt/zordsdavini/zordfsdb v1.0.0/go.mod h1:bC1e952FU1B8xmNd0U8IImkRZP0mztA4ZaLPAV49vC4=
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/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
@ -90,6 +94,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=

32
main.go
View file

@ -12,6 +12,7 @@ import (
"strings"
"time"
"g.arns.lt/zordsdavini/zordfsdb"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/certificate"
"github.com/flosch/pongo2/v6"
@ -77,6 +78,14 @@ func main() {
}
func processRebuild(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
db, err := zordfsdb.InitDB("./db")
if err != nil {
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
log.Fatal(err)
return
}
db.Now("last_rebuild")
log.Println("REBUILD")
q, err := gemini.QueryUnescape(r.URL.RawQuery)
@ -121,6 +130,14 @@ func processFavicon(_ context.Context, w gemini.ResponseWriter, _ *gemini.Reques
func process(_ context.Context, w gemini.ResponseWriter, r *gemini.Request) {
log.Println("-> " + r.URL.Path)
db, err := zordfsdb.InitDB("./db")
if err != nil {
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
log.Fatal(err)
return
}
db.Inc("page_counter")
conn, err := grpc.Dial(
fileSrvHost+":"+fileSrvPort,
grpc.WithTransportCredentials(insecure.NewCredentials()),
@ -203,6 +220,13 @@ func getLangFilters() []*TreeRequest_Filter {
}
func renderIndex(w gemini.ResponseWriter, client TreeManagerClient) {
db, err := zordfsdb.InitDB("./db")
if err != nil {
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
log.Fatal(err)
return
}
path := ""
tr := TreeRequest{Path: &path, Filter: getLangFilters()}
@ -215,7 +239,13 @@ func renderIndex(w gemini.ResponseWriter, client TreeManagerClient) {
w.SetMediaType("text/gemini")
tpl := pongo2.Must(pongo2.FromFile(fmt.Sprintf("templates/%s/index.gmi", defaultLang)))
page, err := tpl.Execute(
pongo2.Context{"tree": tree, "lang": defaultLang, "lastFiles": GetLastFiles(tree.Files)},
pongo2.Context{
"tree": tree,
"lang": defaultLang,
"lastFiles": GetLastFiles(tree.Files),
"lastRebuild": db.Get("last_rebuild"),
"pageCounter": db.Get("page_counter"),
},
)
if err != nil {
log.Fatalf("template failed: %v", err)

View file

@ -19,3 +19,8 @@ Welcome to my capsule in Gemini space. {{ logo() }}
=> /f all texts ({{ tree.Files|length }})
=> /s search
```
Last build: {{lastRebuild}}
Page counter: {{pageCounter}}
```

View file

@ -21,3 +21,8 @@ Sveikė atvīkėn i mona kapsolė Gemini ertie. {{ logo() }}
=> /f vėsė straipsnē ({{ tree.Files|length }})
=> /s ėiškuok
```
Paskutinis atnaujėnėms: {{lastRebuild}}
Lonkītuoju skaitlios: {{pageCounter}}
```