2022-07-16 00:53:50 +01:00
|
|
|
package pageHandler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.captainalm.com/cityuni-webserver/conf"
|
|
|
|
"golang.captainalm.com/cityuni-webserver/utils/info"
|
2022-07-16 11:47:52 +01:00
|
|
|
"golang.captainalm.com/cityuni-webserver/utils/io"
|
2022-07-16 00:53:50 +01:00
|
|
|
"html/template"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const templateName = "goinfo.go.html"
|
|
|
|
|
|
|
|
func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool) *goInfoPage {
|
|
|
|
var ptm *sync.Mutex
|
|
|
|
if cacheTemplates {
|
|
|
|
ptm = &sync.Mutex{}
|
|
|
|
}
|
2022-07-16 16:01:53 +01:00
|
|
|
pageToReturn := &goInfoPage{
|
2022-07-16 00:53:50 +01:00
|
|
|
Handler: handlerIn,
|
|
|
|
DataStore: dataStore,
|
|
|
|
PageTemplateMutex: ptm,
|
|
|
|
}
|
2022-07-16 16:01:53 +01:00
|
|
|
return pageToReturn
|
2022-07-16 00:53:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type goInfoPage struct {
|
|
|
|
Handler *PageHandler
|
|
|
|
DataStore string
|
|
|
|
PageTemplateMutex *sync.Mutex
|
|
|
|
PageTemplate *template.Template
|
|
|
|
}
|
|
|
|
|
2022-07-16 11:44:38 +01:00
|
|
|
func (gipg *goInfoPage) GetCacheIDExtension(urlParameters url.Values) string {
|
|
|
|
if urlParameters.Has("full") {
|
|
|
|
return "full"
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:53:50 +01:00
|
|
|
type goInfoTemplateMarshal struct {
|
|
|
|
FullOutput bool
|
2023-09-19 23:27:32 +01:00
|
|
|
CurrentTime time.Time
|
2022-07-16 00:53:50 +01:00
|
|
|
RegisteredPages []string
|
|
|
|
CachedPages []string
|
2022-07-16 11:44:38 +01:00
|
|
|
ProcessID int
|
2022-07-16 16:01:53 +01:00
|
|
|
ParentProcessID int
|
2022-07-16 11:44:38 +01:00
|
|
|
ProductLocation string
|
2022-07-16 00:53:50 +01:00
|
|
|
ProductName string
|
|
|
|
ProductDescription string
|
|
|
|
BuildVersion string
|
|
|
|
BuildDate string
|
2022-07-16 11:44:38 +01:00
|
|
|
WorkingDirectory string
|
|
|
|
Hostname string
|
|
|
|
PageSize int
|
2022-07-16 00:53:50 +01:00
|
|
|
GoVersion string
|
|
|
|
GoRoutineNum int
|
|
|
|
GoCGoCallNum int64
|
|
|
|
NumCPU int
|
|
|
|
GoRoot string
|
|
|
|
GoMaxProcs int
|
2022-07-16 16:01:53 +01:00
|
|
|
Compiler string
|
|
|
|
GoArch string
|
|
|
|
GoOS string
|
2022-07-16 00:53:50 +01:00
|
|
|
ListenSettings conf.ListenYaml
|
|
|
|
ServeSettings conf.ServeYaml
|
2022-07-16 11:44:38 +01:00
|
|
|
Environment []string
|
2022-07-16 00:53:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gipg *goInfoPage) GetPath() string {
|
|
|
|
return "/goinfo.go"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gipg *goInfoPage) GetLastModified() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) {
|
|
|
|
theTemplate, err := gipg.getPageTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false
|
|
|
|
}
|
2022-07-16 11:44:38 +01:00
|
|
|
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())
|
|
|
|
}
|
2022-07-17 13:37:50 +01:00
|
|
|
theBuffer := &io.BufferedWriter{}
|
2022-07-16 00:53:50 +01:00
|
|
|
err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{
|
|
|
|
FullOutput: urlParameters.Has("full"),
|
2023-09-19 23:27:32 +01:00
|
|
|
CurrentTime: time.Now(),
|
2022-07-16 11:44:38 +01:00
|
|
|
RegisteredPages: regPages,
|
|
|
|
CachedPages: cacPages,
|
|
|
|
ProcessID: os.Getpid(),
|
2022-07-16 16:01:53 +01:00
|
|
|
ParentProcessID: os.Getppid(),
|
2022-07-16 11:44:38 +01:00
|
|
|
ProductLocation: getStringOrError(os.Executable),
|
2022-07-16 00:53:50 +01:00
|
|
|
ProductName: info.BuildName,
|
|
|
|
ProductDescription: info.BuildDescription,
|
|
|
|
BuildVersion: info.BuildVersion,
|
|
|
|
BuildDate: info.BuildDate,
|
2022-07-16 11:44:38 +01:00
|
|
|
WorkingDirectory: getStringOrError(os.Getwd),
|
|
|
|
Hostname: getStringOrError(os.Hostname),
|
|
|
|
PageSize: os.Getpagesize(),
|
2022-07-16 00:53:50 +01:00
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
GoRoutineNum: runtime.NumGoroutine(),
|
|
|
|
GoCGoCallNum: runtime.NumCgoCall(),
|
|
|
|
NumCPU: runtime.NumCPU(),
|
|
|
|
GoRoot: runtime.GOROOT(),
|
|
|
|
GoMaxProcs: runtime.GOMAXPROCS(0),
|
2022-07-16 16:01:53 +01:00
|
|
|
Compiler: runtime.Compiler,
|
|
|
|
GoArch: runtime.GOARCH,
|
|
|
|
GoOS: runtime.GOOS,
|
2022-07-16 00:53:50 +01:00
|
|
|
ListenSettings: info.ListenSettings,
|
|
|
|
ServeSettings: info.ServeSettings,
|
2022-07-16 11:44:38 +01:00
|
|
|
Environment: env,
|
2022-07-16 00:53:50 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false
|
|
|
|
}
|
|
|
|
return "text/html", theBuffer.Data, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gipg *goInfoPage) PurgeTemplate() {
|
2022-07-16 22:20:56 +01:00
|
|
|
if gipg.PageTemplateMutex != nil {
|
2022-07-16 00:53:50 +01:00
|
|
|
gipg.PageTemplateMutex.Lock()
|
|
|
|
gipg.PageTemplate = nil
|
|
|
|
gipg.PageTemplateMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) {
|
2022-07-16 22:20:56 +01:00
|
|
|
if gipg.PageTemplateMutex != nil {
|
2022-07-16 00:53:50 +01:00
|
|
|
gipg.PageTemplateMutex.Lock()
|
|
|
|
defer gipg.PageTemplateMutex.Unlock()
|
|
|
|
}
|
|
|
|
if gipg.PageTemplate == nil {
|
|
|
|
thePath := templateName
|
|
|
|
if gipg.DataStore != "" {
|
|
|
|
thePath = path.Join(gipg.DataStore, thePath)
|
|
|
|
}
|
|
|
|
loadedData, err := os.ReadFile(thePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tmpl, err := template.New(templateName).Parse(string(loadedData))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-16 22:20:56 +01:00
|
|
|
if gipg.PageTemplateMutex != nil {
|
2022-07-16 00:53:50 +01:00
|
|
|
gipg.PageTemplate = tmpl
|
|
|
|
}
|
|
|
|
return tmpl, nil
|
|
|
|
} else {
|
|
|
|
return gipg.PageTemplate, nil
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 11:44:38 +01:00
|
|
|
|
|
|
|
func getStringOrError(funcIn func() (string, error)) string {
|
|
|
|
toReturn, err := funcIn()
|
|
|
|
if err == nil {
|
|
|
|
return toReturn
|
|
|
|
} else {
|
|
|
|
return "Error: " + err.Error()
|
|
|
|
}
|
|
|
|
}
|