template and data dirs

drew/sql-it
Drew Bednar 7 months ago
parent a51cc3903f
commit 14882e1add

@ -1,2 +1,3 @@
gowiki/wiki
gowiki/*.txt
gowiki/gowiki
*.txt

@ -1 +0,0 @@
This is my fixed test page. Work!

@ -1 +0,0 @@
This is a test page for page two.

@ -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
}

@ -0,0 +1,3 @@
module gowiki
go 1.21.0

@ -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 {

Loading…
Cancel
Save