From aa2413eafc58f41cb6e8cbcce647890c6507a70d Mon Sep 17 00:00:00 2001 From: Seednode Date: Sun, 23 Oct 2022 13:45:49 -0500 Subject: [PATCH] Compile regex at start, instead of each page load --- cmd/files.go | 4 +--- cmd/web.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 778fb36..6006ef2 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -192,12 +192,10 @@ func getPreviousFile(p *Path) (string, error) { return fileName, err } -func splitPath(path string) (*Path, error) { +func splitPath(path string, re regexp.Regexp) (*Path, error) { p := Path{} var err error - re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) - split := re.FindAllStringSubmatch(path, -1) if len(split) < 1 || len(split[0]) < 3 { diff --git a/cmd/web.go b/cmd/web.go index 2d7bba2..59803c5 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "strconv" "strings" "time" @@ -227,7 +228,7 @@ func serveStaticFileHandler(paths []string) appHandler { } } -func serveHtmlHandler(paths []string) appHandler { +func serveHtmlHandler(paths []string, re regexp.Regexp) appHandler { return func(w http.ResponseWriter, r *http.Request) error { refererUri, err := stripQueryParams(refererToUri(r.Referer())) if err != nil { @@ -254,7 +255,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(query) + path, err := splitPath(query, re) if err != nil { return err } @@ -275,7 +276,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(filePath) + path, err := splitPath(filePath, re) if err != nil { return err } @@ -302,7 +303,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(filePath) + path, err := splitPath(filePath, re) if err != nil { return err } @@ -324,7 +325,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(query) + path, err := splitPath(query, re) if err != nil { return err } @@ -345,7 +346,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(filePath) + path, err := splitPath(filePath, re) if err != nil { return err } @@ -372,7 +373,7 @@ func serveHtmlHandler(paths []string) appHandler { return err } - path, err := splitPath(filePath) + path, err := splitPath(filePath, re) if err != nil { return err } @@ -450,7 +451,9 @@ func ServePage(args []string) error { fmt.Println("Paths: " + i) } - http.Handle("/", serveHtmlHandler(paths)) + re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) + + http.Handle("/", serveHtmlHandler(paths, *re)) http.Handle(PREFIX+"/", http.StripPrefix(PREFIX, serveStaticFileHandler(paths))) http.HandleFunc("/favicon.ico", doNothing)