flounder

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

commit 6888b8e79940eb9e79dc8edc4b55998e94d46911
parent 7052b40538b59228f4ad763396682b8989ec0ceb
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 24 Oct 2020 10:23:17 -0700

Create edit file function

Diffstat:
MREADME.md | 2++
Mhttp.go | 23+++++++++++++++++++----
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md @@ -7,6 +7,8 @@ Designed to help make the Gemini ecosystem more accessible. ## Hosting +Flounder is designed to be very simple to host, and should be able to be relatively easily run by a single person. + Very simple to host -- a single binary with a gemini server, http server included. ## Customizing diff --git a/http.go b/http.go @@ -4,6 +4,7 @@ import ( "git.sr.ht/~adnano/gmi" "github.com/gorilla/handlers" "html/template" + "io/ioutil" "log" "net/http" "os" @@ -61,21 +62,35 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { } func editFileHandler(w http.ResponseWriter, r *http.Request) { - // read file content. create if dne - // authUser := "alex" + authUser := "alex" + fileName := filepath.Clean(r.URL.Path[len("/edit/"):]) + filePath := path.Join(c.FilesDirectory, authUser, fileName) if r.Method == "GET" { + f, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644) + defer f.Close() + fileBytes, err := ioutil.ReadAll(f) + if err != nil { + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) + return + } data := struct { FileName string FileText string PageTitle string - }{"filename", "filetext", c.SiteTitle} - err := t.ExecuteTemplate(w, "edit_file.html", data) + }{fileName, string(fileBytes), c.SiteTitle} + err = t.ExecuteTemplate(w, "edit_file.html", data) if err != nil { log.Println(err) renderError(w, InternalServerErrorMsg, 500) return } } else if r.Method == "POST" { + // get post body + r.ParseForm() + fileText := r.Form.Get("file_text") + ioutil.WriteFile(filePath, []byte(fileText), 0644) + http.Redirect(w, r, "/my_site", 302) } }