Merge Edge with sort and resize #2
@ -7,13 +7,14 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const indexName = "index.go"
|
||||||
|
|
||||||
type PageHandler struct {
|
type PageHandler struct {
|
||||||
PageContentsCache map[string]*CachedPage
|
PageContentsCache map[string]*CachedPage
|
||||||
PageProviders map[string]PageProvider
|
PageProviders map[string]PageProvider
|
||||||
@ -50,7 +51,16 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
actualPagePath := strings.TrimRight(request.URL.Path, "/")
|
actualPagePath := ""
|
||||||
|
if strings.HasSuffix(request.URL.Path, "/") {
|
||||||
|
if strings.HasSuffix(request.URL.Path, ".go/") {
|
||||||
|
actualPagePath = strings.TrimRight(request.URL.Path, "/")
|
||||||
|
} else {
|
||||||
|
actualPagePath = request.URL.Path + indexName
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
actualPagePath = request.URL.Path
|
||||||
|
}
|
||||||
|
|
||||||
var currentProvider PageProvider
|
var currentProvider PageProvider
|
||||||
canCache := false
|
canCache := false
|
||||||
@ -64,7 +74,7 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
|||||||
actualQueries = currentProvider.GetCacheIDExtension(queryValues)
|
actualQueries = currentProvider.GetCacheIDExtension(queryValues)
|
||||||
|
|
||||||
if ph.CacheSettings.EnableContentsCaching {
|
if ph.CacheSettings.EnableContentsCaching {
|
||||||
cached := ph.getPageFromCache(request.URL, actualQueries)
|
cached := ph.getPageFromCache(request.URL.Path, actualQueries)
|
||||||
if cached != nil {
|
if cached != nil {
|
||||||
pageContent = cached.Content
|
pageContent = cached.Content
|
||||||
pageContentType = cached.ContentType
|
pageContentType = cached.ContentType
|
||||||
@ -76,7 +86,7 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
|||||||
pageContentType, pageContent, canCache = currentProvider.GetContents(queryValues)
|
pageContentType, pageContent, canCache = currentProvider.GetContents(queryValues)
|
||||||
lastMod = currentProvider.GetLastModified()
|
lastMod = currentProvider.GetLastModified()
|
||||||
if pageContentType != "" && canCache && ph.CacheSettings.EnableContentsCaching {
|
if pageContentType != "" && canCache && ph.CacheSettings.EnableContentsCaching {
|
||||||
ph.setPageToCache(request.URL, actualQueries, &CachedPage{
|
ph.setPageToCache(request.URL.Path, actualQueries, &CachedPage{
|
||||||
Content: pageContent,
|
Content: pageContent,
|
||||||
ContentType: pageContentType,
|
ContentType: pageContentType,
|
||||||
LastMod: lastMod,
|
LastMod: lastMod,
|
||||||
@ -143,7 +153,7 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
ph.PurgeTemplateCache(actualPagePath)
|
ph.PurgeTemplateCache(actualPagePath, request.URL.Path == "/")
|
||||||
ph.PurgeContentsCache(request.URL.Path, actualQueries)
|
ph.PurgeContentsCache(request.URL.Path, actualQueries)
|
||||||
utils.SetNeverCacheHeader(writer.Header())
|
utils.SetNeverCacheHeader(writer.Header())
|
||||||
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "")
|
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "")
|
||||||
@ -167,12 +177,12 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
|||||||
|
|
||||||
func (ph *PageHandler) PurgeContentsCache(path string, query string) {
|
func (ph *PageHandler) PurgeContentsCache(path string, query string) {
|
||||||
if ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge {
|
if ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge {
|
||||||
if path == "" {
|
if path == "/" {
|
||||||
ph.pageContentsCacheRWMutex.Lock()
|
ph.pageContentsCacheRWMutex.Lock()
|
||||||
ph.PageContentsCache = make(map[string]*CachedPage)
|
ph.PageContentsCache = make(map[string]*CachedPage)
|
||||||
ph.pageContentsCacheRWMutex.Unlock()
|
ph.pageContentsCacheRWMutex.Unlock()
|
||||||
} else {
|
} else {
|
||||||
if strings.HasSuffix(path, "/") {
|
if strings.HasSuffix(path, ".go/") {
|
||||||
ph.pageContentsCacheRWMutex.RLock()
|
ph.pageContentsCacheRWMutex.RLock()
|
||||||
toDelete := make([]string, len(ph.PageContentsCache))
|
toDelete := make([]string, len(ph.PageContentsCache))
|
||||||
theSize := 0
|
theSize := 0
|
||||||
@ -189,7 +199,10 @@ func (ph *PageHandler) PurgeContentsCache(path string, query string) {
|
|||||||
delete(ph.PageContentsCache, toDelete[i])
|
delete(ph.PageContentsCache, toDelete[i])
|
||||||
}
|
}
|
||||||
ph.pageContentsCacheRWMutex.Unlock()
|
ph.pageContentsCacheRWMutex.Unlock()
|
||||||
} else {
|
return
|
||||||
|
} else if strings.HasSuffix(path, "/") {
|
||||||
|
path += indexName
|
||||||
|
}
|
||||||
ph.pageContentsCacheRWMutex.Lock()
|
ph.pageContentsCacheRWMutex.Lock()
|
||||||
if query == "" {
|
if query == "" {
|
||||||
delete(ph.PageContentsCache, path)
|
delete(ph.PageContentsCache, path)
|
||||||
@ -199,12 +212,11 @@ func (ph *PageHandler) PurgeContentsCache(path string, query string) {
|
|||||||
ph.pageContentsCacheRWMutex.Unlock()
|
ph.pageContentsCacheRWMutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ph *PageHandler) PurgeTemplateCache(path string) {
|
func (ph *PageHandler) PurgeTemplateCache(path string, all bool) {
|
||||||
if ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge {
|
if ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge {
|
||||||
if path == "" {
|
if all {
|
||||||
for _, pageProvider := range ph.PageProviders {
|
for _, pageProvider := range ph.PageProviders {
|
||||||
pageProvider.PurgeTemplate()
|
pageProvider.PurgeTemplate()
|
||||||
}
|
}
|
||||||
@ -215,36 +227,39 @@ func (ph *PageHandler) PurgeTemplateCache(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (ph *PageHandler) getPageFromCache(urlIn *url.URL, cleanedQueries string) *CachedPage {
|
func (ph *PageHandler) getPageFromCache(pathIn string, cleanedQueries string) *CachedPage {
|
||||||
ph.pageContentsCacheRWMutex.RLock()
|
ph.pageContentsCacheRWMutex.RLock()
|
||||||
defer ph.pageContentsCacheRWMutex.RUnlock()
|
defer ph.pageContentsCacheRWMutex.RUnlock()
|
||||||
if strings.HasSuffix(urlIn.Path, "/") {
|
if strings.HasSuffix(pathIn, ".go/") {
|
||||||
return ph.PageContentsCache[strings.TrimRight(urlIn.Path, "/")]
|
return ph.PageContentsCache[strings.TrimRight(pathIn, "/")]
|
||||||
} else {
|
} else if strings.HasSuffix(pathIn, "/") {
|
||||||
if cleanedQueries == "" {
|
pathIn += indexName
|
||||||
return ph.PageContentsCache[urlIn.Path]
|
|
||||||
} else {
|
|
||||||
return ph.PageContentsCache[urlIn.Path+"?"+cleanedQueries]
|
|
||||||
}
|
}
|
||||||
|
if cleanedQueries == "" {
|
||||||
|
return ph.PageContentsCache[pathIn]
|
||||||
|
} else {
|
||||||
|
return ph.PageContentsCache[pathIn+"?"+cleanedQueries]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ph *PageHandler) setPageToCache(urlIn *url.URL, cleanedQueries string, newPage *CachedPage) {
|
func (ph *PageHandler) setPageToCache(pathIn string, cleanedQueries string, newPage *CachedPage) {
|
||||||
ph.pageContentsCacheRWMutex.Lock()
|
ph.pageContentsCacheRWMutex.Lock()
|
||||||
defer ph.pageContentsCacheRWMutex.Unlock()
|
defer ph.pageContentsCacheRWMutex.Unlock()
|
||||||
if strings.HasSuffix(urlIn.Path, "/") {
|
if strings.HasSuffix(pathIn, ".go/") {
|
||||||
ph.PageContentsCache[strings.TrimRight(urlIn.Path, "/")] = newPage
|
ph.PageContentsCache[strings.TrimRight(pathIn, "/")] = newPage
|
||||||
} else {
|
return
|
||||||
if cleanedQueries == "" {
|
} else if strings.HasSuffix(pathIn, "/") {
|
||||||
ph.PageContentsCache[urlIn.Path] = newPage
|
pathIn += indexName
|
||||||
} else {
|
|
||||||
ph.PageContentsCache[urlIn.Path+"?"+cleanedQueries] = newPage
|
|
||||||
}
|
}
|
||||||
|
if cleanedQueries == "" {
|
||||||
|
ph.PageContentsCache[pathIn] = newPage
|
||||||
|
} else {
|
||||||
|
ph.PageContentsCache[pathIn+"?"+cleanedQueries] = newPage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ph *PageHandler) getAllowedMethodsForPath(pathIn string) []string {
|
func (ph *PageHandler) getAllowedMethodsForPath(pathIn string) []string {
|
||||||
if strings.HasSuffix(pathIn, "/") {
|
if pathIn == "/" || strings.HasSuffix(pathIn, ".go/") {
|
||||||
if (ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge) ||
|
if (ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge) ||
|
||||||
(ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge) {
|
(ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge) {
|
||||||
return []string{http.MethodHead, http.MethodGet, http.MethodOptions, http.MethodDelete}
|
return []string{http.MethodHead, http.MethodGet, http.MethodOptions, http.MethodDelete}
|
||||||
|
Loading…
Reference in New Issue
Block a user