Switch over from test to go-info.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Improve Makefile and add new compilation fields.
This commit is contained in:
parent
b326df8a9c
commit
ef3c387f39
7
Makefile
7
Makefile
@ -1,11 +1,12 @@
|
||||
SHELL := /bin/bash
|
||||
BIN := dist/wappcityuni
|
||||
ENTRY_POINT := ./cmd/wappcityuni
|
||||
PRODUCT_NAME := wappcityuni
|
||||
BIN := dist/${PRODUCT_NAME}
|
||||
ENTRY_POINT := ./cmd/${PRODUCT_NAME}
|
||||
HASH := $(shell git rev-parse --short HEAD)
|
||||
COMMIT_DATE := $(shell git show -s --format=%ci ${HASH})
|
||||
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
|
||||
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
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
"golang.captainalm.com/cityuni-webserver/conf"
|
||||
"golang.captainalm.com/cityuni-webserver/pageHandler"
|
||||
"golang.captainalm.com/cityuni-webserver/utils/info"
|
||||
"gopkg.in/yaml.v3"
|
||||
"log"
|
||||
"net"
|
||||
@ -20,13 +21,16 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
buildVersion = "develop"
|
||||
buildDate = ""
|
||||
buildName = ""
|
||||
buildDescription = "City Uni Portfolio Web APP"
|
||||
buildVersion = "develop"
|
||||
buildDate = ""
|
||||
)
|
||||
|
||||
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()
|
||||
info.SetupProductInfo(buildName, buildDescription, buildVersion, buildDate)
|
||||
|
||||
//Hold main thread till safe shutdown exit:
|
||||
wg := &sync.WaitGroup{}
|
||||
@ -78,6 +82,8 @@ func main() {
|
||||
//Server definitions:
|
||||
var webServer *http.Server
|
||||
var fcgiListen net.Listener
|
||||
info.ListenSettings = configYml.Listen
|
||||
info.ServeSettings = configYml.Serve
|
||||
switch strings.ToLower(configYml.Listen.WebMethod) {
|
||||
case "http":
|
||||
webServer = &http.Server{Handler: pageHandler.GetRouter(configYml)}
|
||||
|
@ -5,5 +5,6 @@ type ServeYaml struct {
|
||||
Domains []string `yaml:"domains"`
|
||||
RangeSupported bool `yaml:"rangeSupported"`
|
||||
FilterURLQueries bool `yaml:"filterURLQueries"`
|
||||
EnableGoInfoPage bool `yaml:"enableGoInfoPage"`
|
||||
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ listen:
|
||||
identify: true
|
||||
serve:
|
||||
rangeSupported: true
|
||||
enableGoInfoPage: true
|
||||
cacheSettings:
|
||||
enableContentsCaching: true
|
||||
enableContentsCachePurge: true
|
||||
|
131
pageHandler/go-info-page.go
Normal file
131
pageHandler/go-info-page.go
Normal 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
|
||||
}
|
||||
}
|
@ -36,14 +36,19 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
|
||||
thePCCMap = make(map[string]*CachedPage)
|
||||
theMutex = &sync.RWMutex{}
|
||||
}
|
||||
return &PageHandler{
|
||||
toReturn := &PageHandler{
|
||||
PageContentsCache: thePCCMap,
|
||||
PageProviders: GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage),
|
||||
pageContentsCacheRWMutex: theMutex,
|
||||
RangeSupported: config.RangeSupported,
|
||||
FilterURLQueries: config.FilterURLQueries,
|
||||
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) {
|
||||
@ -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
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
package pageHandler
|
||||
|
||||
import "golang.captainalm.com/cityuni-webserver/pageHandler/pages"
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
return providers
|
||||
|
@ -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() {
|
||||
}
|
10
pageHandler/utils/buffered-writer.go
Normal file
10
pageHandler/utils/buffered-writer.go
Normal 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
6
utils/info/conf-info.go
Normal file
@ -0,0 +1,6 @@
|
||||
package info
|
||||
|
||||
import "golang.captainalm.com/cityuni-webserver/conf"
|
||||
|
||||
var ListenSettings conf.ListenYaml
|
||||
var ServeSettings conf.ServeYaml
|
13
utils/info/product-info.go
Normal file
13
utils/info/product-info.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user