diff options
author | EuAndreh <eu@euandre.org> | 2024-11-14 18:36:06 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-11-14 18:36:06 -0300 |
commit | 0b70ac41c398df84387e0fd3872377697ae2a5a5 (patch) | |
tree | 80445efa011b1519ca0bc847790dec07e51833ab | |
parent | src/glaze.go: Add simplistic -X redirect implementation (diff) | |
download | glaze-0b70ac41c398df84387e0fd3872377697ae2a5a5.tar.gz glaze-0b70ac41c398df84387e0fd3872377697ae2a5a5.tar.xz |
src/glaze.go: Stick to 80 columns
-rw-r--r-- | src/glaze.go | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/glaze.go b/src/glaze.go index 2644211..673f7dc 100644 --- a/src/glaze.go +++ b/src/glaze.go @@ -72,7 +72,11 @@ func adjustPattern(pattern string) string { } } -func handleSymlink(info pathInfo, w http.ResponseWriter, r *http.Request) error { +func handleSymlink( + info pathInfo, + w http.ResponseWriter, + r *http.Request, +) error { linked, err := os.Readlink(info.path) if err != nil { return err @@ -101,7 +105,11 @@ func handleProxy(info pathInfo, w http.ResponseWriter, r *http.Request) error { target.Host = "localhost" httpClient := http.Client{ Transport: &http.Transport{ - DialContext: func(_ context.Context, _ string, _ string) (net.Conn, error) { + DialContext: func( + _ context.Context, + _ string, + _ string, + ) (net.Conn, error) { return net.Dial("unix", info.path) }, }, @@ -125,7 +133,13 @@ func handleProxy(info pathInfo, w http.ResponseWriter, r *http.Request) error { } func handleFile(info pathInfo, w http.ResponseWriter, r *http.Request) error { - http.ServeContent(w, r, info.fileInfo.Name(), info.fileInfo.ModTime(), info.fileHandle) + http.ServeContent( + w, + r, + info.fileInfo.Name(), + info.fileInfo.ModTime(), + info.fileHandle, + ) return nil } @@ -138,7 +152,11 @@ func getXattr(path string, name string) (string, error) { return string(data[0:i]), nil } -func handleDirectoryTarget(info pathInfo, w http.ResponseWriter, r *http.Request) error { +func handleDirectoryTarget( + info pathInfo, + w http.ResponseWriter, + r *http.Request, +) error { targetPath := withTrailingSlash(info.path) + r.URL.Path newInfo, fn, err := handlerForDynPath(targetPath) if err != nil { @@ -176,7 +194,9 @@ func handleDirectoryTarget(info pathInfo, w http.ResponseWriter, r *http.Request return nil } -func handlerFuncFor(mode fs.FileMode) (string, func(pathInfo, http.ResponseWriter, *http.Request) error) { +func handlerFuncFor( + mode fs.FileMode, +) (string, func(pathInfo, http.ResponseWriter, *http.Request) error) { if mode.Type() == fs.ModeSymlink { return "symlink", handleSymlink } else if mode.Type() == fs.ModeSocket { @@ -188,7 +208,9 @@ func handlerFuncFor(mode fs.FileMode) (string, func(pathInfo, http.ResponseWrite } } -func handlerForDynPath(path string) (pathInfo, func(pathInfo, http.ResponseWriter, *http.Request) error, error) { +func handlerForDynPath( + path string, +) (pathInfo, func(pathInfo, http.ResponseWriter, *http.Request) error, error) { var info pathInfo fileHandle, err := os.Open(path) if err != nil { @@ -211,7 +233,11 @@ func handlerForDynPath(path string) (pathInfo, func(pathInfo, http.ResponseWrite return info, fn, nil } -func logged(pattern string, path string, handler http.HandlerFunc) http.HandlerFunc { +func logged( + pattern string, + path string, + handler http.HandlerFunc, +) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() id := guuid.New().String() @@ -231,7 +257,8 @@ func logged(pattern string, path string, handler http.HandlerFunc) http.HandlerF ) handler(w, r) durationNano := time.Since(start) - durationMilli := float64(durationNano) / float64(time.Millisecond) + durationMilli := + float64(durationNano) / float64(time.Millisecond) g.Info( "in-response", "in-response", slices.Concat( @@ -284,6 +311,13 @@ func (_ *patternPath) Set(value string) error { return nil } +func registerRedirect() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + target := "https://" + r.Host + r.RequestURI + http.Redirect(w, r, target, http.StatusMovedPermanently) + }) +} + func parseArgs(args []string) string { var pat patternPath redirect := false @@ -303,10 +337,7 @@ func parseArgs(args []string) string { } if redirect { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - target := "https://" + r.Host + r.RequestURI - http.Redirect(w, r, target, http.StatusMovedPermanently) - }) + registerRedirect() } return fs.Arg(0) |