From 884c88525eaddce4563575806bfa9d983f6d7065 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 16 Jul 2022 11:44:38 +0100 Subject: [PATCH] Switch over cache handling system. Add more values for the Go Info page. --- pageHandler/go-info-page.go | 44 +++++++++++++++++- pageHandler/page-handler.go | 88 +++++++++++------------------------- pageHandler/page-provider.go | 2 +- 3 files changed, 70 insertions(+), 64 deletions(-) diff --git a/pageHandler/go-info-page.go b/pageHandler/go-info-page.go index 378d94d..96d9bb5 100644 --- a/pageHandler/go-info-page.go +++ b/pageHandler/go-info-page.go @@ -36,14 +36,27 @@ type goInfoPage struct { PageTemplate *template.Template } +func (gipg *goInfoPage) GetCacheIDExtension(urlParameters url.Values) string { + if urlParameters.Has("full") { + return "full" + } else { + return "" + } +} + type goInfoTemplateMarshal struct { FullOutput bool RegisteredPages []string CachedPages []string + ProcessID int + ProductLocation string ProductName string ProductDescription string BuildVersion string BuildDate string + WorkingDirectory string + Hostname string + PageSize int GoVersion string GoRoutineNum int GoCGoCallNum int64 @@ -52,6 +65,7 @@ type goInfoTemplateMarshal struct { GoMaxProcs int ListenSettings conf.ListenYaml ServeSettings conf.ServeYaml + Environment []string } func (gipg *goInfoPage) GetPath() string { @@ -72,14 +86,30 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false } theBuffer := &utils.BufferedWriter{} + var regPages []string + var cacPages []string + env := make([]string, 0) + if urlParameters.Has("full") { + regPages = gipg.Handler.GetRegisteredPages() + cacPages = gipg.Handler.GetCachedPages() + env = os.Environ() + } else { + regPages = make([]string, len(gipg.Handler.PageProviders)) + cacPages = make([]string, gipg.Handler.GetNumberOfCachedPages()) + } err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{ FullOutput: urlParameters.Has("full"), - RegisteredPages: gipg.Handler.GetRegisteredPages(), - CachedPages: gipg.Handler.GetCachedPages(), + RegisteredPages: regPages, + CachedPages: cacPages, + ProcessID: os.Getpid(), + ProductLocation: getStringOrError(os.Executable), ProductName: info.BuildName, ProductDescription: info.BuildDescription, BuildVersion: info.BuildVersion, BuildDate: info.BuildDate, + WorkingDirectory: getStringOrError(os.Getwd), + Hostname: getStringOrError(os.Hostname), + PageSize: os.Getpagesize(), GoVersion: runtime.Version(), GoRoutineNum: runtime.NumGoroutine(), GoCGoCallNum: runtime.NumCgoCall(), @@ -88,6 +118,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin GoMaxProcs: runtime.GOMAXPROCS(0), ListenSettings: info.ListenSettings, ServeSettings: info.ServeSettings, + Environment: env, }) if err != nil { return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false @@ -129,3 +160,12 @@ func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) { return gipg.PageTemplate, nil } } + +func getStringOrError(funcIn func() (string, error)) string { + toReturn, err := funcIn() + if err == nil { + return toReturn + } else { + return "Error: " + err.Error() + } +} diff --git a/pageHandler/page-handler.go b/pageHandler/page-handler.go index 0faffd7..9c71d93 100644 --- a/pageHandler/page-handler.go +++ b/pageHandler/page-handler.go @@ -53,26 +53,30 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler { func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { actualPagePath := strings.TrimRight(request.URL.Path, "/") - queryCollection, actualQueries := ph.GetCleanQuery(request) + var currentProvider PageProvider + canCache := false + actualQueries := "" + queryValues := request.URL.Query() var pageContent []byte - var pageContentType string + pageContentType := "" var lastMod time.Time - if ph.CacheSettings.EnableContentsCaching { - cached := ph.getPageFromCache(request.URL, actualQueries) - if cached != nil { - pageContent = cached.Content - pageContentType = cached.ContentType - lastMod = cached.LastMod - } - } + if currentProvider = ph.PageProviders[actualPagePath]; currentProvider != nil { + actualQueries = currentProvider.GetCacheIDExtension(queryValues) - if pageContentType == "" { - if provider := ph.PageProviders[actualPagePath]; provider != nil { - var canCache bool - pageContentType, pageContent, canCache = provider.GetContents(queryCollection) - lastMod = provider.GetLastModified() + if ph.CacheSettings.EnableContentsCaching { + cached := ph.getPageFromCache(request.URL, actualQueries) + if cached != nil { + pageContent = cached.Content + pageContentType = cached.ContentType + lastMod = cached.LastMod + } + } + + if pageContentType == "" { + pageContentType, pageContent, canCache = currentProvider.GetContents(queryValues) + lastMod = currentProvider.GetLastModified() if pageContentType != "" && canCache && ph.CacheSettings.EnableContentsCaching { ph.setPageToCache(request.URL, actualQueries, &CachedPage{ Content: pageContent, @@ -163,50 +167,6 @@ func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque } } -func (ph *PageHandler) GetCleanQuery(request *http.Request) (url.Values, string) { - toClean := request.URL.Query() - provider := ph.PageProviders[request.URL.Path] - if provider == nil { - return make(url.Values), "" - } - supportedKeys := provider.GetSupportedURLParameters() - var toDelete []string - if ph.FilterURLQueries { - toDelete = make([]string, len(toClean)) - } - theSize := 0 - theQuery := "" - for s, v := range toClean { - noExist := true - for _, key := range supportedKeys { - if s == key { - noExist = false - break - } - } - if noExist { - if ph.FilterURLQueries { - toDelete[theSize] = s - theSize++ - } - } else { - for _, i := range v { - if i == "" { - theQuery += s + "&" - } else { - theQuery += s + "=" + i + "&" - } - } - } - } - if ph.FilterURLQueries { - for i := 0; i < theSize; i++ { - delete(toClean, toDelete[i]) - } - } - return toClean, strings.TrimRight(theQuery, "&") -} - func (ph *PageHandler) PurgeContentsCache(path string, query string) { if ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge { if path == "" { @@ -313,8 +273,8 @@ func (ph *PageHandler) GetRegisteredPages() []string { } func (ph *PageHandler) GetCachedPages() []string { - ph.pageContentsCacheRWMutex.Lock() - defer ph.pageContentsCacheRWMutex.Unlock() + ph.pageContentsCacheRWMutex.RLock() + defer ph.pageContentsCacheRWMutex.RUnlock() pages := make([]string, len(ph.PageContentsCache)) index := 0 for s := range ph.PageContentsCache { @@ -323,3 +283,9 @@ func (ph *PageHandler) GetCachedPages() []string { } return pages } + +func (ph *PageHandler) GetNumberOfCachedPages() int { + ph.pageContentsCacheRWMutex.RLock() + defer ph.pageContentsCacheRWMutex.RUnlock() + return len(ph.PageContentsCache) +} diff --git a/pageHandler/page-provider.go b/pageHandler/page-provider.go index 08da601..8cd6b6f 100644 --- a/pageHandler/page-provider.go +++ b/pageHandler/page-provider.go @@ -7,8 +7,8 @@ import ( type PageProvider interface { GetPath() string - GetSupportedURLParameters() []string GetLastModified() time.Time + GetCacheIDExtension(urlParameters url.Values) string GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) PurgeTemplate() }