This commit is contained in:
parent
3302fa52cf
commit
b15c976693
4 changed files with 27 additions and 3 deletions
2
go.mod
2
go.mod
|
@ -13,7 +13,7 @@ require (
|
|||
require g.arns.lt/zordsdavini/abcex/v4 v4.0.4 // indirect
|
||||
|
||||
require (
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.4
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.5
|
||||
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-20220715151400-c0bba94af5f8 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -4,6 +4,8 @@ g.arns.lt/zordsdavini/abcex/v4 v4.0.4 h1:idjvgkCjrjZfDxLyOcX7lCIdIndISDAkj77VCvh
|
|||
g.arns.lt/zordsdavini/abcex/v4 v4.0.4/go.mod h1:/+//gYSUtJrdsmTtWNoffRO4xD1BuPRUMGW4ynet7iE=
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.4 h1:jtM9sXAh5kLatHyrkbky7XO2z9wgfz0fjYBg5NHVCrk=
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.4/go.mod h1:CpFWjby+GB/+mrJFTsCDJHS5UCWHty6Y7Pf/Ui7IR90=
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.5 h1:8HoKqkQM3u9GzkCl+Bco1ctBr+Uz/J+tSxyyfMShbvA=
|
||||
g.arns.lt/zordsdavini/zordfsdb v1.0.5/go.mod h1:CpFWjby+GB/+mrJFTsCDJHS5UCWHty6Y7Pf/Ui7IR90=
|
||||
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=
|
||||
|
|
24
main.go
24
main.go
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -152,7 +154,7 @@ func process(_ context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
|||
|
||||
switch {
|
||||
case "/" == r.URL.Path:
|
||||
renderIndex(w, client)
|
||||
renderIndex(w, client, r)
|
||||
case regexp.MustCompile(`^/atom.xml$`).MatchString(r.URL.Path):
|
||||
renderFeed(w, r, client)
|
||||
case regexp.MustCompile(`^/a/?$`).MatchString(r.URL.Path):
|
||||
|
@ -223,7 +225,7 @@ func getLangFilters() []*TreeRequest_Filter {
|
|||
return filters
|
||||
}
|
||||
|
||||
func renderIndex(w gemini.ResponseWriter, client TreeManagerClient) {
|
||||
func renderIndex(w gemini.ResponseWriter, client TreeManagerClient, r *gemini.Request) {
|
||||
db, err := zordfsdb.InitDB("./db")
|
||||
if err != nil {
|
||||
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
|
||||
|
@ -250,6 +252,12 @@ func renderIndex(w gemini.ResponseWriter, client TreeManagerClient) {
|
|||
db.Save("page_counter._"+defaultLang, "1")
|
||||
}
|
||||
|
||||
adminCert, found := db.Get("admin_cert")
|
||||
if !found {
|
||||
adminCert = "xxx"
|
||||
db.Save("admin_cert", "xxx")
|
||||
}
|
||||
|
||||
w.SetMediaType("text/gemini")
|
||||
tpl := pongo2.Must(pongo2.FromFile(fmt.Sprintf("templates/%s/index.gmi", defaultLang)))
|
||||
page, err := tpl.Execute(
|
||||
|
@ -259,6 +267,7 @@ func renderIndex(w gemini.ResponseWriter, client TreeManagerClient) {
|
|||
"lastFiles": GetLastFiles(tree.Files),
|
||||
"lastRebuild": lastRebuild,
|
||||
"pageCounter": pageCounter,
|
||||
"admin": adminCert == getCertFingerprint(r),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -579,3 +588,14 @@ func redirectAction(w gemini.ResponseWriter, r *gemini.Request, client TreeManag
|
|||
),
|
||||
)
|
||||
}
|
||||
|
||||
func getCertFingerprint(r *gemini.Request) string {
|
||||
if len(r.TLS().PeerCertificates) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
crt := r.TLS().PeerCertificates[0]
|
||||
sum := sha256.Sum256(crt.Raw)
|
||||
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
|
|
@ -25,3 +25,5 @@ Sveikė atvīkėn i mona kapsolė Gemini ertie. {{ logo() }}
|
|||
Last build: {{lastRebuild}}
|
||||
Total page counter: {{pageCounter}}
|
||||
```
|
||||
|
||||
{% if admin %}ADMIN{% endif %}
|
||||
|
|
Loading…
Add table
Reference in a new issue