Update the system to use html templates.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Captain ALM 2022-07-12 11:04:25 +01:00
parent 86840437cc
commit 012d464128
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
5 changed files with 91 additions and 23 deletions

View File

@ -14,9 +14,17 @@ type PackageMetaTagOutputter struct {
} }
func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string { func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string {
return "<meta name=\"go-import\" content=\"" + pkgMTO.getPrefix(pathIn) + " git " + pkgMTO.getHomeURL(pathIn) + "\">\r\n" + return "<meta name=\"go-import\" content=\"" + pkgMTO.GetMetaContentForGoImport(pathIn) + "\">\r\n" +
"<meta name=\"go-source\" content=\"" + pkgMTO.getPrefix(pathIn) + " " + pkgMTO.getHomeURL(pathIn) + " " + "<meta name=\"go-source\" content=\"" + pkgMTO.GetMetaContentForGoSource(pathIn) + "\">"
pkgMTO.getDirectoryURL(pathIn) + " " + pkgMTO.getFileURL(pathIn) + "\">" }
func (pkgMTO *PackageMetaTagOutputter) GetMetaContentForGoImport(pathIn string) string {
return pkgMTO.getPrefix(pathIn) + " git " + pkgMTO.getHomeURL(pathIn)
}
func (pkgMTO *PackageMetaTagOutputter) GetMetaContentForGoSource(pathIn string) string {
return pkgMTO.getPrefix(pathIn) + " " + pkgMTO.getHomeURL(pathIn) + " " +
pkgMTO.getDirectoryURL(pathIn) + " " + pkgMTO.getFileURL(pathIn)
} }
func (pkgMTO *PackageMetaTagOutputter) assureBasePrefixURL() (failed bool) { func (pkgMTO *PackageMetaTagOutputter) assureBasePrefixURL() (failed bool) {

18
web/outputpage.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
{{ if isNotEmpty .PageHandler.Name }}
<title>Go Package: {{ .PageHandler.Name }}</title>
{{ end }}
<meta name="go-import" content="{{ .GetGoImportMetaContent }}">
<meta name="go-source" content="{{ .GetGoSourceMetaContent }}">
</head>
<body>
{{ if .PageHandler.OutputPage }}
{{ if isNotEmpty .PageHandler.Name }}
<h1>Go Package: {{ .PageHandler.Name }}</h1>
{{ end }}
<a href="{{ .GetLink }}">{{ .GetLink }}</a>
{{ end }}
</body>
</html>

View File

@ -0,0 +1,27 @@
package web
import (
"path"
"strings"
)
type handlerTemplateMarshal struct {
PageHandler PageHandler
RequestPath string
}
func (htm handlerTemplateMarshal) GetGoImportMetaContent() string {
return htm.PageHandler.MetaOutput.GetMetaContentForGoImport(htm.RequestPath)
}
func (htm handlerTemplateMarshal) GetGoSourceMetaContent() string {
return htm.PageHandler.MetaOutput.GetMetaContentForGoSource(htm.RequestPath)
}
func (htm handlerTemplateMarshal) GetLink() string {
if htm.PageHandler.MetaOutput.Username == "" {
return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Clean(htm.RequestPath), "/")
} else {
return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Join(htm.PageHandler.MetaOutput.Username, htm.RequestPath), "/")
}
}

View File

@ -1,11 +1,11 @@
package web package web
import ( import (
_ "embed"
"golang.captainalm.com/GOPackageHeaderServer/outputMeta" "golang.captainalm.com/GOPackageHeaderServer/outputMeta"
"html/template"
"net/http" "net/http"
"path"
"strconv" "strconv"
"strings"
) )
type PageHandler struct { type PageHandler struct {
@ -14,30 +14,36 @@ type PageHandler struct {
MetaOutput *outputMeta.PackageMetaTagOutputter MetaOutput *outputMeta.PackageMetaTagOutputter
} }
//go:embed outputpage.html
var outputPage string
var pageTemplateFuncMap template.FuncMap = template.FuncMap{
"isNotEmpty": func(stringIn string) bool {
return stringIn != ""
},
}
func (pgh *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (pgh *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.Method == http.MethodGet || request.Method == http.MethodHead { if request.Method == http.MethodGet || request.Method == http.MethodHead {
thePage := "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n" tmpl, err := template.New("page-handler").Funcs(pageTemplateFuncMap).Parse(outputPage)
if pgh.OutputPage && pgh.Name != "" { if err != nil {
thePage += "<title>Go Package: " + pgh.Name + "</title>\r\n" writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusInternalServerError, "Page Template Parsing Failure")
return
} }
thePage += pgh.MetaOutput.GetMetaTags(request.URL.Path) + "\r\n</head>\r\n<body>\r\n" tm := handlerTemplateMarshal{
if pgh.OutputPage { PageHandler: *pgh,
if pgh.Name != "" { RequestPath: request.URL.Path,
thePage += "<h1>Go Package: " + pgh.Name + "</h1>\r\n"
} }
var theLink string theBuffer := &BufferedWriter{}
if pgh.MetaOutput.Username == "" { err = tmpl.Execute(theBuffer, tm)
theLink = pgh.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Clean(request.URL.Path), "/") if err != nil {
} else { writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusInternalServerError, "Page Template Execution Failure")
theLink = pgh.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Join(pgh.MetaOutput.Username, request.URL.Path), "/") return
} }
thePage += "<a href=\"" + theLink + "\">" + theLink + "</a>\r\n" writer.Header().Set("Content-Length", strconv.Itoa(len(theBuffer.Data)))
}
thePage += "</body>\r\n</html>\r\n"
writer.Header().Set("Content-Length", strconv.Itoa(len([]byte(thePage))))
writer.Header().Set("Content-Type", "text/html; charset=utf-8") writer.Header().Set("Content-Type", "text/html; charset=utf-8")
if writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "") { if writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "") {
_, _ = writer.Write([]byte(thePage)) _, _ = writer.Write(theBuffer.Data)
} }
} else { } else {
writer.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead) writer.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead)

View File

@ -22,3 +22,12 @@ func writeResponseHeaderCanWriteBody(method string, rw http.ResponseWriter, stat
} }
return false return false
} }
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
}