mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 18:20:50 +00:00
Four modules each called AuthInit and built their own instance, so which one Runtime handed back was decided by whichever module initialised last. The JwtToken key was also registered as an unbound method expression, which GetHandlerFunc's type assertion can never match - the key was registered and unusable at the same time. The instance is now built once here and registered as a bound closure. Modules read it back through GetAuthMiddleware, which is fatal rather than nil when called before InitMiddleware has run: a process without a JWT middleware should not reach the point of serving a request. Only one call site needs the instance itself rather than the handler (admin's /login, for LoginHandler); the thirty-odd MiddlewareFunc() call sites are unchanged. Claude-Session: https://claude.ai/code/session_01HPTAw8b8tAdFNFn8rKdPYx
39 lines
934 B
Go
39 lines
934 B
Go
package router
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/go-admin-team/go-admin-core/v2/logger"
|
|
"github.com/go-admin-team/go-admin-core/v2/sdk"
|
|
common "go-admin/common/middleware"
|
|
)
|
|
|
|
// InitRouter 路由初始化,不要怀疑,这里用到了
|
|
func InitRouter() {
|
|
var r *gin.Engine
|
|
h := sdk.Runtime.GetEngine()
|
|
if h == nil {
|
|
log.Fatal("not found engine...")
|
|
os.Exit(-1)
|
|
}
|
|
switch h.(type) {
|
|
case *gin.Engine:
|
|
r = h.(*gin.Engine)
|
|
default:
|
|
log.Fatal("not support other engine")
|
|
os.Exit(-1)
|
|
}
|
|
|
|
// the jwt middleware: shared instance InitMiddleware built at startup,
|
|
// not one built here per module (see common/middleware.GetAuthMiddleware).
|
|
authMiddleware := common.GetAuthMiddleware()
|
|
|
|
// 注册系统路由
|
|
InitSysRouter(r, authMiddleware)
|
|
|
|
// 注册业务路由
|
|
// TODO: 这里可存放业务路由,里边并无实际路由只有演示代码
|
|
InitExamplesRouter(r, authMiddleware)
|
|
}
|