gemini-homepage

Gemini homepage for knutsen.co
git clone git://git.knutsen.co/gemini-homepage
Log | Files | Refs | LICENSE

commit 4323df1f1a60b6942891d7be2c37641c99b10d73
parent c0620bcc10ed46a7b65e341985d107b5ebb5631f
Author: Demonstrandum <moi@knutsen.co>
Date:   Sun,  7 Feb 2021 17:56:47 +0000

Added facility for users to add their own pages.

Diffstat:
Mmain.go | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Ausers/frost/index.gmi | 3+++
Ausers/samuel/hello.gmi | 5+++++
Ausers/samuel/index.gmi | 7+++++++
4 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/main.go b/main.go @@ -3,6 +3,7 @@ package main import ("os"; "io"; "fmt"; "log") import ("os/exec"; "time"; "bufio") import "strings" +import "path/filepath" import "text/template" import "github.com/pitr/gig" @@ -10,15 +11,6 @@ import "github.com/pitr/gig" var GEMINI_CERT = os.Getenv("GEMINI_CERT") var GEMINI_KEY = os.Getenv("GEMINI_KEY") -type Template struct { - templates *template.Template -} - -func (t *Template) Render(writer io.Writer, name string, - data interface{}, c gig.Context) error { - return t.templates.ExecuteTemplate(writer, name, data) -} - type DefaultTemplate struct { Date time.Time Fortune string @@ -64,32 +56,87 @@ func NewDefaultTemplate() (t DefaultTemplate) { return } +func walkTemplates(roots ...string) map[string] *template.Template { + templates := make(map[string] *template.Template) + for _, root := range roots { + err := filepath.Walk(root, + func(path string, info os.FileInfo, err error) error { + if path[len(path) - 4:] == ".gmi" { + templates[path] = template.Must(template.ParseFiles(path)) + if err != nil { + log.Println(err) + } + } + return err + }) + if err != nil { + panic(err) + } + } + + for path, t := range templates { + log.Printf("<Template name=%s> %s\n", t.Name(), path) + } + return templates +} + +func Template(c gig.Context, templates map[string] *template.Template, + path string, data interface{}) (err error) { + res := c.Response() + + t, ok := templates[path] + if !ok { return gig.ErrNotFound } + err = res.WriteHeader(gig.StatusSuccess, gig.MIMETextGemini); + if err != nil { return } + + return t.ExecuteTemplate(res, t.Name(), data) +} + func main() { fmt.Println("[~] CERT:", GEMINI_CERT) fmt.Println("[~] KEY:", GEMINI_KEY) s := gig.Default() - s.Renderer = &Template{ - templates: template.Must(template.ParseGlob("views/*.gmi")), - } + + templates := walkTemplates("./views", "./users") defaultView := func (file string) (func(gig.Context) error) { return func(c gig.Context) error { defaultTemplate := NewDefaultTemplate() - return c.Render(file + ".gmi", defaultTemplate); + return Template(c, templates, "views/" + file + ".gmi", + defaultTemplate); } } - paramView := func(param string) (func(gig.Context) error) { + userView := func (name string, file string) (func(gig.Context) error) { return func(c gig.Context) error { - return defaultView(c.Param("path"))(c) + defaultTemplate := NewDefaultTemplate() + return Template(c, templates, "users/" + name + "/" + file + ".gmi", + defaultTemplate); } } s.Static("/static", "static") s.Handle("/", defaultView("index")) - s.Handle("/~:name/:path", paramView("path")) - s.Handle("/:path", paramView("path")) + + user := s.Group("/~:name") + { + fileView := func(file string) (func(gig.Context) error) { + return func(c gig.Context) error { + return userView(c.Param("name"), file)(c) + } + } + + user.Handle("", fileView("index")) + user.Handle("/", fileView("index")) + user.Handle("/:path", func(c gig.Context) error { + return fileView(c.Param("path"))(c) + }) + } + + s.Handle("/:path", func(c gig.Context) error { + return defaultView(c.Param("path"))(c) + }) s.Run(GEMINI_CERT, GEMINI_KEY) } diff --git a/users/frost/index.gmi b/users/frost/index.gmi @@ -0,0 +1,3 @@ +# Home of Sam Frost + +Epic. diff --git a/users/samuel/hello.gmi b/users/samuel/hello.gmi @@ -0,0 +1,5 @@ +# Hello, World! + +## Your fortune + +{{ .Fortune }} diff --git a/users/samuel/index.gmi b/users/samuel/index.gmi @@ -0,0 +1,7 @@ +# Samuel's Page + +Personal pages for ~samuel. + +## Index + +=> /~samuel/hello Hello!