mirror of https://github.com/jlelse/GoBlog
Reduce amount of middleware initializations when rebuilding router
This commit is contained in:
parent
610113e9c9
commit
c4baa47456
|
@ -16,6 +16,10 @@ func serve404(w http.ResponseWriter, r *http.Request) {
|
||||||
serveError(w, r, fmt.Sprintf("%s was not found", r.RequestURI), http.StatusNotFound)
|
serveError(w, r, fmt.Sprintf("%s was not found", r.RequestURI), http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func serveNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||||
|
serveError(w, r, "", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
var errorCheckMediaTypes = []contenttype.MediaType{
|
var errorCheckMediaTypes = []contenttype.MediaType{
|
||||||
contenttype.NewMediaType(contentTypeHTML),
|
contenttype.NewMediaType(contentTypeHTML),
|
||||||
}
|
}
|
||||||
|
|
102
http.go
102
http.go
|
@ -110,8 +110,6 @@ var (
|
||||||
privateMode = false
|
privateMode = false
|
||||||
privateModeHandler = []func(http.Handler) http.Handler{}
|
privateModeHandler = []func(http.Handler) http.Handler{}
|
||||||
|
|
||||||
setBlogMiddlewares = map[string]func(http.Handler) http.Handler{}
|
|
||||||
|
|
||||||
captchaHandler http.Handler
|
captchaHandler http.Handler
|
||||||
|
|
||||||
micropubRouter *chi.Mux
|
micropubRouter *chi.Mux
|
||||||
|
@ -122,6 +120,14 @@ var (
|
||||||
editorRouter *chi.Mux
|
editorRouter *chi.Mux
|
||||||
commentsRouter *chi.Mux
|
commentsRouter *chi.Mux
|
||||||
searchRouter *chi.Mux
|
searchRouter *chi.Mux
|
||||||
|
|
||||||
|
setBlogMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
sectionMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
taxonomyMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
photosMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
searchMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
customPagesMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
commentsMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildStaticHandlersRouters() error {
|
func buildStaticHandlersRouters() error {
|
||||||
|
@ -194,14 +200,59 @@ func buildStaticHandlersRouters() error {
|
||||||
searchRouter.Get(searchResultPath+feedPath, serveSearchResult)
|
searchRouter.Get(searchResultPath+feedPath, serveSearchResult)
|
||||||
searchRouter.Get(searchResultPath+paginationPath, serveSearchResult)
|
searchRouter.Get(searchResultPath+paginationPath, serveSearchResult)
|
||||||
|
|
||||||
for blog := range appConfig.Blogs {
|
for blog, blogConfig := range appConfig.Blogs {
|
||||||
sbm := middleware.WithValue(blogContextKey, blog)
|
sbm := middleware.WithValue(blogContextKey, blog)
|
||||||
setBlogMiddlewares[blog] = sbm
|
setBlogMiddlewares[blog] = sbm
|
||||||
|
|
||||||
|
blogPath := blogPath(blog)
|
||||||
|
|
||||||
|
for _, section := range blogConfig.Sections {
|
||||||
|
if section.Name != "" {
|
||||||
|
secPath := blogPath + "/" + section.Name
|
||||||
|
sectionMiddlewares[secPath] = middleware.WithValue(indexConfigKey, &indexConfig{
|
||||||
|
path: secPath,
|
||||||
|
section: section,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, taxonomy := range blogConfig.Taxonomies {
|
||||||
|
if taxonomy.Name != "" {
|
||||||
|
taxPath := blogPath + "/" + taxonomy.Name
|
||||||
|
taxonomyMiddlewares[taxPath] = middleware.WithValue(taxonomyContextKey, taxonomy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if blogConfig.Photos != nil && blogConfig.Photos.Enabled {
|
||||||
|
photosMiddlewares[blog] = middleware.WithValue(indexConfigKey, &indexConfig{
|
||||||
|
path: blogPath + blogConfig.Photos.Path,
|
||||||
|
parameter: blogConfig.Photos.Parameter,
|
||||||
|
title: blogConfig.Photos.Title,
|
||||||
|
description: blogConfig.Photos.Description,
|
||||||
|
summaryTemplate: templatePhotosSummary,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if blogConfig.Search != nil && blogConfig.Search.Enabled {
|
||||||
|
searchMiddlewares[blog] = middleware.WithValue(pathContextKey, blogPath+blogConfig.Search.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cp := range blogConfig.CustomPages {
|
||||||
|
customPagesMiddlewares[cp.Path] = middleware.WithValue(customPageContextKey, cp)
|
||||||
|
}
|
||||||
|
|
||||||
|
if commentsConfig := blogConfig.Comments; commentsConfig != nil && commentsConfig.Enabled {
|
||||||
|
commentsMiddlewares[blog] = middleware.WithValue(pathContextKey, blogPath+"/comment")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
taxValueMiddlewares = map[string]func(http.Handler) http.Handler{}
|
||||||
|
)
|
||||||
|
|
||||||
func buildDynamicRouter() (*chi.Mux, error) {
|
func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
@ -315,15 +366,12 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
// Sections
|
// Sections
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(privateModeHandler...)
|
r.Use(privateModeHandler...)
|
||||||
r.Use(cacheMiddleware)
|
r.Use(cacheMiddleware, sbm)
|
||||||
for _, section := range blogConfig.Sections {
|
for _, section := range blogConfig.Sections {
|
||||||
if section.Name != "" {
|
if section.Name != "" {
|
||||||
secPath := blogPath + "/" + section.Name
|
secPath := blogPath + "/" + section.Name
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(sbm, middleware.WithValue(indexConfigKey, &indexConfig{
|
r.Use(sectionMiddlewares[secPath])
|
||||||
path: secPath,
|
|
||||||
section: section,
|
|
||||||
}))
|
|
||||||
r.Get(secPath, serveIndex)
|
r.Get(secPath, serveIndex)
|
||||||
r.Get(secPath+feedPath, serveIndex)
|
r.Get(secPath+feedPath, serveIndex)
|
||||||
r.Get(secPath+paginationPath, serveIndex)
|
r.Get(secPath+paginationPath, serveIndex)
|
||||||
|
@ -342,16 +390,19 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
}
|
}
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(privateModeHandler...)
|
r.Use(privateModeHandler...)
|
||||||
r.Use(cacheMiddleware)
|
r.Use(cacheMiddleware, sbm)
|
||||||
r.With(sbm, middleware.WithValue(taxonomyContextKey, taxonomy)).Get(taxPath, serveTaxonomy)
|
r.With(taxonomyMiddlewares[taxPath]).Get(taxPath, serveTaxonomy)
|
||||||
for _, tv := range taxValues {
|
for _, tv := range taxValues {
|
||||||
vPath := taxPath + "/" + urlize(tv)
|
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(sbm, middleware.WithValue(indexConfigKey, &indexConfig{
|
vPath := taxPath + "/" + urlize(tv)
|
||||||
path: vPath,
|
if _, ok := taxValueMiddlewares[vPath]; !ok {
|
||||||
tax: taxonomy,
|
taxValueMiddlewares[vPath] = middleware.WithValue(indexConfigKey, &indexConfig{
|
||||||
taxValue: tv,
|
path: vPath,
|
||||||
}))
|
tax: taxonomy,
|
||||||
|
taxValue: tv,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
r.Use(taxValueMiddlewares[vPath])
|
||||||
r.Get(vPath, serveIndex)
|
r.Get(vPath, serveIndex)
|
||||||
r.Get(vPath+feedPath, serveIndex)
|
r.Get(vPath+feedPath, serveIndex)
|
||||||
r.Get(vPath+paginationPath, serveIndex)
|
r.Get(vPath+paginationPath, serveIndex)
|
||||||
|
@ -365,15 +416,8 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
if blogConfig.Photos != nil && blogConfig.Photos.Enabled {
|
if blogConfig.Photos != nil && blogConfig.Photos.Enabled {
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(privateModeHandler...)
|
r.Use(privateModeHandler...)
|
||||||
r.Use(cacheMiddleware)
|
r.Use(cacheMiddleware, sbm, photosMiddlewares[blog])
|
||||||
photoPath := blogPath + blogConfig.Photos.Path
|
photoPath := blogPath + blogConfig.Photos.Path
|
||||||
r.Use(sbm, middleware.WithValue(indexConfigKey, &indexConfig{
|
|
||||||
path: photoPath,
|
|
||||||
parameter: blogConfig.Photos.Parameter,
|
|
||||||
title: blogConfig.Photos.Title,
|
|
||||||
description: blogConfig.Photos.Description,
|
|
||||||
summaryTemplate: templatePhotosSummary,
|
|
||||||
}))
|
|
||||||
r.Get(photoPath, serveIndex)
|
r.Get(photoPath, serveIndex)
|
||||||
r.Get(photoPath+feedPath, serveIndex)
|
r.Get(photoPath+feedPath, serveIndex)
|
||||||
r.Get(photoPath+paginationPath, serveIndex)
|
r.Get(photoPath+paginationPath, serveIndex)
|
||||||
|
@ -383,7 +427,7 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
// Search
|
// Search
|
||||||
if blogConfig.Search != nil && blogConfig.Search.Enabled {
|
if blogConfig.Search != nil && blogConfig.Search.Enabled {
|
||||||
searchPath := blogPath + blogConfig.Search.Path
|
searchPath := blogPath + blogConfig.Search.Path
|
||||||
r.With(sbm, middleware.WithValue(pathContextKey, searchPath)).Mount(searchPath, searchRouter)
|
r.With(sbm, searchMiddlewares[blog]).Mount(searchPath, searchRouter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stats
|
// Stats
|
||||||
|
@ -430,7 +474,7 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
|
|
||||||
// Custom pages
|
// Custom pages
|
||||||
for _, cp := range blogConfig.CustomPages {
|
for _, cp := range blogConfig.CustomPages {
|
||||||
scp := middleware.WithValue(customPageContextKey, cp)
|
scp := customPagesMiddlewares[cp.Path]
|
||||||
if cp.Cache {
|
if cp.Cache {
|
||||||
r.With(privateModeHandler...).With(cacheMiddleware, sbm, scp).Get(cp.Path, serveCustomPage)
|
r.With(privateModeHandler...).With(cacheMiddleware, sbm, scp).Get(cp.Path, serveCustomPage)
|
||||||
} else {
|
} else {
|
||||||
|
@ -453,7 +497,7 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
// Comments
|
// Comments
|
||||||
if commentsConfig := blogConfig.Comments; commentsConfig != nil && commentsConfig.Enabled {
|
if commentsConfig := blogConfig.Comments; commentsConfig != nil && commentsConfig.Enabled {
|
||||||
commentsPath := blogPath + "/comment"
|
commentsPath := blogPath + "/comment"
|
||||||
r.With(sbm, middleware.WithValue(pathContextKey, commentsPath)).Mount(commentsPath, commentsRouter)
|
r.With(sbm, commentsMiddlewares[blog]).Mount(commentsPath, commentsRouter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -470,9 +514,7 @@ func buildDynamicRouter() (*chi.Mux, error) {
|
||||||
// Check redirects, then serve 404
|
// Check redirects, then serve 404
|
||||||
r.With(cacheMiddleware, checkRegexRedirects).NotFound(serve404)
|
r.With(cacheMiddleware, checkRegexRedirects).NotFound(serve404)
|
||||||
|
|
||||||
r.MethodNotAllowed(func(rw http.ResponseWriter, r *http.Request) {
|
r.MethodNotAllowed(serveNotAllowed)
|
||||||
serveError(rw, r, "", http.StatusMethodNotAllowed)
|
|
||||||
})
|
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue