feat: add file tree to /tree

This commit is contained in:
Lucas Barbieri 2024-08-19 16:11:25 -03:00
parent deada49d46
commit a84d275990

15
main.go
View File

@ -6,6 +6,8 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings"
"time" "time"
) )
@ -18,11 +20,22 @@ var url string = os.Getenv("URL")
func main() { func main() {
http.HandleFunc("/upload", uploadHandler) http.HandleFunc("/upload", uploadHandler)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(filesDir)))) http.Handle("/tree/", http.StripPrefix("/tree", http.FileServer(http.Dir(filesDir))))
http.HandleFunc("/", fileHandler)
log.Printf("Server running on port %s\n", port) log.Printf("Server running on port %s\n", port)
log.Fatal(http.ListenAndServe(port, nil)) log.Fatal(http.ListenAndServe(port, nil))
} }
func fileHandler(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(filesDir, strings.TrimPrefix(r.URL.Path, "/"))
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path)
} else {
http.NotFound(w, r)
}
}
func uploadHandler(w http.ResponseWriter, r *http.Request) { func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
return return