Fix go-gemini dep, tls cert
alex wennerberg alex@alexwennerberg.com
Mon, 26 Oct 2020 12:56:22 -0700
5 files changed,
92 insertions(+),
11 deletions(-)
M
gemini.go
→
gemini.go
@@ -4,7 +4,7 @@ import (
"crypto/tls" "strings" // "fmt" - "git.sr.ht/~adnano/gmi" + gmi "git.sr.ht/~adnano/go-gemini" "io/ioutil" "log" "path"@@ -57,16 +57,22 @@
hostname := strings.SplitN(c.Host, ":", 1)[0] // is this necc? server.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate { - cert, err := tls.LoadX509KeyPair(c.TLSCertFile, c.TLSKeyFile) + cert, err := store.Lookup(hostname) if err != nil { - log.Fatal("Invalid TLS cert") + cert, err := tls.LoadX509KeyPair(c.TLSCertFile, c.TLSKeyFile) + if err != nil { + log.Fatal("Invalid TLS cert") + } + store.Add(hostname, cert) + return &cert } - return &cert + return cert } + var mux gmi.ServeMux // replace with wildcard cert - server.HandleFunc(hostname, gmiIndex) - server.HandleFunc("*."+hostname, gmiPage) + mux.HandleFunc(hostname, gmiIndex) + mux.HandleFunc("*."+hostname, gmiPage) server.ListenAndServe() }
A
gmi2html.go
@@ -0,0 +1,75 @@
+package main + +import ( + "fmt" + "html" + "strings" + + "git.sr.ht/~adnano/go-gemini" +) + +func textToHTML(text gemini.Text) string { + var b strings.Builder + var pre bool + var list bool + for _, l := range text { + if _, ok := l.(gemini.LineListItem); ok { + if !list { + list = true + fmt.Fprint(&b, "<ul>\n") + } + } else if list { + list = false + fmt.Fprint(&b, "</ul>\n") + } + switch l.(type) { + case gemini.LineLink: + link := l.(gemini.LineLink) + url := html.EscapeString(link.URL) + name := html.EscapeString(link.Name) + if name == "" { + name = url + } + fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) + case gemini.LinePreformattingToggle: + pre = !pre + if pre { + fmt.Fprint(&b, "<pre>\n") + } else { + fmt.Fprint(&b, "</pre>\n") + } + case gemini.LinePreformattedText: + text := string(l.(gemini.LinePreformattedText)) + fmt.Fprintf(&b, "%s\n", html.EscapeString(text)) + case gemini.LineHeading1: + text := string(l.(gemini.LineHeading1)) + fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text)) + case gemini.LineHeading2: + text := string(l.(gemini.LineHeading2)) + fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text)) + case gemini.LineHeading3: + text := string(l.(gemini.LineHeading3)) + fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text)) + case gemini.LineListItem: + text := string(l.(gemini.LineListItem)) + fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text)) + case gemini.LineQuote: + text := string(l.(gemini.LineQuote)) + fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text)) + case gemini.LineText: + text := string(l.(gemini.LineText)) + if text == "" { + fmt.Fprint(&b, "<br>\n") + } else { + fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text)) + } + } + } + if pre { + fmt.Fprint(&b, "</pre>\n") + } + if list { + fmt.Fprint(&b, "</ul>\n") + } + return b.String() +}
M
go.sum
→
go.sum
@@ -1,5 +1,5 @@
-git.sr.ht/~adnano/gmi v0.1.0-alpha.2 h1:5/wzImYT3mJmZ27lazJ8YAdpiVN3QNJruLX7PXOITeo= -git.sr.ht/~adnano/gmi v0.1.0-alpha.2/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= +git.sr.ht/~adnano/go-gemini v0.1.1 h1:g6OwUxLviy6dkPiuW2eRQP5Fow412vUsKmKYbCr2H9U= +git.sr.ht/~adnano/go-gemini v0.1.1/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
M
http.go
→
http.go
@@ -3,7 +3,7 @@
import ( "bytes" "database/sql" - "git.sr.ht/~adnano/gmi" + gmi "git.sr.ht/~adnano/go-gemini" "github.com/gorilla/handlers" "github.com/gorilla/sessions" _ "github.com/mattn/go-sqlite3"@@ -355,7 +355,7 @@ return
} file, _ := os.Open(fileName) - htmlString := gmi.Parse(file).HTML() + htmlString := textToHTML(gmi.Parse(file)) data := struct { SiteBody template.HTML PageTitle string