39 lines
873 B
Go
39 lines
873 B
Go
// Package handlers provides HTTP handlers for the file system.
|
|
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"git.max-richter.dev/max/marka/server/internal/fsx"
|
|
"git.max-richter.dev/max/marka/server/internal/httpx"
|
|
)
|
|
|
|
type File struct{ root string }
|
|
|
|
func NewFile(root string) http.Handler { return &File{root: root} }
|
|
|
|
func (h *File) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
reqPath := r.URL.Path
|
|
if reqPath == "" {
|
|
reqPath = "/"
|
|
}
|
|
cleanRel, err := fsx.SafeRel(h.root, reqPath)
|
|
if err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
target := filepath.Join(h.root, filepath.FromSlash(cleanRel))
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
h.get(w, r, target)
|
|
case http.MethodPost:
|
|
h.post(w, r, target)
|
|
default:
|
|
httpx.WriteError(w, http.StatusMethodNotAllowed, errors.New("method not allowed"))
|
|
}
|
|
}
|
|
|