home/internal/static/delivery/http/static_http.go

35 lines
636 B
Go

package http
import (
"errors"
"io/fs"
"net/http"
"path"
"strings"
"source.toby3d.me/toby3d/home/internal/static"
)
type Handler struct {
static static.UseCase
}
func NewHandler(static static.UseCase) *Handler {
return &Handler{static: static}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s, err := h.static.Do(r.Context(), strings.TrimPrefix(path.Clean(r.URL.Path), "/"))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
http.NotFound(w, r)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
http.ServeContent(w, r, s.Name(), s.ModTime(), s)
}