flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 19944763fc195d2db224147841ebcbf2aafb435d
parent f282a3b485f2bbe813913fc8e61edc5cc6370161
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Tue, 20 Oct 2020 22:10:54 -0700

add simple html templates

Diffstat:
Mconfig.go | 3++-
Mconfig.toml | 5++++-
Aflounder.toml | 3+++
Mgo.sum | 2++
Mhttp.go | 40++++++++++++++++++++++++++++++++++------
Mmain.go | 14++++++++------
Atemplates/footer.html | 5+++++
Atemplates/head.html | 8++++++++
Atemplates/header.html | 12++++++++++++
Atemplates/index.html | 19+++++++++++++++++++
Atemplates/nav.html | 0
11 files changed, 97 insertions(+), 14 deletions(-)

diff --git a/config.go b/config.go @@ -5,9 +5,10 @@ import ( ) type Config struct { - DbURI string FilesPath string RootDomain string + SiteTitle string + Debug bool SecretKey string } diff --git a/config.toml b/config.toml @@ -1 +1,4 @@ -key=value +site_title="flounder" +root_domain="localhost:8080" +debug=true + diff --git a/flounder.toml b/flounder.toml @@ -0,0 +1,3 @@ +site_title="flounder" +RootDomain="localhost:8080" + diff --git a/go.sum b/go.sum @@ -2,5 +2,7 @@ git.sr.ht/~adnano/gmi v0.1.0-alpha.1 h1:5sha5ucev32U95drEEaPw9rm6hGWJtL8y5HViQ3V git.sr.ht/~adnano/gmi v0.1.0-alpha.1/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/mattn/go-sqlite3 v1.14.4 h1:4rQjbDxdu9fSgI/r3KN72G3c2goxknAqHHgPWWs8UlI= github.com/mattn/go-sqlite3 v1.14.4/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= diff --git a/http.go b/http.go @@ -2,15 +2,38 @@ package main import ( "fmt" + "html/template" "log" "net/http" ) -func indexHandler(w http.ResponseWriter, r *http.Request) { - indexFiles, _ := getIndexFiles() - for _, file := range indexFiles { - fmt.Fprintf(w, "%s\n", file.Name) +var t *template.Template + +type IndexHandler struct { + Domain string + SiteTitle string +} + +func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + indexFiles, err := getIndexFiles() + if err != nil { + log.Fatal(err) } + allUsers, err := getUsers() + if err != nil { + log.Fatal(err) + } + data := struct { + Domain string + PageTitle string + Files []*File + Users []string + }{h.Domain, h.SiteTitle, indexFiles, allUsers} + err = t.ExecuteTemplate(w, "index.html", data) + if err != nil { + log.Fatal(err) + } + } func editFileHandler(w http.ResponseWriter, r *http.Request) { @@ -31,8 +54,13 @@ func mySiteHandler(w http.ResponseWriter, r *http.Request) { } } -func runHTTPServer() { - http.HandleFunc("/", indexHandler) +func runHTTPServer(config *Config) { + var err error + t, err = template.ParseGlob("./templates/*.html") // TODO make template dir configruable + if err != nil { + log.Fatal(err) + } + http.Handle("/", &IndexHandler{config.RootDomain, config.SiteTitle}) http.HandleFunc("/my_site", mySiteHandler) http.HandleFunc("/edit/", editFileHandler) // http.HandleFunc("/delete/", deleteFileHandler) diff --git a/main.go b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "io/ioutil" "log" "os" @@ -62,11 +63,12 @@ func getUserFiles(user string) ([]*File, error) { } func main() { - config := Config{} - // http functions - // go serve gemini - // go serve http -- not - // runHTTPServer() - runGeminiServer(&config) + configPath := flag.String("c", "flounder.toml", "path to config file") + config, err := getConfig(*configPath) + if err != nil { + log.Fatal(err) + } + runHTTPServer(&config) + // runGeminiServer(&config) // go log.Fatal(gmi.ListenAndServe(":8080", nil)) } diff --git a/templates/footer.html b/templates/footer.html @@ -0,0 +1,5 @@ +{{ define "footer"}} +</main> +</body> +</html> +{{ end }} diff --git a/templates/head.html b/templates/head.html @@ -0,0 +1,8 @@ +{{ define "head"}} + <head> + <meta charset="utf-8" /> + <title>{{.SiteTitle }}<title> + <meta name="viewport" content="width=device-width" /> + <link rel="stylesheet" type="text/css" href="/static/style.css" /> + </head> +{{ end }} diff --git a/templates/header.html b/templates/header.html @@ -0,0 +1,12 @@ +{{ define "header"}} +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8" /> + <title>{{.PageTitle }}</title> + <meta name="viewport" content="width=device-width" /> + <link rel="stylesheet" type="text/css" href="/static/style.css" /> + </head> + <body> + <main> +{{ end }} diff --git a/templates/index.html b/templates/index.html @@ -0,0 +1,19 @@ +{{$domain := .Domain}} +{{template "header" .}} +<h1>{{.PageTitle}}</h1> +<h2>All users:</h2> +{{ range .Users}} +<a href="https://{{.}}.{{$domain}}" class='person-link'>{{.}}</a> +{{end}} +<h2>Recently updated files:</h2> +{{ range .Files }} +<div> + <a href="https://{{.Creator}}.{{$domain}}" class='person-link'> + {{ .Creator }}</a> + <em>{{.UpdatedTime}}</em> + <a href="https://{{.Creator}}.{{$domain}}/{{.Name}}"> + {{ .Name}} + </a> +</div> +{{end}} +{{template "footer" .}} diff --git a/templates/nav.html b/templates/nav.html