flounder

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

commit 9cae990bcac0b7990cfe3f57f1d5fc5c14969e3e
parent 664d07956cd4b2c58ef4bb4414ce7a324555d3f3
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu, 22 Oct 2020 17:07:32 -0700

add some site templates

Diffstat:
Mconfig.go | 3++-
Mflounder.toml | 2+-
Mhttp.go | 45++++++++++++++++++++++++++++++++-------------
Atemplates/edit_file.html | 8++++++++
Mtemplates/login.html | 2+-
Atemplates/my_site.html | 43+++++++++++++++++++++++++++++++++++++++++++
6 files changed, 87 insertions(+), 16 deletions(-)

diff --git a/config.go b/config.go @@ -10,7 +10,8 @@ type Config struct { SiteTitle string Debug bool SecretKey string - PasswdFile string + DBFile string + PasswdFile string // TODO remove } func getConfig(filename string) (Config, error) { diff --git a/flounder.toml b/flounder.toml @@ -1,4 +1,4 @@ SiteTitle="­čÉčflounder" RootDomain="localhost" FilesDirectory="./files" -PasswdFile="accounts.htpasswd" +DBFile="./flounder.db" diff --git a/http.go b/http.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "html/template" "log" "net/http" @@ -51,30 +50,41 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { } func editFileHandler(w http.ResponseWriter, r *http.Request) { - // get vs post - // read file content - authUser := "alex" - files, _ := getUserFiles(authUser) - for _, file := range files { - fmt.Fprintf(w, "%s\n", file.Name) + // read file content. create if dne + // authUser := "alex" + data := struct { + FileName string + FileText string + PageTitle string + }{"filename", "filetext", c.SiteTitle} + err := t.ExecuteTemplate(w, "edit_file.html", data) + if err != nil { + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) + return } } func mySiteHandler(w http.ResponseWriter, r *http.Request) { authUser := "alex" + // check auth files, _ := getUserFiles(authUser) - for _, file := range files { - fmt.Fprintf(w, "%s\n", file.Name) - } + data := struct { + Domain string + PageTitle string + AuthUser string + Files []*File + }{c.RootDomain, c.SiteTitle, authUser, files} + _ = t.ExecuteTemplate(w, "my_site.html", data) } func loginHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { // show page data := struct { - Error error + Error string PageTitle string - }{nil, c.SiteTitle} + }{"", c.SiteTitle} err := t.ExecuteTemplate(w, "login.html", data) if err != nil { log.Println(err) @@ -90,7 +100,16 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { log.Println("logged in") // redirect home } else { - log.Println(err) + data := struct { + Error string + PageTitle string + }{"Invalid login or password", c.SiteTitle} + err := t.ExecuteTemplate(w, "login.html", data) + if err != nil { + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) + return + } } // create session // redirect home diff --git a/templates/edit_file.html b/templates/edit_file.html @@ -0,0 +1,8 @@ +{{template "header" .}} +<h2>Editing {{.FileName}}</h2> +<form id="edit-form" action="/edit/{{.FileName}}" method="POST"> + <textarea rows="20" name="file_text" id="editor">{{.FileText}}</textarea> + <br> + <input type="submit" value="Save file" class="button"> +</form> +{{template "footer" .}} diff --git a/templates/login.html b/templates/login.html @@ -12,7 +12,7 @@ </p> {{ if .Error }} <div class="error"> - <p>{{.}}</p> + <p>{{.Error}}</p> </div> {{ end}} <p> diff --git a/templates/my_site.html b/templates/my_site.html @@ -0,0 +1,43 @@ +{{$domain := .Domain}} +{{$authUser := .AuthUser}} +{{template "header" .}} +<h1>Managing + <a href="https://{{$authUser}}.{{$domain}}"> + {{.AuthUser}}.{{$domain}} + </a> +</h1> +{{template "nav.html" .}} +<h3>Your files:</h3> +{{ range .Files }} +<div> + <a href="https://{{$authUser}}.{{$domain}}/{{.Name}}"> + {{ .Name }}</a> + <a href="/edit/{{.Name}}">edit</a> + <form action="/delete/{{.Name}}" method="POST" class="inline"> + <input + class="button" + type="submit" + onclick="return confirm('Are you sure you want to delete this file?');" + value="delete" + /> + </form> +</div> +{{ end }} +<h3>Create file by name:</h3> +<noscript>Create a new page by going to /edit/[filename]</noscript> +<input id="edit_new" size=32 placeholder="New filename, e.g. newfile.gmi" /> +<a href="#" id="create_new">Edit new page</a><br> +<script> + var input = document.getElementById('edit_new') + var create = document.getElementById('create_new') + create.href = '/edit/' + input.value + input.onchange = input.onkeyup = function () { + create.href = '/edit/' + input.value + } +</script> +<br /> +<form action="/upload" enctype="multipart/form-data" method="POST"> + <input type="file" id="myFile" name="file" multiple /> + <input type="submit" value="Upload file" class="button" /> +</form> +{{template "footer" .}}