From 14882e1addbfc1022848b7c50ee8d0fd41a9412e Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 23 Jun 2024 09:55:11 -0400 Subject: [PATCH] template and data dirs --- go-web-app/.gitignore | 3 ++- go-web-app/gowiki/TestPage.txt | 1 - go-web-app/gowiki/TestPage2.txt | 1 - go-web-app/gowiki/common.go | 12 ++++++++++++ go-web-app/gowiki/{test.txt => data/.gitkeep} | 0 go-web-app/gowiki/go.mod | 3 +++ go-web-app/gowiki/wiki.go | 19 ++++++++++++++++--- 7 files changed, 33 insertions(+), 6 deletions(-) delete mode 100644 go-web-app/gowiki/TestPage.txt delete mode 100644 go-web-app/gowiki/TestPage2.txt create mode 100644 go-web-app/gowiki/common.go rename go-web-app/gowiki/{test.txt => data/.gitkeep} (100%) create mode 100644 go-web-app/gowiki/go.mod diff --git a/go-web-app/.gitignore b/go-web-app/.gitignore index bcc6ba6..1885eea 100644 --- a/go-web-app/.gitignore +++ b/go-web-app/.gitignore @@ -1,2 +1,3 @@ gowiki/wiki -gowiki/*.txt \ No newline at end of file +gowiki/gowiki +*.txt \ No newline at end of file diff --git a/go-web-app/gowiki/TestPage.txt b/go-web-app/gowiki/TestPage.txt deleted file mode 100644 index 26295e5..0000000 --- a/go-web-app/gowiki/TestPage.txt +++ /dev/null @@ -1 +0,0 @@ -This is my fixed test page. Work! \ No newline at end of file diff --git a/go-web-app/gowiki/TestPage2.txt b/go-web-app/gowiki/TestPage2.txt deleted file mode 100644 index 6d8fbf7..0000000 --- a/go-web-app/gowiki/TestPage2.txt +++ /dev/null @@ -1 +0,0 @@ -This is a test page for page two. \ No newline at end of file diff --git a/go-web-app/gowiki/common.go b/go-web-app/gowiki/common.go new file mode 100644 index 0000000..833574e --- /dev/null +++ b/go-web-app/gowiki/common.go @@ -0,0 +1,12 @@ +package main + +import "os" + +func GetDefaultEnv(key, default_value string) string { + // TODO figure out how to write unit test for this How would you wring + value := os.Getenv(key) + if value == "" { + value = default_value + } + return value +} diff --git a/go-web-app/gowiki/test.txt b/go-web-app/gowiki/data/.gitkeep similarity index 100% rename from go-web-app/gowiki/test.txt rename to go-web-app/gowiki/data/.gitkeep diff --git a/go-web-app/gowiki/go.mod b/go-web-app/gowiki/go.mod new file mode 100644 index 0000000..f9391db --- /dev/null +++ b/go-web-app/gowiki/go.mod @@ -0,0 +1,3 @@ +module gowiki + +go 1.21.0 diff --git a/go-web-app/gowiki/wiki.go b/go-web-app/gowiki/wiki.go index f1d325d..5e89307 100644 --- a/go-web-app/gowiki/wiki.go +++ b/go-web-app/gowiki/wiki.go @@ -8,15 +8,28 @@ import ( "text/template" ) +var template_dir string = GetDefaultEnv("GOWIKI_TEMPLATE_DIR", "./tmpl") +var data_dir string = GetDefaultEnv("GOWIKI_DATA_DIR", "./data") + // Template caching // var templates = template.Must(template.ParseFiles("edit.html", "view.html")) -var templates = template.Must(template.ParseGlob("./tmpl/*.html")) +var templates = template.Must(template.ParseGlob(template_dir + "/*.html")) // var templates = template.Must(template.ParseFiles("edit.html", "view.html")) // Validation to prevent abitrary paths var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$") +// // Retreives and environment variable or supplies the specified default +// func GetDefaultEnv(key, default_value string) string { +// // TODO figure out how to write unit test for this How would you wring +// value := os.Getenv(key) +// if value == "" { +// value = default_value +// } +// return value +// } + // A wiki consists of a series of interconnected pages, each of which has a title and a body type Page struct { Title string @@ -26,7 +39,7 @@ type Page struct { // save Saves a page to disk. func (p *Page) save() error { - filename := p.Title + ".txt" + filename := data_dir + "/" + p.Title + ".txt" return os.WriteFile(filename, p.Body, 0600) } @@ -46,7 +59,7 @@ func (p *Page) save() error { // file, and returns a pointer to a Page struct containing the title and body. // If an error occurs during reading, it returns the error. func loadPage(title string) (*Page, error) { - filename := title + ".txt" + filename := data_dir + "/" + title + ".txt" body, err := os.ReadFile(filename) if err != nil {