add middlewares

This commit is contained in:
2025-01-10 01:20:45 +08:00
parent 45f69d994e
commit b2445cf774
5 changed files with 158 additions and 2 deletions

View File

@ -9,6 +9,11 @@ import (
"time"
cacheproxy "git.jeffthecoder.xyz/guochao/cache-proxy"
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware"
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/handlerlog"
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/httplog"
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/recover"
"github.com/getsentry/sentry-go"
"gopkg.in/yaml.v3"
)
@ -115,9 +120,16 @@ func main() {
server := cacheproxy.NewServer(*config)
http.HandleFunc("GET /{path...}", server.HandleRequestWithCache)
mux := http.NewServeMux()
mux.HandleFunc("GET /", server.HandleRequestWithCache)
slog.With("addr", ":8881").Info("serving app")
if err := http.ListenAndServe(":8881", nil); err != nil {
if err := http.ListenAndServe(":8881", middleware.Use(mux,
recover.Recover(),
handlerlog.FindHandler(mux),
httplog.Log(httplog.Config{LogStart: true, LogFinish: true}),
)); err != nil {
slog.With("error", err).Error("failed to start server")
os.Exit(-1)
}