diff --git a/outputMeta/packagemetatagoutputter.go b/outputMeta/packagemetatagoutputter.go index a1f4c09..72f361e 100644 --- a/outputMeta/packagemetatagoutputter.go +++ b/outputMeta/packagemetatagoutputter.go @@ -14,9 +14,17 @@ type PackageMetaTagOutputter struct { } func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string { - return "\r\n" + - "" + return "\r\n" + + "" +} + +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) { diff --git a/web/outputpage.html b/web/outputpage.html new file mode 100644 index 0000000..715586a --- /dev/null +++ b/web/outputpage.html @@ -0,0 +1,18 @@ + + + + {{ if isNotEmpty .PageHandler.Name }} + Go Package: {{ .PageHandler.Name }} + {{ end }} + + + + + {{ if .PageHandler.OutputPage }} + {{ if isNotEmpty .PageHandler.Name }} +

Go Package: {{ .PageHandler.Name }}

+ {{ end }} + {{ .GetLink }} + {{ end }} + + \ No newline at end of file diff --git a/web/pagehandler-templatemarshal.go b/web/pagehandler-templatemarshal.go new file mode 100644 index 0000000..bcde894 --- /dev/null +++ b/web/pagehandler-templatemarshal.go @@ -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), "/") + } +} diff --git a/web/pagehandler.go b/web/pagehandler.go index a8dfb3b..36769df 100644 --- a/web/pagehandler.go +++ b/web/pagehandler.go @@ -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 := "\r\n\r\n\r\n" - if pgh.OutputPage && pgh.Name != "" { - thePage += "Go Package: " + pgh.Name + "\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\r\n\r\n" - if pgh.OutputPage { - if pgh.Name != "" { - thePage += "

Go Package: " + pgh.Name + "

\r\n" - } - 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), "/") - } - thePage += "" + theLink + "\r\n" + tm := handlerTemplateMarshal{ + PageHandler: *pgh, + RequestPath: request.URL.Path, } - thePage += "\r\n\r\n" - writer.Header().Set("Content-Length", strconv.Itoa(len([]byte(thePage)))) + theBuffer := &BufferedWriter{} + err = tmpl.Execute(theBuffer, tm) + if err != nil { + writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusInternalServerError, "Page Template Execution Failure") + return + } + 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) diff --git a/web/utils.go b/web/utils.go index 382bb95..e5ffca5 100644 --- a/web/utils.go +++ b/web/utils.go @@ -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 +}