aboutsummaryrefslogtreecommitdiff
path: root/page/misc.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2023-01-07 13:31:23 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2023-01-07 13:31:23 -0500
commitca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch)
treedeaabaf15d6d91079a68f247e46070399e4343ee /page/misc.go
parent97dd660925434be537cd9a49a1d0c893b223e357 (diff)
downloadgo-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.gz
go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.xz
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'page/misc.go')
-rw-r--r--page/misc.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/page/misc.go b/page/misc.go
index e603458..4b67945 100644
--- a/page/misc.go
+++ b/page/misc.go
@@ -2,10 +2,58 @@ package page
import (
"encoding/json"
+ "net/http"
+ "path/filepath"
"gopkg.in/yaml.v3"
)
+// logReq just maks that we've done a request in the log, format is roughly:
+// remoteAddr, Method, statusCode, page path.
+func logReq(r *http.Request, statusCode int) {
+ Logger.Printf("%s %s %d %s",
+ r.RemoteAddr,
+ r.Method,
+ statusCode,
+ GetPagePath(r))
+}
+
+// logErr logs messages in the format of:
+// RemoteAddr, Method, path: pagePath, message, error.
+func logErr(r *http.Request, msg string, err error) {
+ Logger.Printf("%s %s path: %s %s: %s",
+ r.RemoteAddr,
+ r.Method,
+ GetPagePath(r),
+ msg,
+ err)
+}
+
+// plainResp sends a text/plain response down to the writer with the appropriate
+// status code and a body of msg.
+func plainResp(w http.ResponseWriter, statusCode int, msg string) {
+ w.Header().Set("Content-type", "text/plain")
+ w.WriteHeader(statusCode)
+ _, _ = w.Write([]byte(msg))
+}
+
+// GetURLPath returns r.URL.Path, unless it's merely a slash, then
+// it returns "index". Usefulf for defining your own handlers.
+func GetURLPath(r *http.Request) string {
+ u := r.URL.Path
+ if u == "/" {
+ u = "/index"
+ }
+
+ return u
+}
+
+// GetPagePath is the same as GetURLPath except that it joins it with
+// the current directory ".". Also useful in defining your own handlers.
+func GetPagePath(r *http.Request) string {
+ return filepath.Join(".", GetURLPath(r))
+}
+
// EncodeYaml is meant to be used in templating functions to encode
// arbitrary information as a yaml string.
func (p Page) EncodeYaml(data interface{}) string {