Skip to content

Commit

Permalink
Fix import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 26, 2024
1 parent ba84cd8 commit c40f090
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
Binary file added cmd/pageship/pageship
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package context
package site

import (
"context"
Expand Down
5 changes: 2 additions & 3 deletions internal/handler/site/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,7 +25,7 @@ const (

type HandlerConfig struct {
HostPattern string
MiddlewaresFunc func(*cache.ContentCache) []middleware.Middleware
MiddlewaresFunc func(*cache.ContentCache) []Middleware
ContentCacheMaxSize int64
}

Expand All @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions internal/handler/site/middleware.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions internal/handler/site/middleware/canon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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"
)

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)
Expand Down
17 changes: 3 additions & 14 deletions internal/handler/site/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 3 additions & 5 deletions internal/handler/site/site_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -22,15 +20,15 @@ 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)),
}

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
}

Expand All @@ -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
Expand Down

0 comments on commit c40f090

Please sign in to comment.