Rearranged ServePage() to be more intuitive in order of operations

This commit is contained in:
Seednode 2023-09-06 17:31:05 -05:00
parent 05f8084089
commit 2e842e6a0a
1 changed files with 51 additions and 51 deletions

View File

@ -975,22 +975,50 @@ func ServePage(args []string) error {
return errors.New("no supported files found in provided paths") return errors.New("no supported files found in provided paths")
} }
Regexes := &Regexes{
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
}
rand.New(rand.NewSource(time.Now().UnixNano())) rand.New(rand.NewSource(time.Now().UnixNano()))
mux := httprouter.New()
index := &Index{ index := &Index{
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
list: []string{}, list: []string{},
} }
mux := httprouter.New() Regexes := &Regexes{
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
}
srv := &http.Server{
Addr: net.JoinHostPort(bind, strconv.Itoa(int(port))),
Handler: mux,
IdleTimeout: 10 * time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Minute,
}
stats := &ServeStats{
mutex: sync.RWMutex{},
list: []string{},
count: make(map[string]uint32),
size: make(map[string]string),
times: make(map[string][]string),
}
mux.PanicHandler = serverErrorHandler() mux.PanicHandler = serverErrorHandler()
mux.GET("/", serveRoot(paths, Regexes, index))
mux.GET("/favicons/*favicon", serveFavicons())
mux.GET("/favicon.ico", serveFavicons())
mux.GET(ImagePrefix+"/*image", serveImage(paths, Regexes, index))
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats))
mux.GET("/version", serveVersion())
if cache { if cache {
skipIndex := false skipIndex := false
@ -1008,51 +1036,12 @@ func ServePage(args []string) error {
mux.GET("/clear_cache", serveCacheClear(args, index)) mux.GET("/clear_cache", serveCacheClear(args, index))
} }
stats := &ServeStats{
mutex: sync.RWMutex{},
list: []string{},
count: make(map[string]uint32),
size: make(map[string]string),
times: make(map[string][]string),
}
if statistics && statisticsFile != "" {
stats.Import(statisticsFile)
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-gracefulShutdown
stats.Export(statisticsFile)
os.Exit(0)
}()
}
if statistics {
mux.GET("/stats", serveStats(args, stats))
}
if debug { if debug {
mux.GET("/html", serveDebugHtml(args, index)) mux.GET("/html", serveDebugHtml(args, index))
mux.GET("/json", serveDebugJson(args, index)) mux.GET("/json", serveDebugJson(args, index))
} }
mux.GET("/", serveRoot(paths, Regexes, index))
mux.GET("/favicons/*favicon", serveFavicons())
mux.GET("/favicon.ico", serveFavicons())
mux.GET(ImagePrefix+"/*image", serveImage(paths, Regexes, index))
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats))
mux.GET("/version", serveVersion())
if profile { if profile {
mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index) mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline) mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
@ -1061,12 +1050,23 @@ func ServePage(args []string) error {
mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace) mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
} }
srv := &http.Server{ if statistics {
Addr: net.JoinHostPort(bind, strconv.Itoa(int(port))), if statisticsFile != "" {
Handler: mux, stats.Import(statisticsFile)
IdleTimeout: 10 * time.Minute,
ReadTimeout: 5 * time.Second, gracefulShutdown := make(chan os.Signal, 1)
WriteTimeout: 5 * time.Minute, signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-gracefulShutdown
stats.Export(statisticsFile)
os.Exit(0)
}()
}
mux.GET("/stats", serveStats(args, stats))
} }
err = srv.ListenAndServe() err = srv.ListenAndServe()