Switch over from test to go-info.
All checks were successful
continuous-integration/drone/push Build is passing

Improve Makefile and add new compilation fields.
This commit is contained in:
Captain ALM 2022-07-16 00:53:50 +01:00
parent b326df8a9c
commit ef3c387f39
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
11 changed files with 210 additions and 51 deletions

View File

@ -1,11 +1,12 @@
SHELL := /bin/bash SHELL := /bin/bash
BIN := dist/wappcityuni PRODUCT_NAME := wappcityuni
ENTRY_POINT := ./cmd/wappcityuni BIN := dist/${PRODUCT_NAME}
ENTRY_POINT := ./cmd/${PRODUCT_NAME}
HASH := $(shell git rev-parse --short HEAD) HASH := $(shell git rev-parse --short HEAD)
COMMIT_DATE := $(shell git show -s --format=%ci ${HASH}) COMMIT_DATE := $(shell git show -s --format=%ci ${HASH})
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S') BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
VERSION := ${HASH} VERSION := ${HASH}
LD_FLAGS := -s -w -X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}' LD_FLAGS := -s -w -X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.buildName=${PRODUCT_NAME}'
COMP_BIN := go COMP_BIN := go
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)

View File

@ -5,6 +5,7 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"golang.captainalm.com/cityuni-webserver/conf" "golang.captainalm.com/cityuni-webserver/conf"
"golang.captainalm.com/cityuni-webserver/pageHandler" "golang.captainalm.com/cityuni-webserver/pageHandler"
"golang.captainalm.com/cityuni-webserver/utils/info"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"log" "log"
"net" "net"
@ -20,13 +21,16 @@ import (
) )
var ( var (
buildVersion = "develop" buildName = ""
buildDate = "" buildDescription = "City Uni Portfolio Web APP"
buildVersion = "develop"
buildDate = ""
) )
func main() { func main() {
log.Printf("[Main] Starting up City Uni Portfolio Web APP #%s (%s)\n", buildVersion, buildDate) log.Printf("[Main] Starting up %s (%s) #%s (%s)\n", buildDescription, buildName, buildVersion, buildDate)
y := time.Now() y := time.Now()
info.SetupProductInfo(buildName, buildDescription, buildVersion, buildDate)
//Hold main thread till safe shutdown exit: //Hold main thread till safe shutdown exit:
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
@ -78,6 +82,8 @@ func main() {
//Server definitions: //Server definitions:
var webServer *http.Server var webServer *http.Server
var fcgiListen net.Listener var fcgiListen net.Listener
info.ListenSettings = configYml.Listen
info.ServeSettings = configYml.Serve
switch strings.ToLower(configYml.Listen.WebMethod) { switch strings.ToLower(configYml.Listen.WebMethod) {
case "http": case "http":
webServer = &http.Server{Handler: pageHandler.GetRouter(configYml)} webServer = &http.Server{Handler: pageHandler.GetRouter(configYml)}

View File

@ -5,5 +5,6 @@ type ServeYaml struct {
Domains []string `yaml:"domains"` Domains []string `yaml:"domains"`
RangeSupported bool `yaml:"rangeSupported"` RangeSupported bool `yaml:"rangeSupported"`
FilterURLQueries bool `yaml:"filterURLQueries"` FilterURLQueries bool `yaml:"filterURLQueries"`
EnableGoInfoPage bool `yaml:"enableGoInfoPage"`
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"` CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`
} }

View File

@ -5,6 +5,7 @@ listen:
identify: true identify: true
serve: serve:
rangeSupported: true rangeSupported: true
enableGoInfoPage: true
cacheSettings: cacheSettings:
enableContentsCaching: true enableContentsCaching: true
enableContentsCachePurge: true enableContentsCachePurge: true

131
pageHandler/go-info-page.go Normal file
View File

@ -0,0 +1,131 @@
package pageHandler
import (
"golang.captainalm.com/cityuni-webserver/conf"
"golang.captainalm.com/cityuni-webserver/pageHandler/utils"
"golang.captainalm.com/cityuni-webserver/utils/info"
"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{}
}
return &goInfoPage{
Handler: handlerIn,
DataStore: dataStore,
CacheTemplate: cacheTemplates,
PageTemplateMutex: ptm,
}
}
type goInfoPage struct {
Handler *PageHandler
DataStore string
CacheTemplate bool
PageTemplateMutex *sync.Mutex
PageTemplate *template.Template
}
type goInfoTemplateMarshal struct {
FullOutput bool
RegisteredPages []string
CachedPages []string
ProductName string
ProductDescription string
BuildVersion string
BuildDate string
GoVersion string
GoRoutineNum int
GoCGoCallNum int64
NumCPU int
GoRoot string
GoMaxProcs int
ListenSettings conf.ListenYaml
ServeSettings conf.ServeYaml
}
func (gipg *goInfoPage) GetPath() string {
return "/goinfo.go"
}
func (gipg *goInfoPage) GetSupportedURLParameters() []string {
return []string{"full"}
}
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
}
theBuffer := &utils.BufferedWriter{}
err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{
FullOutput: urlParameters.Has("full"),
RegisteredPages: gipg.Handler.GetRegisteredPages(),
CachedPages: gipg.Handler.GetCachedPages(),
ProductName: info.BuildName,
ProductDescription: info.BuildDescription,
BuildVersion: info.BuildVersion,
BuildDate: info.BuildDate,
GoVersion: runtime.Version(),
GoRoutineNum: runtime.NumGoroutine(),
GoCGoCallNum: runtime.NumCgoCall(),
NumCPU: runtime.NumCPU(),
GoRoot: runtime.GOROOT(),
GoMaxProcs: runtime.GOMAXPROCS(0),
ListenSettings: info.ListenSettings,
ServeSettings: info.ServeSettings,
})
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() {
if gipg.CacheTemplate {
gipg.PageTemplateMutex.Lock()
gipg.PageTemplate = nil
gipg.PageTemplateMutex.Unlock()
}
}
func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) {
if gipg.CacheTemplate {
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
}
if gipg.CacheTemplate {
gipg.PageTemplate = tmpl
}
return tmpl, nil
} else {
return gipg.PageTemplate, nil
}
}

