Skip to content

Commit

Permalink
feat: 中间件注入
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Dec 15, 2024
1 parent 8116ea7 commit fe4f0d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/web/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"github.com/TheTNB/panel/internal/http/middleware"
"github.com/TheTNB/panel/internal/job"
"github.com/google/wire"

Expand All @@ -15,5 +16,5 @@ import (

// initWeb init application.
func initWeb() (*app.Web, error) {
panic(wire.Build(bootstrap.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, job.ProviderSet, app.NewWeb))
panic(wire.Build(bootstrap.ProviderSet, middleware.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, job.ProviderSet, app.NewWeb))
}
12 changes: 7 additions & 5 deletions cmd/web/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/bootstrap/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/TheTNB/panel/internal/route"
)

func NewRouter(conf *koanf.Koanf, db *gorm.DB, log *slog.Logger, session *sessions.Manager, http *route.Http, ws *route.Ws) (*chi.Mux, error) {
func NewRouter(conf *koanf.Koanf, db *gorm.DB, log *slog.Logger, session *sessions.Manager, middlewares *middleware.Middlewares, http *route.Http, ws *route.Ws) (*chi.Mux, error) {
r := chi.NewRouter()

// add middleware
r.Use(middleware.GlobalMiddleware(r, conf, db, log, session)...)
r.Use(middlewares.Globals(r)...)
// add http route
http.Register(r)
// add ws route
Expand Down
37 changes: 28 additions & 9 deletions internal/http/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package middleware

import (
"github.com/TheTNB/panel/internal/biz"
"github.com/go-chi/chi/v5"
"github.com/go-rat/sessions"
"github.com/google/wire"
"github.com/knadh/koanf/v2"
"gorm.io/gorm"
"log/slog"
"net/http"

Expand All @@ -13,22 +14,40 @@ import (
"github.com/golang-cz/httplog"
)

// GlobalMiddleware is a collection of global middleware that will be applied to every request.
func GlobalMiddleware(r *chi.Mux, conf *koanf.Koanf, db *gorm.DB, log *slog.Logger, session *sessions.Manager) []func(http.Handler) http.Handler {
var ProviderSet = wire.NewSet(NewMiddlewares)

type Middlewares struct {
conf *koanf.Koanf
log *slog.Logger
session *sessions.Manager
app biz.AppRepo
}

func NewMiddlewares(conf *koanf.Koanf, log *slog.Logger, session *sessions.Manager, app biz.AppRepo) *Middlewares {
return &Middlewares{
conf: conf,
log: log,
session: session,
app: app,
}
}

// Globals is a collection of global middleware that will be applied to every request.
func (r *Middlewares) Globals(mux *chi.Mux) []func(http.Handler) http.Handler {
return []func(http.Handler) http.Handler{
sessionmiddleware.StartSession(session),
//middleware.SupressNotFound(r),// bug https://github.com/go-chi/chi/pull/940
sessionmiddleware.StartSession(r.session),
//middleware.SupressNotFound(mux),// bug https://github.com/go-chi/chi/pull/940
middleware.CleanPath,
middleware.StripSlashes,
middleware.Compress(5),
httplog.RequestLogger(log, &httplog.Options{
httplog.RequestLogger(r.log, &httplog.Options{
Level: slog.LevelInfo,
LogRequestHeaders: []string{"User-Agent"},
}),
middleware.Recoverer,
Status,
Entrance(conf, session),
MustLogin(session),
MustInstall(db),
Entrance(r.conf, r.session),
MustLogin(r.session),
MustInstall(r.app),
}
}
7 changes: 3 additions & 4 deletions internal/http/middleware/must_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package middleware

import (
"fmt"
"github.com/TheTNB/panel/internal/data"
"gorm.io/gorm"
"github.com/TheTNB/panel/internal/biz"
"net/http"
"strings"

"github.com/go-rat/chix"
)

// MustInstall 确保已安装应用
func MustInstall(db *gorm.DB) func(next http.Handler) http.Handler {
func MustInstall(app biz.AppRepo) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var slugs []string
Expand All @@ -34,7 +33,7 @@ func MustInstall(db *gorm.DB) func(next http.Handler) http.Handler {

flag := false
for _, s := range slugs {
if installed, _ := data.NewAppRepo(db, nil, nil).IsInstalled("slug = ?", s); installed { // TODO 优化实现
if installed, _ := app.IsInstalled("slug = ?", s); installed { // TODO 优化实现
flag = true
break
}
Expand Down

0 comments on commit fe4f0d6

Please sign in to comment.