Skip to content

Commit

Permalink
feat: add tree to /tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuxas committed Aug 19, 2024
1 parent deada49 commit e2d451e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)

Expand All @@ -18,11 +20,22 @@ var url string = os.Getenv("URL")

func main() {
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.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) {
if r.Method != http.MethodPost {
return
Expand Down

0 comments on commit e2d451e

Please sign in to comment.