support index.htm and index.html

master
nate smith 2019-09-09 16:08:01 -05:00
parent a49f0f6a46
commit f46df4e6d9
1 changed files with 33 additions and 2 deletions

35
main.go
View File

@ -13,6 +13,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -121,9 +122,35 @@ func news() []NewsEntry {
return entries 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 { func pageTitleFor(username string) string {
pageTitleRe := regexp.MustCompile(`<title[^>]*>(.*)</title>`) pageTitleRe := regexp.MustCompile(`<title[^>]*>(.*)</title>`)
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) content, err := ioutil.ReadFile(indexPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to read %q: %v\n", indexPath, err) 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 { 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) indexFile, err := os.Open(indexPath)
if err != nil { if err != nil {
return false return false