package web import ( "golang.captainalm.com/GOPackageHeaderServer/outputMeta" "net/http" "path" "strconv" "strings" ) type PageHandler struct { Name string OutputPage bool MetaOutput *outputMeta.PackageMetaTagOutputter } 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" } 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" } thePage += "\r\n\r\n" writer.Header().Set("Content-Length", strconv.Itoa(len([]byte(thePage)))) writer.Header().Set("Content-Type", "text/html; charset=utf-8") if writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "") { _, _ = writer.Write([]byte(thePage)) } } else { writer.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead) if request.Method == http.MethodOptions { writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "") } else { writeResponseHeaderCanWriteBody(request.Method, writer, http.StatusMethodNotAllowed, "") } } }