Switch over cache handling system.
continuous-integration/drone/push Build is passing Details

Add more values for the Go Info page.
This commit is contained in:
Captain ALM 2022-07-16 11:44:38 +01:00
parent ef3c387f39
commit 884c88525e
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
3 changed files with 70 additions and 64 deletions

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -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()
}