View File

@ -36,14 +36,19 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
thePCCMap = make(map[string]*CachedPage) thePCCMap = make(map[string]*CachedPage)
theMutex = &sync.RWMutex{} theMutex = &sync.RWMutex{}
} }
return &PageHandler{ toReturn := &PageHandler{
PageContentsCache: thePCCMap, PageContentsCache: thePCCMap,
PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage),
pageContentsCacheRWMutex: theMutex, pageContentsCacheRWMutex: theMutex,
RangeSupported: config.RangeSupported, RangeSupported: config.RangeSupported,
FilterURLQueries: config.FilterURLQueries, FilterURLQueries: config.FilterURLQueries,
CacheSettings: config.CacheSettings, CacheSettings: config.CacheSettings,
} }
if config.EnableGoInfoPage {
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage, toReturn)
} else {
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage, nil)
}
return toReturn
} }
func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
@ -296,3 +301,25 @@ func (ph *PageHandler) getAllowedMethodsForPath(pathIn string) []string {
} }
} }
} }
func (ph *PageHandler) GetRegisteredPages() []string {
pages := make([]string, len(ph.PageProviders))
index := 0
for s := range ph.PageProviders {
pages[index] = s
index++
}
return pages
}
func (ph *PageHandler) GetCachedPages() []string {
ph.pageContentsCacheRWMutex.Lock()
defer ph.pageContentsCacheRWMutex.Unlock()
pages := make([]string, len(ph.PageContentsCache))
index := 0
for s := range ph.PageContentsCache {
pages[index] = s
index++
}
return pages
}

View File

@ -1,13 +1,15 @@
package pageHandler package pageHandler
import "golang.captainalm.com/cityuni-webserver/pageHandler/pages"
var providers map[string]PageProvider var providers map[string]PageProvider
func GetProviders(cacheTemplates bool, dataStorage string) map[string]PageProvider { func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHandler) map[string]PageProvider {
if providers == nil { if providers == nil {
providers = make(map[string]PageProvider) providers = make(map[string]PageProvider)
providers["/test.go"] = pages.NewTestPage() //Test Page if pageHandler != nil {
infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates)
providers[infoPage.GetPath()] = infoPage //Go Information Page
}
//Add the providers in the pages sub package //Add the providers in the pages sub package
} }
return providers return providers

View File

@ -1,39 +0,0 @@
package pages
import (
"net/url"
"time"
)
var startTime = time.Now()
func NewTestPage() *TestPage {
return &TestPage{}
}
type TestPage struct {
}
func (tp *TestPage) GetPath() string {
return "/test.go"
}
func (tp *TestPage) GetSupportedURLParameters() []string {
return []string{"test"}
}
func (tp *TestPage) GetLastModified() time.Time {
return startTime
}
func (tp *TestPage) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) {
if val, ok := urlParameters["test"]; ok {
if len(val) > 0 {
return "text/plain", ([]byte)("Testing!\r\n" + val[0]), len(val) == 1
}
}
return "text/plain", ([]byte)("Testing!"), true
}
func (tp *TestPage) PurgeTemplate() {
}

View File

@ -0,0 +1,10 @@
package utils
type BufferedWriter struct {
Data []byte
}
func (c *BufferedWriter) Write(p []byte) (n int, err error) {
c.Data = append(c.Data, p...)
return len(p), nil
}

6
utils/info/conf-info.go Normal file
View File

@ -0,0 +1,6 @@
package info
import "golang.captainalm.com/cityuni-webserver/conf"
var ListenSettings conf.ListenYaml
var ServeSettings conf.ServeYaml

View File

@ -0,0 +1,13 @@
package info
var BuildName string
var BuildDescription string
var BuildVersion string
var BuildDate string
func SetupProductInfo(buildName string, buildDescription string, buildVersion string, buildDate string) {
BuildName = buildName
BuildDescription = buildDescription
BuildVersion = buildVersion
BuildDate = buildDate
}