Update the system to use html templates.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
86840437cc
commit
012d464128
@ -14,9 +14,17 @@ type PackageMetaTagOutputter struct {
|
||||
}
|
||||
|
||||
func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string {
|
||||
return "<meta name=\"go-import\" content=\"" + pkgMTO.getPrefix(pathIn) + " git " + pkgMTO.getHomeURL(pathIn) + "\">\r\n" +
|
||||
"<meta name=\"go-source\" content=\"" + pkgMTO.getPrefix(pathIn) + " " + pkgMTO.getHomeURL(pathIn) + " " +
|
||||
pkgMTO.getDirectoryURL(pathIn) + " " + pkgMTO.getFileURL(pathIn) + "\">"
|
||||
return "<meta name=\"go-import\" content=\"" + pkgMTO.GetMetaContentForGoImport(pathIn) + "\">\r\n" +
|
||||
"<meta name=\"go-source\" content=\"" + pkgMTO.GetMetaContentForGoSource(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) {
|
||||
|
18
web/outputpage.html
Normal file
18
web/outputpage.html
Normal 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>
|
27
web/pagehandler-templatemarshal.go
Normal file
27
web/pagehandler-templatemarshal.go
Normal 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), "/")
|
||||
}
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"golang.captainalm.com/GOPackageHeaderServer/outputMeta"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PageHandler struct {
|
||||
@ -14,30 +14,36 @@ type PageHandler struct {
|
||||
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) {
|
||||
if request.Method == http.MethodGet || request.Method == http.MethodHead {
|
||||
thePage := "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n"
|
||||
if pgh.OutputPage && pgh.Name != "" {
|
||||
thePage += "<title>Go Package: " + pgh.Name + "</title>\r\n"
|
||||
tmpl, err := template.New("page-handler").Funcs(pageTemplateFuncMap).Parse(outputPage)
|
||||
if err != nil {
|
||||
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"
|
||||
if pgh.OutputPage {
|
||||
if pgh.Name != "" {
|
||||
thePage += "<h1>Go Package: " + pgh.Name + "</h1>\r\n"
|
||||
tm := handlerTemplateMarshal{
|
||||
PageHandler: *pgh,
|
||||
RequestPath: request.URL.Path,
|
||||
}
|
||||
var theLink string
|
||||
if pgh.MetaOutput.Username == "" {
|
||||
theLink = pgh.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Clean(request.URL.Path), "/")
|
||||
} else {
|
||||
theLink = pgh.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Join(pgh.MetaOutput.Username, request.URL.Path), "/")
|
||||
theBuffer := &BufferedWriter{}
|
||||
err = tmpl.Execute(theBuffer, tm)
|
||||
if err != nil {
|
||||
writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusInternalServerError, "Page Template Execution Failure")
|
||||
return
|
||||
}
|
||||
thePage += "<a href=\"" + theLink + "\">" + theLink + "</a>\r\n"
|
||||
}
|
||||
thePage += "</body>\r\n</html>\r\n"
|
||||
writer.Header().Set("Content-Length", strconv.Itoa(len([]byte(thePage))))
|
||||
writer.Header().Set("Content-Length", strconv.Itoa(len(theBuffer.Data)))
|
||||
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "") {
|
||||
_, _ = writer.Write([]byte(thePage))
|
||||
_, _ = writer.Write(theBuffer.Data)
|
||||
}
|
||||
} else {
|
||||
writer.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead)
|
||||
|
@ -22,3 +22,12 @@ func writeResponseHeaderCanWriteBody(method string, rw http.ResponseWriter, stat
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user