2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 05:31:44 +08:00
Files
panel/internal/http/middleware/middleware.go

57 lines
1.5 KiB
Go

package middleware
import (
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-rat/sessions"
sessionmiddleware "github.com/go-rat/sessions/middleware"
"github.com/golang-cz/httplog"
"github.com/google/wire"
"github.com/knadh/koanf/v2"
"github.com/leonelquinteros/gotext"
"github.com/tnb-labs/panel/internal/biz"
)
var ProviderSet = wire.NewSet(NewMiddlewares)
type Middlewares struct {
conf *koanf.Koanf
log *slog.Logger
session *sessions.Manager
app biz.AppRepo
userToken biz.UserTokenRepo
}
func NewMiddlewares(conf *koanf.Koanf, log *slog.Logger, session *sessions.Manager, app biz.AppRepo, userToken biz.UserTokenRepo) *Middlewares {
return &Middlewares{
conf: conf,
log: log,
session: session,
app: app,
userToken: userToken,
}
}
// Globals is a collection of global middleware that will be applied to every request.
func (r *Middlewares) Globals(t *gotext.Locale, mux *chi.Mux) []func(http.Handler) http.Handler {
return []func(http.Handler) http.Handler{
middleware.Recoverer,
//middleware.SupressNotFound(mux),// bug https://github.com/go-chi/chi/pull/940
middleware.StripSlashes,
httplog.RequestLogger(r.log, &httplog.Options{
Level: slog.LevelInfo,
LogRequestHeaders: []string{"User-Agent"},
}),
middleware.Compress(5),
sessionmiddleware.StartSession(r.session),
Status(t),
Entrance(t, r.conf, r.session),
MustLogin(t, r.session, r.userToken),
MustInstall(t, r.app),
}
}