1
0
Fork 0

More and better error logging.

This commit is contained in:
Solderpunk 2020-07-01 22:58:07 +02:00
parent f7e588dfae
commit 3cd4a072fd
2 changed files with 14 additions and 6 deletions

View file

@ -12,11 +12,11 @@ import (
"strings"
)
func generateDirectoryListing(URL *url.URL, path string, config Config) string {
func generateDirectoryListing(URL *url.URL, path string, config Config) (string, error) {
var listing string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
return listing, err
}
listing = "# Directory listing\n\n"
// Override with .mollyhead file
@ -24,9 +24,10 @@ func generateDirectoryListing(URL *url.URL, path string, config Config) string {
_, err = os.Stat(header_path)
if err == nil {
header, err := ioutil.ReadFile(header_path)
if err == nil {
listing = string(header)
if err != nil {
return listing, err
}
listing = string(header)
}
// Do "up" link first
if URL.Path != "/" {
@ -62,7 +63,7 @@ func generateDirectoryListing(URL *url.URL, path string, config Config) string {
}
listing += fmt.Sprintf("=> %s %s\n", url.PathEscape(file.Name()), generatePrettyFileLabel(file, path, config))
}
return listing
return listing, nil
}
func generatePrettyFileLabel(info os.FileInfo, path string, config Config) string {

View file

@ -277,9 +277,16 @@ func serveDirectory(URL *url.URL, path string, log *LogEntry, conn net.Conn, con
serveFile(index_path, log, conn, config, errorLog)
// Serve a generated listing
} else {
listing, err := generateDirectoryListing(URL, path, config)
if err != nil {
errorLog.Println("Error generating listing for directory " + path + ": " + err.Error())
conn.Write([]byte("40 Server error!\r\n"))
log.Status = 40
return
}
conn.Write([]byte("20 text/gemini\r\n"))
log.Status = 20
conn.Write([]byte(generateDirectoryListing(URL, path, config)))
conn.Write([]byte(listing))
}
}