diff --git a/go-web-app/gowiki/cmd/gowiki/main.go b/go-web-app/gowiki/cmd/gowiki/main.go index d5b8e19..20640f8 100644 --- a/go-web-app/gowiki/cmd/gowiki/main.go +++ b/go-web-app/gowiki/cmd/gowiki/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "path/filepath" "regexp" "text/template" ) @@ -21,16 +22,6 @@ var templates = template.Must(template.ParseGlob(template_dir + "/*.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 @@ -40,8 +31,10 @@ type Page struct { // save Saves a page to disk. func (p *Page) save() error { - filename := data_dir + "/" + p.Title + ".txt" - return os.WriteFile(filename, p.Body, 0600) + // filename := data_dir + "/" + p.Title + ".txt" + filename := p.Title + ".txt" + fullPath := filepath.Join(data_dir, filename) + return os.WriteFile(fullPath, p.Body, 0600) } // Not neccessary after makeHandler implemented this. @@ -60,8 +53,9 @@ 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 := data_dir + "/" + title + ".txt" - body, err := os.ReadFile(filename) + // filename := data_dir + "/" + title + ".txt" + fullPath := filepath.Join(data_dir, title+".txt") + body, err := os.ReadFile(fullPath) if err != nil { return nil, err