add some site templates
alex wennerberg alex@alexwennerberg.com
Thu, 22 Oct 2020 17:07:32 -0700
6 files changed,
87 insertions(+),
16 deletions(-)
M
flounder.toml
→
flounder.toml
@@ -1,4 +1,4 @@
SiteTitle="🐟flounder" RootDomain="localhost" FilesDirectory="./files" -PasswdFile="accounts.htpasswd" +DBFile="./flounder.db"
M
http.go
→
http.go
@@ -1,7 +1,6 @@
package main import ( - "fmt" "html/template" "log" "net/http"@@ -51,30 +50,41 @@
} 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 @@ if err == nil {
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
A
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" .}}
M
templates/login.html
→
templates/login.html
@@ -12,7 +12,7 @@ <input id="password" name="password" size="32" type="password" value="" />
</p> {{ if .Error }} <div class="error"> - <p>{{.}}</p> + <p>{{.Error}}</p> </div> {{ end}} <p>
A
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" .}}