1、所有route使用interface方式注册。

This commit is contained in:
redhat 2025-07-04 16:21:27 +08:00
parent 9219363b7c
commit 22c58200dc
4 changed files with 46 additions and 27 deletions

42
controller/fileroute.go Normal file
View File

@ -0,0 +1,42 @@
package controller
import (
"dashboard/pkg/jwt"
"dashboard/routes"
"dashboard/settings"
"time"
"github.com/gin-gonic/gin"
)
type FileRoute struct {
cjwt *jwt.Jwt
}
func NewFileRoute(jwtC settings.JwtConfig) *FileRoute {
res := new(FileRoute)
res.cjwt = jwt.New(jwt.WithSalt(jwtC.Salt), jwt.WithExpire(time.Duration(jwtC.Expire)*time.Second))
return res
}
func (f *FileRoute) AddRoute(r *gin.Engine) {
// 静态文件服务
r.Static("/static", "./static")
r.StaticFile("/", "./static/index.html")
r.GET("login", LoginPage)
r.POST("/api/login", routes.ErrWapper(UserSignInHandler(f.cjwt)))
r.POST("/api/logout", UserLogOutHandler)
g1 := r.Group("/api")
g1.Use(routes.ErrWapper(routes.GinJwtAuthor(f.cjwt)))
{
g1.GET("/system-info", routes.ErrWapper(SystemInfoHandle))
g1.POST("/upload", routes.ErrWapper(FileUploadHandle))
g1.GET("/files", routes.ErrWapper(FileListHandle))
g1.GET("/download", routes.ErrWapper(FileDownloadHandle))
}
r.GET("/ws/terminal", routes.ErrWapper(routes.GinJwtAuthor(f.cjwt)), routes.ErrWapper(TerminalHandle()))
}

View File

@ -2,6 +2,7 @@ package main
import (
"context"
"dashboard/controller"
"dashboard/controller/unisc"
"dashboard/dao/sqldb"
"dashboard/logger"
@ -80,7 +81,7 @@ func main() {
errwg := errgroups.NewErrGroup()
errwg.Add(routes.NewRouter(fmt.Sprintf(":%d", sets.BaseConfig.Port),
log, *sets.RateLimitConfig, *sets.JwtConfig,
log, *sets.RateLimitConfig, controller.NewFileRoute(*sets.JwtConfig),
unisc.NewHttpRoute(route...)))
errwg.Add(sunisa...)

View File

@ -129,7 +129,7 @@ func NewFsm(name string, initState string, events Events, states StateRules, log
}
fsm.eventNames = append(fsm.eventNames, internalEvents...)
if len(logs) == 1 {
if len(logs) > 0 {
fsm.log = logs[0]
}
if fsm.log == nil {

View File

@ -2,9 +2,7 @@ package routes
import (
"context"
"dashboard/controller"
"dashboard/logger"
"dashboard/pkg/jwt"
"dashboard/settings"
"errors"
"net/http"
@ -30,9 +28,7 @@ type AddRouter interface {
AddRoute(*gin.Engine)
}
func NewRouter(addr string, log *logger.Logger, rate settings.RateLimitConfig, jwtC settings.JwtConfig, routes ...AddRouter) *Router {
cjwt := jwt.New(jwt.WithSalt(jwtC.Salt), jwt.WithExpire(time.Duration(jwtC.Expire)*time.Second))
func NewRouter(addr string, log *logger.Logger, rate settings.RateLimitConfig, routes ...AddRouter) *Router {
r := gin.New()
r.Use(cors.New(cors.Config{
@ -46,26 +42,6 @@ func NewRouter(addr string, log *logger.Logger, rate settings.RateLimitConfig, j
r.Use(GinLogger(log), GinRecovery(log, true), GinLog(log), ErrWapper(GinRateLimit(rate)))
// 静态文件服务
r.Static("/static", "./static")
r.StaticFile("/", "./static/index.html")
r.GET("login", controller.LoginPage)
r.POST("/api/login", ErrWapper(controller.UserSignInHandler(cjwt)))
r.POST("/api/logout", controller.UserLogOutHandler)
g1 := r.Group("/api")
g1.Use(ErrWapper(GinJwtAuthor(cjwt)))
{
g1.GET("/system-info", ErrWapper(controller.SystemInfoHandle))
g1.POST("/upload", ErrWapper(controller.FileUploadHandle))
g1.GET("/files", ErrWapper(controller.FileListHandle))
g1.GET("/download", ErrWapper(controller.FileDownloadHandle))
}
r.GET("/ws/terminal", ErrWapper(GinJwtAuthor(cjwt)), ErrWapper(controller.TerminalHandle()))
for _, route := range routes {
route.AddRoute(r)
}