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 PageTemplate *template.Template
} }
func (gipg *goInfoPage) GetCacheIDExtension(urlParameters url.Values) string {
if urlParameters.Has("full") {
return "full"
} else {
return ""
}
}
type goInfoTemplateMarshal struct { type goInfoTemplateMarshal struct {
FullOutput bool FullOutput bool
RegisteredPages []string RegisteredPages []string
CachedPages []string CachedPages []string
ProcessID int
ProductLocation string
ProductName string ProductName string
ProductDescription string ProductDescription string
BuildVersion string BuildVersion string
BuildDate string BuildDate string
WorkingDirectory string
Hostname string
PageSize int
GoVersion string GoVersion string
GoRoutineNum int GoRoutineNum int
GoCGoCallNum int64 GoCGoCallNum int64
@ -52,6 +65,7 @@ type goInfoTemplateMarshal struct {
GoMaxProcs int GoMaxProcs int
ListenSettings conf.ListenYaml ListenSettings conf.ListenYaml
ServeSettings conf.ServeYaml ServeSettings conf.ServeYaml
Environment []string
} }
func (gipg *goInfoPage) GetPath() 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 return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false
} }
theBuffer := &utils.BufferedWriter{} 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{ err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{
FullOutput: urlParameters.Has("full"), FullOutput: urlParameters.Has("full"),
RegisteredPages: gipg.Handler.GetRegisteredPages(), RegisteredPages: regPages,
CachedPages: gipg.Handler.GetCachedPages(), CachedPages: cacPages,
ProcessID: os.Getpid(),
ProductLocation: getStringOrError(os.Executable),
ProductName: info.BuildName, ProductName: info.BuildName,
ProductDescription: info.BuildDescription, ProductDescription: info.BuildDescription,
BuildVersion: info.BuildVersion, BuildVersion: info.BuildVersion,
BuildDate: info.BuildDate, BuildDate: info.BuildDate,
WorkingDirectory: getStringOrError(os.Getwd),
Hostname: getStringOrError(os.Hostname),
PageSize: os.Getpagesize(),
GoVersion: runtime.Version(), GoVersion: runtime.Version(),
GoRoutineNum: runtime.NumGoroutine(), GoRoutineNum: runtime.NumGoroutine(),
GoCGoCallNum: runtime.NumCgoCall(), GoCGoCallNum: runtime.NumCgoCall(),
@ -88,6 +118,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin
GoMaxProcs: runtime.GOMAXPROCS(0), GoMaxProcs: runtime.GOMAXPROCS(0),
ListenSettings: info.ListenSettings, ListenSettings: info.ListenSettings,
ServeSettings: info.ServeSettings, ServeSettings: info.ServeSettings,
Environment: env,
}) })
if err != nil { if err != nil {
return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false 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 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) { func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
actualPagePath := strings.TrimRight(request.URL.Path, "/") 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 pageContent []byte
var pageContentType string pageContentType := ""
var lastMod time.Time var lastMod time.Time
if ph.CacheSettings.EnableContentsCaching { if currentProvider = ph.PageProviders[actualPagePath]; currentProvider != nil {
cached := ph.getPageFromCache(request.URL, actualQueries) actualQueries = currentProvider.GetCacheIDExtension(queryValues)
if cached != nil {
pageContent = cached.Content
pageContentType = cached.ContentType
lastMod = cached.LastMod
}
}
if pageContentType == "" { if ph.CacheSettings.EnableContentsCaching {
if provider := ph.PageProviders[actualPagePath]; provider != nil { cached := ph.getPageFromCache(request.URL, actualQueries)
var canCache bool if cached != nil {
pageContentType, pageContent, canCache = provider.GetContents(queryCollection) pageContent = cached.Content
lastMod = provider.GetLastModified() pageContentType = cached.ContentType
lastMod = cached.LastMod
}
}
if pageContentType == "" {
pageContentType, pageContent, canCache = currentProvider.GetContents(queryValues)
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, actualQueries, &CachedPage{
Content: pageContent, 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) { 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 == "" {
@ -313,8 +273,8 @@ func (ph *PageHandler) GetRegisteredPages() []string {
} }
func (ph *PageHandler) GetCachedPages() []string { func (ph *PageHandler) GetCachedPages() []string {
ph.pageContentsCacheRWMutex.Lock() ph.pageContentsCacheRWMutex.RLock()
defer ph.pageContentsCacheRWMutex.Unlock() defer ph.pageContentsCacheRWMutex.RUnlock()
pages := make([]string, len(ph.PageContentsCache)) pages := make([]string, len(ph.PageContentsCache))
index := 0 index := 0
for s := range ph.PageContentsCache { for s := range ph.PageContentsCache {
@ -323,3 +283,9 @@ func (ph *PageHandler) GetCachedPages() []string {
} }
return pages 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 { type PageProvider interface {
GetPath() string GetPath() string
GetSupportedURLParameters() []string
GetLastModified() time.Time GetLastModified() time.Time
GetCacheIDExtension(urlParameters url.Values) string
GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool)
PurgeTemplate() PurgeTemplate()
} }