85 lines
2 KiB
Go
85 lines
2 KiB
Go
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)
|
|
}
|