all repos — flounder @ 19944763fc195d2db224147841ebcbf2aafb435d

A small site builder for the Gemini protocol

add simple html templates
alex wennerberg alex@alexwennerberg.com
Tue, 20 Oct 2020 22:10:54 -0700
commit

19944763fc195d2db224147841ebcbf2aafb435d

parent

f282a3b485f2bbe813913fc8e61edc5cc6370161

M config.goconfig.go

@@ -5,9 +5,10 @@ "github.com/BurntSushi/toml"

) type Config struct { - DbURI string FilesPath string RootDomain string + SiteTitle string + Debug bool SecretKey string }
M config.tomlconfig.toml

@@ -1,1 +1,4 @@

-key=value +site_title="flounder" +root_domain="localhost:8080" +debug=true +
A flounder.toml

@@ -0,0 +1,3 @@

+site_title="flounder" +RootDomain="localhost:8080" +
M go.sumgo.sum

@@ -2,5 +2,7 @@ git.sr.ht/~adnano/gmi v0.1.0-alpha.1 h1:5sha5ucev32U95drEEaPw9rm6hGWJtL8y5HViQ3VDJQ=

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=
M http.gohttp.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 @@ fmt.Fprintf(w, "%s\n", file.Name)

} } -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)
M main.gomain.go

@@ -1,6 +1,7 @@

package main import ( + "flag" "io/ioutil" "log" "os"

@@ -62,11 +63,12 @@ return result, nil

} 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)) }
A templates/footer.html

@@ -0,0 +1,5 @@

+{{ define "footer"}} +</main> +</body> +</html> +{{ end }}
A 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 }}
A 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 }}
A 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" .}}