From 2e842e6a0a92956332a27e16a428dc0bd322d1ff Mon Sep 17 00:00:00 2001 From: Seednode Date: Wed, 6 Sep 2023 17:31:05 -0500 Subject: [PATCH] Rearranged ServePage() to be more intuitive in order of operations --- cmd/web.go | 102 ++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index af38940..05d99ef 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -975,22 +975,50 @@ func ServePage(args []string) error { 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())) + mux := httprouter.New() + index := &Index{ mutex: sync.RWMutex{}, 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.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 { skipIndex := false @@ -1008,51 +1036,12 @@ func ServePage(args []string) error { 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 { mux.GET("/html", serveDebugHtml(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 { mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index) 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) } - 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, + if statistics { + if 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) + }() + } + + mux.GET("/stats", serveStats(args, stats)) } err = srv.ListenAndServe()