~adnano/astronaut

ref: 72ba913688a9d2dafb0bfdf11a5cee16685ce98a astronaut/about.go -rw-r--r-- 1.7 KiB
72ba9136Adnan Maolood Use scfg to configure settings 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"crypto/x509"
	"embed"
	"os"
	"path/filepath"
	"text/template"

	"git.sr.ht/~adnano/go-gemini"
	"git.sr.ht/~adnano/go-gemini/tofu"
	"git.sr.ht/~adnano/go-xdg"
)

//go:embed about
var static embed.FS

// handleAbout handles a request for an about: URI.
func (t *Tab) handleAbout(w gemini.ResponseWriter, r *gemini.Request) {
	switch r.URL.Host {
	case "about":
		ctx := struct {
			Version string
		}{
			Version: Version,
		}
		templates.ExecuteTemplate(w, "about.tmpl", ctx)
	case "bookmarks":
		path := filepath.Join(xdg.DataHome(), "astronaut", "bookmarks.gmi")
		gemini.ServeFile(w, os.DirFS(""), path)
	case "certificates":
		templates.ExecuteTemplate(w, "certificates.tmpl", t.browser.certs.Entries())
	case "history":
		templates.ExecuteTemplate(w, "history.tmpl", t.pages)
	case "knownhosts":
		templates.ExecuteTemplate(w, "knownhosts.tmpl", t.browser.hosts.Entries())
	case "newtab":
		gemini.ServeFile(w, static, "about/newtab.gmi")
	case "welcome":
		gemini.ServeFile(w, static, "about/welcome.gmi")
	default:
		w.WriteHeader(gemini.StatusNotFound, "Not found")
	}
}

// handleFile handles a request for a local file.
func handleFile(w gemini.ResponseWriter, r *gemini.Request) {
	gemini.ServeFile(w, os.DirFS(""), r.URL.Path)
}

var templates *template.Template

func init() {
	funcs := template.FuncMap{
		"host": func(hostname string, cert *x509.Certificate) tofu.Host {
			return tofu.NewHost(hostname, cert.Raw)
		},
		"title": func(err error) string {
			if title, ok := err.(interface {
				Title() string
			}); ok {
				return title.Title()
			}
			return ""
		},
	}
	templates = template.New("templates").Funcs(funcs)
	templates = template.Must(templates.ParseFS(static, "about/*.tmpl"))
}