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,15 +1036,22 @@ func ServePage(args []string) error {
mux.GET("/clear_cache", serveCacheClear(args, index)) mux.GET("/clear_cache", serveCacheClear(args, index))
} }
stats := &ServeStats{ if debug {
mutex: sync.RWMutex{}, mux.GET("/html", serveDebugHtml(args, index))
list: []string{},
count: make(map[string]uint32), mux.GET("/json", serveDebugJson(args, index))
size: make(map[string]string),
times: make(map[string][]string),
} }
if statistics && statisticsFile != "" { if profile {
mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
}
if statistics {
if statisticsFile != "" {
stats.Import(statisticsFile) stats.Import(statisticsFile)
gracefulShutdown := make(chan os.Signal, 1) gracefulShutdown := make(chan os.Signal, 1)
@ -1031,44 +1066,9 @@ func ServePage(args []string) error {
}() }()
} }
if statistics {
mux.GET("/stats", serveStats(args, stats)) 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)
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
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,
}
err = srv.ListenAndServe() err = srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) { if !errors.Is(err, http.ErrServerClosed) {
return err return err