From f46df4e6d97200b3eb39b42a75c37894a540f742 Mon Sep 17 00:00:00 2001 From: nate smith Date: Mon, 9 Sep 2019 16:08:01 -0500 Subject: [PATCH] support index.htm and index.html --- main.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index e1fc44c..41301fe 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "bufio" "bytes" "encoding/json" + "errors" "fmt" "io/ioutil" "log" @@ -121,9 +122,35 @@ func news() []NewsEntry { return entries } +func indexPathFor(username string) (string, error) { + potentialPaths := []string{"index.html", "index.htm"} + indexPath := "" + errs := []error{} + for _, p := range potentialPaths { + fullPath := path.Join(homesDir(), username, "public_html", p) + _, staterr := os.Stat(fullPath) + if staterr != nil { + errs = append(errs, staterr) + } else { + indexPath = fullPath + break + } + } + + if indexPath == "" { + return "", errors.New(fmt.Sprintf("Failed to locate index file for %v; tried %v; encountered errors: %v", username, potentialPaths, errs)) + } + + return indexPath, nil +} + func pageTitleFor(username string) string { pageTitleRe := regexp.MustCompile(`]*>(.*)`) - indexPath := path.Join(homesDir(), username, "public_html", "index.html") + indexPath, err := indexPathFor(username) + if err != nil { + log.Print(err) + return "" + } content, err := ioutil.ReadFile(indexPath) if err != nil { fmt.Fprintf(os.Stderr, "failed to read %q: %v\n", indexPath, err) @@ -172,7 +199,11 @@ func mtimeFor(username string) int64 { } func detectDefaultPageFor(username string, defaultHTML []byte) bool { - indexPath := path.Join(homesDir(), username, "public_html", "index.html") + indexPath, err := indexPathFor(username) + if err != nil { + log.Print(err) + return false + } indexFile, err := os.Open(indexPath) if err != nil { return false