More stdlib

drew/sql-it
Drew Bednar 7 months ago
parent fcce8d7343
commit e28d2c52b7

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"regexp" "regexp"
"text/template" "text/template"
) )
@ -21,16 +22,6 @@ var templates = template.Must(template.ParseGlob(template_dir + "/*.html"))
// Validation to prevent abitrary paths // Validation to prevent abitrary paths
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$") 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 // A wiki consists of a series of interconnected pages, each of which has a title and a body
type Page struct { type Page struct {
Title string Title string
@ -40,8 +31,10 @@ type Page struct {
// save Saves a page to disk. // save Saves a page to disk.
func (p *Page) save() error { func (p *Page) save() error {
filename := data_dir + "/" + p.Title + ".txt" // filename := data_dir + "/" + p.Title + ".txt"
return os.WriteFile(filename, p.Body, 0600) filename := p.Title + ".txt"
fullPath := filepath.Join(data_dir, filename)
return os.WriteFile(fullPath, p.Body, 0600)
} }
// Not neccessary after makeHandler implemented this. // 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. // file, and returns a pointer to a Page struct containing the title and body.
// If an error occurs during reading, it returns the error. // If an error occurs during reading, it returns the error.
func loadPage(title string) (*Page, error) { func loadPage(title string) (*Page, error) {
filename := data_dir + "/" + title + ".txt" // filename := data_dir + "/" + title + ".txt"
body, err := os.ReadFile(filename) fullPath := filepath.Join(data_dir, title+".txt")
body, err := os.ReadFile(fullPath)
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save