♻️ Refactored static HTTP delivery

This commit is contained in:
Maxim Lebedev 2024-02-14 22:42:49 +06:00
parent 3e4b54cf92
commit 60d0304aa0
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 29 additions and 24 deletions

View File

@ -27,6 +27,7 @@ import (
servercase "source.toby3d.me/toby3d/home/internal/server/usecase" servercase "source.toby3d.me/toby3d/home/internal/server/usecase"
sitefsrepo "source.toby3d.me/toby3d/home/internal/site/repository/fs" sitefsrepo "source.toby3d.me/toby3d/home/internal/site/repository/fs"
siteucase "source.toby3d.me/toby3d/home/internal/site/usecase" siteucase "source.toby3d.me/toby3d/home/internal/site/usecase"
statichttpdelivery "source.toby3d.me/toby3d/home/internal/static/delivery/http"
staticfsrepo "source.toby3d.me/toby3d/home/internal/static/repository/fs" staticfsrepo "source.toby3d.me/toby3d/home/internal/static/repository/fs"
staticucase "source.toby3d.me/toby3d/home/internal/static/usecase" staticucase "source.toby3d.me/toby3d/home/internal/static/usecase"
themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs" themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs"
@ -63,13 +64,17 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
webfingerer := webfingerucase.NewWebFingerUseCase(sites) webfingerer := webfingerucase.NewWebFingerUseCase(sites)
webfingerHandler := webfingerhttpdelivery.NewHandler(webfingerer) webfingerHandler := webfingerhttpdelivery.NewHandler(webfingerer)
entryHandler := entryhttpdelivery.NewHandler(siter, entrier, themer) entryHandler := entryhttpdelivery.NewHandler(siter, entrier, themer)
staticHandler := statichttpdelivery.NewHandler(staticer)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// NOTE(toby3d): any static file is public and unprotected by // NOTE(toby3d): any file in $HOME_STATIC_DIR is public and
// design, so it's safe to search it first before deep down to // unprotected by design, so it's safe to search it first before
// any page or it's resource which might be protected by // deep down to any page or it's resource which might be
// middleware or something else. // protected by middleware or something else.
if static, err := staticer.Do(r.Context(), r.URL.Path); err == nil { if handler, err := staticHandler.Handle(r.Context(), r.URL.Path); err == nil {
http.ServeContent(w, r, static.Name(), static.ModTime(), static) handler.ServeHTTP(w, r)
return
}
return return
} }

View File

@ -1,11 +1,9 @@
package http package http
import ( import (
"errors" "context"
"io/fs" "fmt"
"net/http" "net/http"
"path"
"strings"
"source.toby3d.me/toby3d/home/internal/static" "source.toby3d.me/toby3d/home/internal/static"
) )
@ -18,17 +16,13 @@ func NewHandler(static static.UseCase) *Handler {
return &Handler{static: static} return &Handler{static: static}
} }
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) Handle(ctx context.Context, path string) (http.Handler, error) {
s, err := h.static.Do(r.Context(), strings.TrimPrefix(path.Clean(r.URL.Path), "/")) static, err := h.static.Do(ctx, path)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrNotExist) { return nil, fmt.Errorf("cannot handle static: %w", err)
http.NotFound(w, r)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
} }
http.ServeContent(w, r, s.Name(), s.ModTime(), s) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, static.Name(), static.ModTime(), static)
}), nil
} }

View File

@ -1,6 +1,7 @@
package http_test package http_test
import ( import (
"context"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
@ -18,11 +19,16 @@ func TestHandler_ServeHTTP(t *testing.T) {
t.Parallel() t.Parallel()
req := httptest.NewRequest(http.MethodGet, "/robots.txt", nil) req := httptest.NewRequest(http.MethodGet, "/robots.txt", nil)
w := httptest.NewRecorder()
testStatic := domain.NewStatic(strings.NewReader("User-agent: *\nAllow: /"), time.Now().UTC(), "robots.txt") testStatic := domain.NewStatic(strings.NewReader("User-agent: *\nAllow: /"), time.Now().UTC(), "robots.txt")
delivery.NewHandler(usecase.NewStaticUseCase(repository.NewStubStaticRepository(nil, testStatic, false))). staticService := usecase.NewStaticUseCase(repository.NewStubStaticRepository(nil, testStatic, false))
ServeHTTP(w, req)
handler, err := delivery.NewHandler(staticService).Handle(context.Background(), req.URL.Path)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result() resp := w.Result()
if expect := http.StatusOK; resp.StatusCode != expect { if expect := http.StatusOK; resp.StatusCode != expect {