diff --git a/cmd/pageship/pageship b/cmd/pageship/pageship new file mode 100755 index 0000000..acb4eb0 Binary files /dev/null and b/cmd/pageship/pageship differ diff --git a/internal/handler/site/context/context.go b/internal/handler/site/context.go similarity index 97% rename from internal/handler/site/context/context.go rename to internal/handler/site/context.go index edf7f71..8eabd1e 100644 --- a/internal/handler/site/context/context.go +++ b/internal/handler/site/context.go @@ -1,4 +1,4 @@ -package context +package site import ( "context" diff --git a/internal/handler/site/handler.go b/internal/handler/site/handler.go index 477a81b..08360e0 100644 --- a/internal/handler/site/handler.go +++ b/internal/handler/site/handler.go @@ -12,7 +12,6 @@ import ( "github.com/oursky/pageship/internal/cache" "github.com/oursky/pageship/internal/config" "github.com/oursky/pageship/internal/domain" - "github.com/oursky/pageship/internal/handler/site/middleware" "github.com/oursky/pageship/internal/httputil" "github.com/oursky/pageship/internal/models" "github.com/oursky/pageship/internal/site" @@ -26,7 +25,7 @@ const ( type HandlerConfig struct { HostPattern string - MiddlewaresFunc func(*cache.ContentCache) []middleware.Middleware + MiddlewaresFunc func(*cache.ContentCache) []Middleware ContentCacheMaxSize int64 } @@ -37,7 +36,7 @@ type Handler struct { siteResolver site.Resolver hostPattern *config.HostPattern cache *cache.Cache[*SiteHandler] - middlewares []middleware.Middleware + middlewares []Middleware contentCache *cache.ContentCache } diff --git a/internal/handler/site/middleware.go b/internal/handler/site/middleware.go new file mode 100644 index 0000000..473cfa5 --- /dev/null +++ b/internal/handler/site/middleware.go @@ -0,0 +1,16 @@ +package site + +import ( + "net/http" + + "github.com/oursky/pageship/internal/site" +) + +type Middleware func(*site.Descriptor, http.Handler) http.Handler + +func ApplyMiddleware(site *site.Descriptor, middlewares []Middleware, handler http.Handler) http.Handler { + for i := len(middlewares) - 1; i >= 0; i-- { + handler = middlewares[i](site, handler) + } + return handler +} diff --git a/internal/handler/site/middleware/canon.go b/internal/handler/site/middleware/canon.go index 7ee07ac..97cc55e 100644 --- a/internal/handler/site/middleware/canon.go +++ b/internal/handler/site/middleware/canon.go @@ -6,7 +6,7 @@ import ( "path" "strings" - "github.com/oursky/pageship/internal/handler/site/context" + siteContext "github.com/oursky/pageship/internal/handler/site" "github.com/oursky/pageship/internal/site" ) @@ -14,7 +14,7 @@ func CanonicalizePath(site *site.Descriptor, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { urlpath, err := canonicalizePath(site.FS, r.URL.Path) if err != nil { - context.Error(w, r, err) + siteContext.Error(w, r, err) return } else if r.URL.Path != urlpath { http.Redirect(w, r, urlpath, http.StatusMovedPermanently) diff --git a/internal/handler/site/middleware/middleware.go b/internal/handler/site/middleware/middleware.go index b27334a..f8cc0c5 100644 --- a/internal/handler/site/middleware/middleware.go +++ b/internal/handler/site/middleware/middleware.go @@ -1,25 +1,14 @@ package middleware import ( - "net/http" - "github.com/oursky/pageship/internal/cache" - "github.com/oursky/pageship/internal/site" + "github.com/oursky/pageship/internal/handler/site" ) -type Middleware func(*site.Descriptor, http.Handler) http.Handler - -func ApplyMiddleware(site *site.Descriptor, middlewares []Middleware, handler http.Handler) http.Handler { - for i := len(middlewares) - 1; i >= 0; i-- { - handler = middlewares[i](site, handler) - } - return handler -} - -func Default(cc *cache.ContentCache) []Middleware { +func Default(cc *cache.ContentCache) []site.Middleware { cacheContext := NewCacheContext(cc) - return []Middleware{ + return []site.Middleware{ RedirectCustomDomain, CanonicalizePath, RouteSPA, diff --git a/internal/handler/site/site_handler.go b/internal/handler/site/site_handler.go index 36199ec..5a45b24 100644 --- a/internal/handler/site/site_handler.go +++ b/internal/handler/site/site_handler.go @@ -10,8 +10,6 @@ import ( "strings" "time" - siteContext "github.com/oursky/pageship/internal/handler/site/context" - "github.com/oursky/pageship/internal/handler/site/middleware" "github.com/oursky/pageship/internal/httputil" "github.com/oursky/pageship/internal/site" ) @@ -22,7 +20,7 @@ type SiteHandler struct { next http.Handler } -func NewSiteHandler(desc *site.Descriptor, middlewares []middleware.Middleware) *SiteHandler { +func NewSiteHandler(desc *site.Descriptor, middlewares []Middleware) *SiteHandler { h := &SiteHandler{ desc: desc, publicFS: site.SubFS(desc.FS, path.Clean("/"+desc.Config.Public)), @@ -30,7 +28,7 @@ func NewSiteHandler(desc *site.Descriptor, middlewares []middleware.Middleware) publicDesc := *desc publicDesc.FS = site.SubFS(desc.FS, path.Clean("/"+desc.Config.Public)) - h.next = middleware.ApplyMiddleware(&publicDesc, middlewares, http.HandlerFunc(h.serveFile)) + h.next = ApplyMiddleware(&publicDesc, middlewares, http.HandlerFunc(h.serveFile)) return h } @@ -44,7 +42,7 @@ func (h *SiteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet, http.MethodHead: - r = r.WithContext(siteContext.WithSiteContext(r.Context())) + r = r.WithContext(WithSiteContext(r.Context())) if !strings.HasPrefix(r.URL.Path, "/") { r.URL.Path = "/" + r.URL.Path