Starting work on the actual website

This commit is contained in:
Caleb Gardner
2024-07-13 03:31:37 -05:00
parent e43409923c
commit 15dcc2928a
6 changed files with 97 additions and 13 deletions
+53
View File
@@ -0,0 +1,53 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
)
const fileElement = "<p><a href='https://darkstorm.tech/%v/'>%v</a></p>"
func filesRequest(w http.ResponseWriter, r *http.Request) {
partPath := filepath.Clean(r.URL.Path)
path := filepath.Join(*webRoot, partPath)
var pageContent string
fil, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
pageContent = "<p>404 Not Found</p>"
w.WriteHeader(http.StatusNotFound)
} else {
pageContent = "<p>Server error!</p>"
w.WriteHeader(http.StatusInternalServerError)
log.Println("error serving files:", err)
}
} else {
stat, _ := fil.Stat()
if stat.IsDir() {
var dirs []os.DirEntry
dirs, err = fil.ReadDir(-1)
if err != nil {
pageContent = "<p>Server error!</p>"
w.WriteHeader(http.StatusInternalServerError)
log.Println("error serving files:", err)
}
for _, f := range dirs {
if f.IsDir() {
continue
}
pageContent += fmt.Sprintf(fileElement, partPath, f.Name())
}
} else {
http.ServeFile(w, r, path)
return
}
}
if r.URL.Query().Get("contentOnly") == "true" {
w.Write([]byte(pageContent))
} else {
sendIndexWithContent(w, pageContent)
}
}