Compile regex at start, instead of each page load

This commit is contained in:
Seednode 2022-10-23 13:45:49 -05:00
parent 76109342ed
commit aa2413eafc
2 changed files with 12 additions and 11 deletions

View File

@ -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 {

View File

@ -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)